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

View File

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

View File

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