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
|
||||
|
||||
@@ -3,6 +3,9 @@ package com.bartlomiejpluta.base.engine.world.animation.model;
|
||||
import com.bartlomiejpluta.base.api.animation.Animation;
|
||||
import com.bartlomiejpluta.base.api.map.layer.base.Layer;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
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.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
||||
@@ -94,6 +97,11 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movement prepareMovement(Direction direction) {
|
||||
return new AnimationMovement(this, direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdd(Layer layer) {
|
||||
this.layer = layer;
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.engine.world.entity.model;
|
||||
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.engine.core.gl.object.mesh.Mesh;
|
||||
import com.bartlomiejpluta.base.engine.world.entity.manager.EntitySetManager;
|
||||
@@ -89,6 +90,11 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
material.setSpritePosition(spriteDefaultRows.get(faceDirection));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movement prepareMovement(Direction direction) {
|
||||
return new EntityMovement(this, direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean move(Movement movement) {
|
||||
if (super.move(movement)) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.engine.world.map.layer.object;
|
||||
import com.bartlomiejpluta.base.api.ai.NPC;
|
||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.entity.InteractiveEntity;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.PassageAbility;
|
||||
import com.bartlomiejpluta.base.api.map.model.GameMap;
|
||||
@@ -28,6 +29,8 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
||||
@Getter
|
||||
private final List<Entity> entities;
|
||||
|
||||
private final List<InteractiveEntity> interactiveEntities = new ArrayList<>();
|
||||
|
||||
@Getter
|
||||
private final PassageAbility[][] passageMap;
|
||||
|
||||
@@ -131,35 +134,14 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
super.update(dt);
|
||||
|
||||
while (!movements.isEmpty()) {
|
||||
var movement = movements.poll();
|
||||
var from = movement.getFrom();
|
||||
var to = movement.getTo();
|
||||
if (isTileReachable(to)) {
|
||||
movement.perform();
|
||||
|
||||
for(var rule : movementRules) {
|
||||
if(((from.equals(rule.from())) || (to.equals(rule.to())))) {
|
||||
rule.invoke(movement);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var entity : entities) {
|
||||
entity.update(dt);
|
||||
|
||||
if (entity instanceof NPC) {
|
||||
((NPC) entity).getStrategy().nextActivity(this, dt);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert entities requested to be added
|
||||
if (!entitiesToAdd.isEmpty()) {
|
||||
for (var entity : entitiesToAdd) {
|
||||
entity.onAdd(this);
|
||||
if (entity instanceof InteractiveEntity) {
|
||||
interactiveEntities.add((InteractiveEntity) entity);
|
||||
}
|
||||
|
||||
entities.add(entity);
|
||||
}
|
||||
|
||||
@@ -176,6 +158,10 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
||||
if (!entitiesToRemove.isEmpty()) {
|
||||
for (var entity : entitiesToRemove) {
|
||||
entities.remove(entity);
|
||||
if (entity instanceof InteractiveEntity) {
|
||||
interactiveEntities.remove(entity);
|
||||
}
|
||||
|
||||
entity.onRemove(this);
|
||||
}
|
||||
|
||||
@@ -187,6 +173,34 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
||||
movementRules.removeAll(movementRulesToRemove);
|
||||
movementRulesToRemove.clear();
|
||||
}
|
||||
|
||||
// Update BaseLayer (animations etc.)
|
||||
super.update(dt);
|
||||
|
||||
// Update movements
|
||||
while (!movements.isEmpty()) {
|
||||
var movement = movements.poll();
|
||||
var from = movement.getFrom();
|
||||
var to = movement.getTo();
|
||||
if (isTileReachable(to)) {
|
||||
movement.perform();
|
||||
|
||||
for (var rule : movementRules) {
|
||||
if (((from.equals(rule.from())) || (to.equals(rule.to())))) {
|
||||
rule.invoke(movement);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update entities
|
||||
for (var entity : entities) {
|
||||
entity.update(dt);
|
||||
|
||||
if (entity instanceof NPC) {
|
||||
((NPC) entity).getStrategy().nextActivity(this, dt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -204,4 +218,26 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
||||
var z = compare(a.getZIndex(), b.getZIndex());
|
||||
return z == 0 ? compare(a.getPosition().y(), b.getPosition().y()) : z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyEntityStepIn(Movement movement, Entity entity) {
|
||||
var target = movement.getTo();
|
||||
|
||||
for (var listener : interactiveEntities) {
|
||||
if (listener.getCoordinates().equals(target)) {
|
||||
listener.onEntityStepIn(movement, entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyEntityStepOut(Movement movement, Entity entity) {
|
||||
var source = movement.getFrom();
|
||||
|
||||
for (var listener : interactiveEntities) {
|
||||
if (listener.getCoordinates().equals(source)) {
|
||||
listener.onEntityStepOut(movement, entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.bartlomiejpluta.base.engine.world.movement;
|
||||
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import com.bartlomiejpluta.base.api.move.Movable;
|
||||
import com.bartlomiejpluta.base.api.move.Movement;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
@@ -30,8 +29,6 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
||||
|
||||
private enum PlacingMode {BY_POSITION, BY_COORDINATES}
|
||||
|
||||
;
|
||||
|
||||
private PlacingMode placingMode;
|
||||
|
||||
@Getter
|
||||
@@ -68,6 +65,7 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
||||
} else {
|
||||
adjustCoordinates();
|
||||
setDefaultAnimationFrame();
|
||||
movement.onFinish();
|
||||
movementVector = null;
|
||||
movement = null;
|
||||
}
|
||||
@@ -80,11 +78,6 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
||||
setCoordinates(movement.getTo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movement prepareMovement(Direction direction) {
|
||||
return new Movement(this, direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean move(Movement movement) {
|
||||
if (this.movement != null) {
|
||||
|
||||
Reference in New Issue
Block a user