From b317581e9c34ad099565b3482abaa4e5fc810e19 Mon Sep 17 00:00:00 2001 From: Urban Modig Date: Wed, 3 Sep 2025 20:21:06 +0200 Subject: [PATCH] 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. --- .../se/urmo/game/entities/ghost/Ghost.java | 59 +++++++++---------- .../urmo/game/entities/ghost/GhostMode.java | 3 +- .../java/se/urmo/game/main/GhostManager.java | 2 +- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/main/java/se/urmo/game/entities/ghost/Ghost.java b/src/main/java/se/urmo/game/entities/ghost/Ghost.java index cb16644..6d9e874 100644 --- a/src/main/java/se/urmo/game/entities/ghost/Ghost.java +++ b/src/main/java/se/urmo/game/entities/ghost/Ghost.java @@ -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,16 +81,21 @@ public class Ghost extends BaseAnimated { } public void update(PacMan pacman, GameMap map) { - if (frozen) return; - if (mode == GhostMode.FRIGHTENED) { - updateInFrightendMode(); - } - if (mode == GhostMode.EATEN) { - if (position.asPoint().distance(startPos.asPoint()) < 10) { - setMode(GhostMode.CHASE); + switch (mode) { + case FRIGHTENED -> { + updateInFrightendMode(); + updatePosition(pacman, map); } + 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() { @@ -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; } diff --git a/src/main/java/se/urmo/game/entities/ghost/GhostMode.java b/src/main/java/se/urmo/game/entities/ghost/GhostMode.java index 64dabb0..8703f33 100644 --- a/src/main/java/se/urmo/game/entities/ghost/GhostMode.java +++ b/src/main/java/se/urmo/game/entities/ghost/GhostMode.java @@ -4,5 +4,6 @@ public enum GhostMode { CHASE, SCATTER, FRIGHTENED, - EATEN + EATEN, + FROZEN } diff --git a/src/main/java/se/urmo/game/main/GhostManager.java b/src/main/java/se/urmo/game/main/GhostManager.java index 92c82a9..c9f9cc0 100644 --- a/src/main/java/se/urmo/game/main/GhostManager.java +++ b/src/main/java/se/urmo/game/main/GhostManager.java @@ -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() {