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);
|
boolean isTileReachable(Vector2ic tileCoordinates);
|
||||||
|
|
||||||
void pushMovement(Movement movement);
|
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;
|
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;
|
||||||
|
|
||||||
@Getter
|
public interface Movement {
|
||||||
@EqualsAndHashCode
|
|
||||||
public final class Movement {
|
|
||||||
private final Movable object;
|
|
||||||
private final Direction direction;
|
|
||||||
private final Vector2ic from;
|
|
||||||
private final Vector2ic to;
|
|
||||||
|
|
||||||
public Movement(@NonNull Movable object, @NonNull Direction direction) {
|
Vector2ic getFrom();
|
||||||
this.object = object;
|
|
||||||
this.direction = direction;
|
|
||||||
|
|
||||||
this.from = object.getCoordinates();
|
Vector2ic getTo();
|
||||||
this.to = direction.vector.add(object.getCoordinates(), new Vector2i());
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean perform() {
|
Direction getDirection();
|
||||||
return object.move(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void abort() {
|
boolean perform();
|
||||||
object.abortMove();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Movement another() {
|
void abort();
|
||||||
return object.prepareMovement(direction);
|
|
||||||
}
|
void onFinish();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.lib.animation;
|
|||||||
import com.bartlomiejpluta.base.api.animation.Animation;
|
import com.bartlomiejpluta.base.api.animation.Animation;
|
||||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.base.Layer;
|
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.Direction;
|
||||||
import com.bartlomiejpluta.base.api.move.Movement;
|
import com.bartlomiejpluta.base.api.move.Movement;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||||
@@ -147,7 +148,7 @@ public abstract class AnimationDelegate implements Animation {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Movement prepareMovement(Direction direction) {
|
public Movement prepareMovement(Direction direction) {
|
||||||
return new Movement(this, direction);
|
return new AnimationMovement(this, direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.bartlomiejpluta.base.api.camera.Camera;
|
|||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.api.move.Direction;
|
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.move.Movement;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||||
import com.bartlomiejpluta.base.internal.object.Placeable;
|
import com.bartlomiejpluta.base.internal.object.Placeable;
|
||||||
@@ -41,7 +42,7 @@ public abstract class EntityDelegate implements Entity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Movement prepareMovement(Direction direction) {
|
public Movement prepareMovement(Direction direction) {
|
||||||
return new Movement(this, direction);
|
return new EntityMovement(this, direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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.animation.Animation;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.base.Layer;
|
import com.bartlomiejpluta.base.api.map.layer.base.Layer;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
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.material.Material;
|
||||||
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
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
|
@Override
|
||||||
public void onAdd(Layer layer) {
|
public void onAdd(Layer layer) {
|
||||||
this.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.entity.Entity;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.api.move.Direction;
|
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.move.Movement;
|
||||||
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||||
import com.bartlomiejpluta.base.engine.world.entity.manager.EntitySetManager;
|
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));
|
material.setSpritePosition(spriteDefaultRows.get(faceDirection));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Movement prepareMovement(Direction direction) {
|
||||||
|
return new EntityMovement(this, direction);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean move(Movement movement) {
|
public boolean move(Movement movement) {
|
||||||
if (super.move(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.ai.NPC;
|
||||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
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.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.PassageAbility;
|
import com.bartlomiejpluta.base.api.map.layer.object.PassageAbility;
|
||||||
import com.bartlomiejpluta.base.api.map.model.GameMap;
|
import com.bartlomiejpluta.base.api.map.model.GameMap;
|
||||||
@@ -28,6 +29,8 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
|||||||
@Getter
|
@Getter
|
||||||
private final List<Entity> entities;
|
private final List<Entity> entities;
|
||||||
|
|
||||||
|
private final List<InteractiveEntity> interactiveEntities = new ArrayList<>();
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final PassageAbility[][] passageMap;
|
private final PassageAbility[][] passageMap;
|
||||||
|
|
||||||
@@ -131,35 +134,14 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(float dt) {
|
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
|
// Insert entities requested to be added
|
||||||
if(!entitiesToAdd.isEmpty()) {
|
if (!entitiesToAdd.isEmpty()) {
|
||||||
for (var entity : entitiesToAdd) {
|
for (var entity : entitiesToAdd) {
|
||||||
entity.onAdd(this);
|
entity.onAdd(this);
|
||||||
|
if (entity instanceof InteractiveEntity) {
|
||||||
|
interactiveEntities.add((InteractiveEntity) entity);
|
||||||
|
}
|
||||||
|
|
||||||
entities.add(entity);
|
entities.add(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +149,7 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Insert rules requested to be added
|
// Insert rules requested to be added
|
||||||
if(!movementRulesToAdd.isEmpty()) {
|
if (!movementRulesToAdd.isEmpty()) {
|
||||||
movementRules.addAll(movementRulesToAdd);
|
movementRules.addAll(movementRulesToAdd);
|
||||||
movementRulesToAdd.clear();
|
movementRulesToAdd.clear();
|
||||||
}
|
}
|
||||||
@@ -176,6 +158,10 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
|||||||
if (!entitiesToRemove.isEmpty()) {
|
if (!entitiesToRemove.isEmpty()) {
|
||||||
for (var entity : entitiesToRemove) {
|
for (var entity : entitiesToRemove) {
|
||||||
entities.remove(entity);
|
entities.remove(entity);
|
||||||
|
if (entity instanceof InteractiveEntity) {
|
||||||
|
interactiveEntities.remove(entity);
|
||||||
|
}
|
||||||
|
|
||||||
entity.onRemove(this);
|
entity.onRemove(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,6 +173,34 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
|||||||
movementRules.removeAll(movementRulesToRemove);
|
movementRules.removeAll(movementRulesToRemove);
|
||||||
movementRulesToRemove.clear();
|
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
|
@Override
|
||||||
@@ -204,4 +218,26 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
|||||||
var z = compare(a.getZIndex(), b.getZIndex());
|
var z = compare(a.getZIndex(), b.getZIndex());
|
||||||
return z == 0 ? compare(a.getPosition().y(), b.getPosition().y()) : z;
|
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;
|
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.Movable;
|
||||||
import com.bartlomiejpluta.base.api.move.Movement;
|
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.material.Material;
|
||||||
@@ -30,8 +29,6 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
|||||||
|
|
||||||
private enum PlacingMode {BY_POSITION, BY_COORDINATES}
|
private enum PlacingMode {BY_POSITION, BY_COORDINATES}
|
||||||
|
|
||||||
;
|
|
||||||
|
|
||||||
private PlacingMode placingMode;
|
private PlacingMode placingMode;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@@ -68,6 +65,7 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
|||||||
} else {
|
} else {
|
||||||
adjustCoordinates();
|
adjustCoordinates();
|
||||||
setDefaultAnimationFrame();
|
setDefaultAnimationFrame();
|
||||||
|
movement.onFinish();
|
||||||
movementVector = null;
|
movementVector = null;
|
||||||
movement = null;
|
movement = null;
|
||||||
}
|
}
|
||||||
@@ -80,11 +78,6 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
|||||||
setCoordinates(movement.getTo());
|
setCoordinates(movement.getTo());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Movement prepareMovement(Direction direction) {
|
|
||||||
return new Movement(this, direction);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean move(Movement movement) {
|
public boolean move(Movement movement) {
|
||||||
if (this.movement != null) {
|
if (this.movement != null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user