Fixed issue with freezing ghosts

This commit is contained in:
Urban Modig
2025-09-06 20:16:13 +02:00
parent c1c998c1cd
commit 80f7897da6
2 changed files with 13 additions and 7 deletions

View File

@ -6,6 +6,12 @@ import se.urmo.game.collision.GhostCollisionChecker;
import se.urmo.game.entities.ghost.Ghost; import se.urmo.game.entities.ghost.Ghost;
import se.urmo.game.entities.ghost.mode.GhostMode; import se.urmo.game.entities.ghost.mode.GhostMode;
import se.urmo.game.entities.ghost.strategy.BlinkyStrategy; 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.ghost.strategy.ScatterToTopRight;
import se.urmo.game.entities.pacman.PacMan; import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.map.GameMap; import se.urmo.game.map.GameMap;
@ -42,9 +48,9 @@ public class GhostManager {
// Create ghosts with their strategies // Create ghosts with their strategies
Ghost blinky = new Ghost(ghostCollisionChecker, new BlinkyStrategy(), new ScatterToTopRight(), BLINKY_ANIMATION, levelManager); Ghost blinky = new Ghost(ghostCollisionChecker, new BlinkyStrategy(), new ScatterToTopRight(), BLINKY_ANIMATION, levelManager);
ghosts.add(blinky); ghosts.add(blinky);
// ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(), new ScatterToTopLeft(), PINKY_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 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 ClydeStrategy(), new ScatterToBottomLeft(), CLYDE_ANIMATION, levelManager));
ghosts.forEach(animationManager::register); ghosts.forEach(animationManager::register);
@ -58,7 +64,6 @@ public class GhostManager {
} }
public void update(PacMan pacman, GameMap map) { public void update(PacMan pacman, GameMap map) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if (phaseIndex < cycleDurations.length) { if (phaseIndex < cycleDurations.length) {
int duration = cycleDurations[phaseIndex]; int duration = cycleDurations[phaseIndex];

View File

@ -72,6 +72,7 @@ public class PlayingState implements GameState {
case READY -> { case READY -> {
// Freeze everything during READY // Freeze everything during READY
if (phaseElapsed() >= READY_MS) { if (phaseElapsed() >= READY_MS) {
ghostManager.setMode(GhostMode.CHASE);
setPhase(RoundPhase.PLAYING); setPhase(RoundPhase.PLAYING);
} }
} }
@ -95,7 +96,7 @@ public class PlayingState implements GameState {
if (phaseElapsed() >= LIFE_LOST_MS) { if (phaseElapsed() >= LIFE_LOST_MS) {
pacman.setState(PacMan.PacmanState.ALIVE); pacman.setState(PacMan.PacmanState.ALIVE);
deathInProgress = false; deathInProgress = false;
ghostManager.setFrozen(false); ghostManager.getGhosts().forEach(ghost -> ghost.requestModeChange(GhostMode.CHASE));
setPhase(RoundPhase.READY); setPhase(RoundPhase.READY);
if (lives <= 0) { if (lives <= 0) {
endGame(); endGame();
@ -215,11 +216,11 @@ public class PlayingState implements GameState {
if (dist < GameMap.MAP_TILESIZE / 2.0) { if (dist < GameMap.MAP_TILESIZE / 2.0) {
if (ghost.isEaten()) return; if (ghost.isEaten()) return;
if (ghost.isFrightened()) { if (ghost.isFrightened()) {
// Pac-Man eats ghost log.debug("Pacman eats ghost");
score += 200 * (1 << (ghostManager.getGhosts().size() - ghostManager.isFrightened())); score += 200 * (1 << (ghostManager.getGhosts().size() - ghostManager.isFrightened()));
ghost.setMode(GhostMode.EATEN); ghost.setMode(GhostMode.EATEN);
} else { } else {
log.debug("Pacman loses a life");
ghostManager.setFrozen(true); ghostManager.setFrozen(true);
pacman.setState(PacMan.PacmanState.DYING); pacman.setState(PacMan.PacmanState.DYING);
deathInProgress = true; deathInProgress = true;