Add "FROZEN" mode to Ghost behavior and update logic

Introduced a new "FROZEN" mode to the Ghost state machine, ensuring proper handling in ghost behaviors and animations. Updated `GhostManager` and `Ghost` classes to integrate this mode, including relevant logic for updates and animations. Simplified and refined ghost initialization and strategy handling for better code maintainability.
This commit is contained in:
Urban Modig
2025-09-03 20:21:06 +02:00
parent 11a550e997
commit b317581e9c
3 changed files with 31 additions and 33 deletions

View File

@ -1,6 +1,5 @@
package se.urmo.game.entities.ghost; package se.urmo.game.entities.ghost;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import se.urmo.game.collision.GhostCollisionChecker; import se.urmo.game.collision.GhostCollisionChecker;
import se.urmo.game.entities.BaseAnimated; import se.urmo.game.entities.BaseAnimated;
@ -35,10 +34,10 @@ public class Ghost extends BaseAnimated {
private final GhostCollisionChecker collisionChecker; private final GhostCollisionChecker collisionChecker;
private final GhostStrategy chaseStrategy; private final GhostStrategy chaseStrategy;
private final MyPoint startPos; private final MyPoint startPos;
private final BufferedImage[] fearAnimation; private static final BufferedImage[] fearAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(8);
private final BufferedImage[] baseAnimation; private final BufferedImage[] baseAnimation;
private final LevelManager levelManager; private final LevelManager levelManager;
private final BufferedImage[] eatenAnimation; private static final BufferedImage[] eatenAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(9);
private double speed; private double speed;
private MyPoint position; private MyPoint position;
@ -51,28 +50,24 @@ public class Ghost extends BaseAnimated {
private final GhostStrategy fearStrategy = new FearStrategy(); private final GhostStrategy fearStrategy = new FearStrategy();
private int frightenedTimer = 0; private int frightenedTimer = 0;
private boolean isBlinking = false; private boolean isBlinking = false;
@Setter private static final GhostStrategy eatenStrategy = new EatenStrategy();
private boolean frozen;
private GhostStrategy eatenStrategy;
public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy strategy, GhostStrategy scaterStrategy, int animation, LevelManager levelManager) { public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy chaseStrategy, GhostStrategy scaterStrategy, int animation, LevelManager levelManager) {
super(ANIMATION_UPDATE_FREQUENCY, GhostManager.MAX_SPRITE_FRAMES); super(ANIMATION_UPDATE_FREQUENCY, GhostManager.MAX_SPRITE_FRAMES);
this.collisionChecker = collisionChecker; this.collisionChecker = collisionChecker;
this.chaseStrategy = strategy; this.chaseStrategy = chaseStrategy;
this.scaterStrategy = scaterStrategy; this.scaterStrategy = scaterStrategy;
this.currentStrategy = this.chaseStrategy;
this.baseAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(animation); this.baseAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(animation);
this.fearAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(8);
this.eatenAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(9);
position = new MyPoint( position = new MyPoint(
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X + ((double) GameMap.MAP_TILESIZE / 2), GameMap.colToScreen(13) + ((double) GameMap.MAP_TILESIZE / 2),
4 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + ((double) GameMap.MAP_TILESIZE / 2)); GameMap.rowToScreen(4) + ((double) GameMap.MAP_TILESIZE / 2));
startPos = position; startPos = position;
this.currentStrategy = chaseStrategy;
this.animation = baseAnimation; this.animation = baseAnimation;
this.levelManager = levelManager; this.levelManager = levelManager;
this.eatenStrategy = new EatenStrategy();
this.speed = BASE_SPEED * levelManager.getGhostSpeed(); this.speed = BASE_SPEED * levelManager.getGhostSpeed();
} }
@ -86,16 +81,21 @@ public class Ghost extends BaseAnimated {
} }
public void update(PacMan pacman, GameMap map) { public void update(PacMan pacman, GameMap map) {
if (frozen) return; switch (mode) {
if (mode == GhostMode.FRIGHTENED) { case FRIGHTENED -> {
updateInFrightendMode(); updateInFrightendMode();
} updatePosition(pacman, map);
if (mode == GhostMode.EATEN) {
if (position.asPoint().distance(startPos.asPoint()) < 10) {
setMode(GhostMode.CHASE);
} }
case EATEN -> {
if (position.asPoint().distance(startPos.asPoint()) < 10) {
setMode(GhostMode.CHASE);
}
updatePosition(pacman, map);
}
case FROZEN -> {
}
default -> updatePosition(pacman, map);
} }
updatePosition(pacman, map);
} }
private void updateInFrightendMode() { private void updateInFrightendMode() {
@ -134,10 +134,6 @@ public class Ghost extends BaseAnimated {
} }
private double getSpeed() {
return speed;
}
private void moveTo(MyPoint newPosition) { private void moveTo(MyPoint newPosition) {
MyPoint destination = collisionChecker.canMoveTo(direction, newPosition.x, newPosition.y); MyPoint destination = collisionChecker.canMoveTo(direction, newPosition.x, newPosition.y);
if (destination != null) { if (destination != null) {
@ -219,13 +215,18 @@ public class Ghost extends BaseAnimated {
currentStrategy = fearStrategy; currentStrategy = fearStrategy;
} }
case EATEN -> { case EATEN -> {
//speed = 1.2; speed = 1.0;
currentStrategy = eatenStrategy; currentStrategy = eatenStrategy;
animation = eatenAnimation; animation = eatenAnimation;
} }
case FROZEN -> {/* Do nothing */}
} }
} }
private double getSpeed() {
return this.speed;
}
public boolean isFrightened() { public boolean isFrightened() {
return mode == GhostMode.FRIGHTENED; return mode == GhostMode.FRIGHTENED;
} }
@ -239,10 +240,6 @@ public class Ghost extends BaseAnimated {
return new Point((int) position.x, (int) position.y); return new Point((int) position.x, (int) position.y);
} }
public void resetModes() {
mode = GhostMode.CHASE;
}
public boolean isEaten() { public boolean isEaten() {
return mode == GhostMode.EATEN; return mode == GhostMode.EATEN;
} }

View File

@ -4,5 +4,6 @@ public enum GhostMode {
CHASE, CHASE,
SCATTER, SCATTER,
FRIGHTENED, FRIGHTENED,
EATEN EATEN,
FROZEN
} }

View File

@ -95,7 +95,7 @@ public class GhostManager {
} }
public void setFrozen(boolean frozen) { public void setFrozen(boolean frozen) {
this.ghosts.forEach(ghost -> ghost.setFrozen(frozen)); this.ghosts.forEach(ghost -> ghost.setMode(GhostMode.FROZEN));
} }
public int isFrightened() { public int isFrightened() {