Remove Movement interface - make DefaultMovement a final Movement class and move it to :API

This commit is contained in:
2021-03-30 13:15:59 +02:00
parent c87567358c
commit cb59434bc7
7 changed files with 41 additions and 48 deletions

View File

@@ -30,4 +30,6 @@ public interface Movable extends Placeable {
int chebyshevDistance(Vector2ic coordinates); int chebyshevDistance(Vector2ic coordinates);
int manhattanDistance(Vector2ic coordinates); int manhattanDistance(Vector2ic coordinates);
boolean move(Movement movement);
} }

View File

@@ -1,17 +1,32 @@
package com.bartlomiejpluta.base.api.move; package com.bartlomiejpluta.base.api.move;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import org.joml.Vector2i;
import org.joml.Vector2ic; import org.joml.Vector2ic;
public interface Movement { @Getter
Movable getObject(); @EqualsAndHashCode
public final class Movement {
private final Movable object;
private final Direction direction;
private final Vector2ic from;
private final Vector2ic to;
boolean perform(); public Movement(@NonNull Movable object, @NonNull Direction direction) {
this.object = object;
this.direction = direction;
Movement another(); this.from = object.getCoordinates();
this.to = direction.vector.add(object.getCoordinates(), new Vector2i());
}
Vector2ic getFrom(); public boolean perform() {
return object.move(this);
}
Vector2ic getTo(); public Movement another() {
return object.prepareMovement(direction);
Direction getDirection(); }
} }

View File

@@ -147,7 +147,7 @@ public abstract class AnimationDelegate implements Animation {
@Override @Override
public Movement prepareMovement(Direction direction) { public Movement prepareMovement(Direction direction) {
return animation.prepareMovement(direction); return new Movement(this, direction);
} }
@Override @Override
@@ -195,6 +195,11 @@ public abstract class AnimationDelegate implements Animation {
animation.setSpeed(speed); animation.setSpeed(speed);
} }
@Override
public boolean move(Movement movement) {
return animation.move(movement);
}
@Override @Override
public void onAdd(Layer layer) { public void onAdd(Layer layer) {
animation.onAdd(layer); animation.onAdd(layer);

View File

@@ -41,7 +41,7 @@ public abstract class EntityDelegate implements Entity {
@Override @Override
public Movement prepareMovement(Direction direction) { public Movement prepareMovement(Direction direction) {
return entity.prepareMovement(direction); return new Movement(this, direction);
} }
@Override @Override
@@ -229,6 +229,11 @@ public abstract class EntityDelegate implements Entity {
entity.setZIndex(zIndex); entity.setZIndex(zIndex);
} }
@Override
public boolean move(Movement movement) {
return entity.move(movement);
}
@Override @Override
public void update(float dt) { public void update(float dt) {
entity.update(dt); entity.update(dt);

View File

@@ -87,7 +87,7 @@ public class DefaultEntity extends MovableSprite implements Entity {
} }
@Override @Override
protected boolean move(Movement movement) { public boolean move(Movement movement) {
if (super.move(movement)) { if (super.move(movement)) {
faceDirection = movement.getDirection(); faceDirection = movement.getDirection();
return true; return true;

View File

@@ -1,35 +0,0 @@
package com.bartlomiejpluta.base.engine.world.movement;
import com.bartlomiejpluta.base.api.move.Direction;
import com.bartlomiejpluta.base.api.move.Movement;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.joml.Vector2i;
import org.joml.Vector2ic;
@Getter
@EqualsAndHashCode
public class DefaultMovement implements Movement {
private final MovableSprite object;
private final Direction direction;
private final Vector2ic from;
private final Vector2ic to;
DefaultMovement(MovableSprite object, Direction direction) {
this.object = object;
this.direction = direction;
this.from = object.getCoordinates();
this.to = direction.vector.add(object.getCoordinates(), new Vector2i());
}
@Override
public boolean perform() {
return object.move(this);
}
@Override
public Movement another() {
return object.prepareMovement(direction);
}
}

View File

@@ -82,10 +82,11 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
@Override @Override
public Movement prepareMovement(Direction direction) { public Movement prepareMovement(Direction direction) {
return new DefaultMovement(this, direction); return new Movement(this, direction);
} }
protected boolean move(Movement movement) { @Override
public boolean move(Movement movement) {
if (this.movement != null) { if (this.movement != null) {
return false; return false;
} }