Create InteractiveEntity
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package com.bartlomiejpluta.base.api.entity;
|
||||
|
||||
import com.bartlomiejpluta.base.api.move.Movement;
|
||||
|
||||
public interface InteractiveEntity extends Entity {
|
||||
void onEntityStepIn(Movement movement, Entity entity);
|
||||
|
||||
void onEntityStepOut(Movement movement, Entity entity);
|
||||
}
|
||||
@@ -30,4 +30,9 @@ public interface ObjectLayer extends Layer {
|
||||
boolean isTileReachable(Vector2ic tileCoordinates);
|
||||
|
||||
void pushMovement(Movement movement);
|
||||
|
||||
// Notifiers
|
||||
void notifyEntityStepIn(Movement movement, Entity entity);
|
||||
|
||||
void notifyEntityStepOut(Movement movement, Entity entity);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.bartlomiejpluta.base.api.move;
|
||||
|
||||
import com.bartlomiejpluta.base.api.animation.Animation;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
public final class AnimationMovement implements Movement {
|
||||
private final Animation object;
|
||||
private final Direction direction;
|
||||
private final Vector2ic from;
|
||||
private final Vector2ic to;
|
||||
|
||||
public AnimationMovement(@NonNull Animation object, @NonNull 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 void abort() {
|
||||
object.abortMove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.bartlomiejpluta.base.api.move;
|
||||
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
public final class EntityMovement implements Movement {
|
||||
private final Entity object;
|
||||
private final Direction direction;
|
||||
private final Vector2ic from;
|
||||
private final Vector2ic to;
|
||||
|
||||
public EntityMovement(@NonNull Entity object, @NonNull 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() {
|
||||
var result = object.move(this);
|
||||
if (result) {
|
||||
object.getLayer().notifyEntityStepOut(this, object);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abort() {
|
||||
object.abortMove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
object.getLayer().notifyEntityStepIn(this, object);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,18 @@
|
||||
package com.bartlomiejpluta.base.api.move;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
public final class Movement {
|
||||
private final Movable object;
|
||||
private final Direction direction;
|
||||
private final Vector2ic from;
|
||||
private final Vector2ic to;
|
||||
public interface Movement {
|
||||
|
||||
public Movement(@NonNull Movable object, @NonNull Direction direction) {
|
||||
this.object = object;
|
||||
this.direction = direction;
|
||||
Vector2ic getFrom();
|
||||
|
||||
this.from = object.getCoordinates();
|
||||
this.to = direction.vector.add(object.getCoordinates(), new Vector2i());
|
||||
}
|
||||
Vector2ic getTo();
|
||||
|
||||
public boolean perform() {
|
||||
return object.move(this);
|
||||
}
|
||||
Direction getDirection();
|
||||
|
||||
public void abort() {
|
||||
object.abortMove();
|
||||
}
|
||||
boolean perform();
|
||||
|
||||
public Movement another() {
|
||||
return object.prepareMovement(direction);
|
||||
}
|
||||
void abort();
|
||||
|
||||
void onFinish();
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.lib.animation;
|
||||
import com.bartlomiejpluta.base.api.animation.Animation;
|
||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.map.layer.base.Layer;
|
||||
import com.bartlomiejpluta.base.api.move.AnimationMovement;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import com.bartlomiejpluta.base.api.move.Movement;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
@@ -147,7 +148,7 @@ public abstract class AnimationDelegate implements Animation {
|
||||
|
||||
@Override
|
||||
public Movement prepareMovement(Direction direction) {
|
||||
return new Movement(this, direction);
|
||||
return new AnimationMovement(this, direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import com.bartlomiejpluta.base.api.move.EntityMovement;
|
||||
import com.bartlomiejpluta.base.api.move.Movement;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.internal.object.Placeable;
|
||||
@@ -41,7 +42,7 @@ public abstract class EntityDelegate implements Entity {
|
||||
|
||||
@Override
|
||||
public Movement prepareMovement(Direction direction) {
|
||||
return new Movement(this, direction);
|
||||
return new EntityMovement(this, direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user