Skip to content

Commit

Permalink
[fight] Fix attract entity
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed Apr 21, 2023
1 parent acdd1e9 commit 40c77c2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/com/leekwars/generator/attack/Attack.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ public List<Entity> applyOnCell(Fight fight, Entity caster, Cell target, boolean
if (parameters.getId() == Effect.TYPE_ATTRACT) {
for (Entity entity : targetEntities) {
// Attract directly to target cell
fight.slideEntity(entity, target, caster);
Cell destination = Pathfinding.getAttractLastAvailableCell(entity.getCell(), target, caster.getCell());
fight.slideEntity(entity, destination, caster);
}
} else if (parameters.getId() == Effect.TYPE_PUSH) {
for (Entity entity : targetEntities) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public abstract class Entity {
public static final int TYPE_BULB = 1;
public static final int TYPE_TURRET = 2;
public static final int TYPE_CHEST = 3;
public static final int TYPE_MOB = 4;

// Characteristics constants
public final static int CHARAC_LIFE = 0;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/leekwars/generator/maps/Pathfinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,24 @@ public static Cell getPushLastAvailableCell(Cell entity, Cell target, Cell caste
}
return current;
}

public static Cell getAttractLastAvailableCell(Cell entity, Cell target, Cell caster) {
// Delta caster --> entity
int cdx = (int) Math.signum(entity.x - caster.x);
int cdy = (int) Math.signum(entity.y - caster.y);
// Delta entity --> target
int dx = (int) Math.signum(target.x - entity.x);
int dy = (int) Math.signum(target.y - entity.y);
// Check deltas (must be attracted in the correct direction)
if (cdx != -dx || cdy != -dy) return entity; // no change
Cell current = entity;
while (current != target) {
Cell next = current.next(dx, dy);
if (!next.available()) {
return current;
}
current = next;
}
return current;
}
}

0 comments on commit 40c77c2

Please sign in to comment.