Skip to content

Commit

Permalink
Decoy entities suffer damage until destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
gpertzov committed Oct 2, 2024
1 parent 6fd7f20 commit 1df83d5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
8 changes: 6 additions & 2 deletions core/src/main/java/net/gpdev/darkly/GameLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<GameEntity> getEntitiesInRange(final GameEntity self, final Vector2 position, final float range) {
Expand Down
23 changes: 11 additions & 12 deletions core/src/main/java/net/gpdev/darkly/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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<GameEntity> entities = level.getEntities();
entities.forEach(entity -> actionQueue.addLast(entity.update(delta)));

// Handle action queue
executeEntityActions();
Expand Down Expand Up @@ -336,6 +328,7 @@ private boolean consumeGameEvent(final TriggeredEvent event) {
}

private void executeEntityActions() {
final Set<GameEntity> entitiesToBeRemoved = new HashSet<>();
final Iterator<EntityAction> actionIterator = actionQueue.iterator();
while (actionIterator.hasNext()) {
final EntityAction action = actionIterator.next();
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/net/gpdev/darkly/actions/Destroy.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public abstract class EntityAction {
public enum Type {
IDLE,
MOVE,
ATTACK
ATTACK,
DESTROY
}

private final Type type;
Expand Down
21 changes: 19 additions & 2 deletions core/src/main/java/net/gpdev/darkly/actors/DecoyEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
}
}

0 comments on commit 1df83d5

Please sign in to comment.