From 1df83d514bc6f4c74ec17cabb155e12356bcaa54 Mon Sep 17 00:00:00 2001 From: gpertzov Date: Wed, 2 Oct 2024 13:25:13 +0300 Subject: [PATCH] Decoy entities suffer damage until destroyed --- .../main/java/net/gpdev/darkly/GameLevel.java | 8 +++++-- .../java/net/gpdev/darkly/GameScreen.java | 23 +++++++++---------- .../net/gpdev/darkly/actions/Destroy.java | 9 ++++++++ .../gpdev/darkly/actions/EntityAction.java | 3 ++- .../net/gpdev/darkly/actors/DecoyEntity.java | 21 +++++++++++++++-- 5 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 core/src/main/java/net/gpdev/darkly/actions/Destroy.java diff --git a/core/src/main/java/net/gpdev/darkly/GameLevel.java b/core/src/main/java/net/gpdev/darkly/GameLevel.java index a8dd4e3..bee52b2 100644 --- a/core/src/main/java/net/gpdev/darkly/GameLevel.java +++ b/core/src/main/java/net/gpdev/darkly/GameLevel.java @@ -83,8 +83,12 @@ public Vector2 getPosition(final String positionId) { return position.scl(unitScale); } - public void addEntity(final GameEntity entity) { - entities.add(entity); + public boolean addEntity(final GameEntity entity) { + return entities.add(entity); + } + + public boolean removeEntity(final GameEntity entity) { + return entities.remove(entity); } public List getEntitiesInRange(final GameEntity self, final Vector2 position, final float range) { diff --git a/core/src/main/java/net/gpdev/darkly/GameScreen.java b/core/src/main/java/net/gpdev/darkly/GameScreen.java index 30a3539..b135280 100644 --- a/core/src/main/java/net/gpdev/darkly/GameScreen.java +++ b/core/src/main/java/net/gpdev/darkly/GameScreen.java @@ -25,10 +25,8 @@ import net.gpdev.darkly.actors.GameEntity; import net.gpdev.darkly.actors.PlayerEntity; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; import java.util.List; +import java.util.*; import static com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion; import static net.gpdev.darkly.DarklyGame.FLASHLIGHT; @@ -245,7 +243,6 @@ public void render(final float delta) { mapRenderer.render(); // Render entities - final Vector2 enemyPosition = enemy.getPosition(); final float attackTime = enemy.getAttackTime(); batch.begin(); @@ -256,7 +253,7 @@ public void render(final float delta) { batch.draw(entity.getSprite(), position.x, position.y, 1, 1); }); - GameEntity target = null; + GameEntity target; if (attackTime > 0 && !attackAnim.isAnimationFinished(attackTime) && (target = enemy.getCurrentTarget()) != null) { final TextureRegion attackFrame = attackAnim.getKeyFrame(attackTime, false); batch.draw(attackFrame, target.getPosition().x, target.getPosition().y, 1, 1); @@ -299,13 +296,8 @@ public void render(final float delta) { } private void update(final float delta) { - // Update player entity - final EntityAction playerAction = player.update(delta); - actionQueue.addLast(playerAction); - - // Update enemy entity - final EntityAction enemyAction = enemy.update(delta); - actionQueue.addLast(enemyAction); + final List entities = level.getEntities(); + entities.forEach(entity -> actionQueue.addLast(entity.update(delta))); // Handle action queue executeEntityActions(); @@ -336,6 +328,7 @@ private boolean consumeGameEvent(final TriggeredEvent event) { } private void executeEntityActions() { + final Set entitiesToBeRemoved = new HashSet<>(); final Iterator actionIterator = actionQueue.iterator(); while (actionIterator.hasNext()) { final EntityAction action = actionIterator.next(); @@ -367,12 +360,18 @@ private void executeEntityActions() { break; case IDLE: { + } + break; + case DESTROY: { + entitiesToBeRemoved.add(action.getSource()); } break; default: throw new RuntimeException("Invalid entity action: " + action.getType()); } } + + entitiesToBeRemoved.forEach(entity -> level.removeEntity(entity)); } private void endGame(final String message) { diff --git a/core/src/main/java/net/gpdev/darkly/actions/Destroy.java b/core/src/main/java/net/gpdev/darkly/actions/Destroy.java new file mode 100644 index 0000000..0db45bb --- /dev/null +++ b/core/src/main/java/net/gpdev/darkly/actions/Destroy.java @@ -0,0 +1,9 @@ +package net.gpdev.darkly.actions; + +import net.gpdev.darkly.actors.GameEntity; + +public class Destroy extends EntityAction { + public Destroy(final GameEntity source) { + super(Type.DESTROY, source, null); + } +} diff --git a/core/src/main/java/net/gpdev/darkly/actions/EntityAction.java b/core/src/main/java/net/gpdev/darkly/actions/EntityAction.java index 4c8479c..3efbbdb 100644 --- a/core/src/main/java/net/gpdev/darkly/actions/EntityAction.java +++ b/core/src/main/java/net/gpdev/darkly/actions/EntityAction.java @@ -6,7 +6,8 @@ public abstract class EntityAction { public enum Type { IDLE, MOVE, - ATTACK + ATTACK, + DESTROY } private final Type type; diff --git a/core/src/main/java/net/gpdev/darkly/actors/DecoyEntity.java b/core/src/main/java/net/gpdev/darkly/actors/DecoyEntity.java index 0677025..bd76566 100644 --- a/core/src/main/java/net/gpdev/darkly/actors/DecoyEntity.java +++ b/core/src/main/java/net/gpdev/darkly/actors/DecoyEntity.java @@ -5,12 +5,16 @@ import com.badlogic.gdx.math.Vector2; import net.gpdev.darkly.Light; import net.gpdev.darkly.TriggeredEvent; +import net.gpdev.darkly.actions.Destroy; import net.gpdev.darkly.actions.EntityAction; import static net.gpdev.darkly.Light.LIGHT_DEFAULT_INTENSITY; +import static net.gpdev.darkly.actions.Idle.IDLE_ACTION; public class DecoyEntity extends GameEntity { + private float health = 0.3f; + public DecoyEntity(final Sprite sprite, final Vector2 position, final float speed, @@ -24,13 +28,26 @@ public DecoyEntity(final Sprite sprite, @Override public EntityAction update(final float delta) { - return super.update(delta); + super.update(delta); + + if (health <= 0) { + return new Destroy(this); + } + // TODO: Decay light intensity over time + + return IDLE_ACTION; } @Override public void reactTo(final TriggeredEvent event) { super.reactTo(event); - // TODO: Handle attack damage + + final float amount = event.getAmount() / 100f; + final TriggeredEvent.Type eventType = event.getType(); + + if (TriggeredEvent.Type.HARM.equals(eventType)) { + health -= amount; + } } }