From 80f7897da600e4d02caeb2547b92ae7d06df49d1 Mon Sep 17 00:00:00 2001 From: Urban Modig Date: Sat, 6 Sep 2025 20:16:13 +0200 Subject: [PATCH] Fixed issue with freezing ghosts --- src/main/java/se/urmo/game/main/GhostManager.java | 13 +++++++++---- src/main/java/se/urmo/game/state/PlayingState.java | 7 ++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/se/urmo/game/main/GhostManager.java b/src/main/java/se/urmo/game/main/GhostManager.java index a7f60b3..2e9cd0c 100644 --- a/src/main/java/se/urmo/game/main/GhostManager.java +++ b/src/main/java/se/urmo/game/main/GhostManager.java @@ -6,6 +6,12 @@ import se.urmo.game.collision.GhostCollisionChecker; import se.urmo.game.entities.ghost.Ghost; import se.urmo.game.entities.ghost.mode.GhostMode; import se.urmo.game.entities.ghost.strategy.BlinkyStrategy; +import se.urmo.game.entities.ghost.strategy.ClydeStrategy; +import se.urmo.game.entities.ghost.strategy.InkyStrategy; +import se.urmo.game.entities.ghost.strategy.PinkyStrategy; +import se.urmo.game.entities.ghost.strategy.ScatterToBottomLeft; +import se.urmo.game.entities.ghost.strategy.ScatterToBottomRight; +import se.urmo.game.entities.ghost.strategy.ScatterToTopLeft; import se.urmo.game.entities.ghost.strategy.ScatterToTopRight; import se.urmo.game.entities.pacman.PacMan; import se.urmo.game.map.GameMap; @@ -42,9 +48,9 @@ public class GhostManager { // Create ghosts with their strategies Ghost blinky = new Ghost(ghostCollisionChecker, new BlinkyStrategy(), new ScatterToTopRight(), BLINKY_ANIMATION, levelManager); ghosts.add(blinky); -// ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(), new ScatterToTopLeft(), PINKY_ANIMATION, levelManager)); -// ghosts.add(new Ghost(ghostCollisionChecker,new InkyStrategy(blinky), new ScatterToBottomRight(), INKY_ANIMATION, levelManager)); -// ghosts.add(new Ghost(ghostCollisionChecker, new ClydeStrategy(), new ScatterToBottomLeft(), CLYDE_ANIMATION, levelManager)); + ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(), new ScatterToTopLeft(), PINKY_ANIMATION, levelManager)); + ghosts.add(new Ghost(ghostCollisionChecker, new InkyStrategy(blinky), new ScatterToBottomRight(), INKY_ANIMATION, levelManager)); + ghosts.add(new Ghost(ghostCollisionChecker, new ClydeStrategy(), new ScatterToBottomLeft(), CLYDE_ANIMATION, levelManager)); ghosts.forEach(animationManager::register); @@ -58,7 +64,6 @@ public class GhostManager { } public void update(PacMan pacman, GameMap map) { - long now = System.currentTimeMillis(); if (phaseIndex < cycleDurations.length) { int duration = cycleDurations[phaseIndex]; diff --git a/src/main/java/se/urmo/game/state/PlayingState.java b/src/main/java/se/urmo/game/state/PlayingState.java index ee5a665..e469d6b 100644 --- a/src/main/java/se/urmo/game/state/PlayingState.java +++ b/src/main/java/se/urmo/game/state/PlayingState.java @@ -72,6 +72,7 @@ public class PlayingState implements GameState { case READY -> { // Freeze everything during READY if (phaseElapsed() >= READY_MS) { + ghostManager.setMode(GhostMode.CHASE); setPhase(RoundPhase.PLAYING); } } @@ -95,7 +96,7 @@ public class PlayingState implements GameState { if (phaseElapsed() >= LIFE_LOST_MS) { pacman.setState(PacMan.PacmanState.ALIVE); deathInProgress = false; - ghostManager.setFrozen(false); + ghostManager.getGhosts().forEach(ghost -> ghost.requestModeChange(GhostMode.CHASE)); setPhase(RoundPhase.READY); if (lives <= 0) { endGame(); @@ -215,11 +216,11 @@ public class PlayingState implements GameState { if (dist < GameMap.MAP_TILESIZE / 2.0) { if (ghost.isEaten()) return; if (ghost.isFrightened()) { - // Pac-Man eats ghost + log.debug("Pacman eats ghost"); score += 200 * (1 << (ghostManager.getGhosts().size() - ghostManager.isFrightened())); - ghost.setMode(GhostMode.EATEN); } else { + log.debug("Pacman loses a life"); ghostManager.setFrozen(true); pacman.setState(PacMan.PacmanState.DYING); deathInProgress = true;