Refactor Pacman state handling to use setState method.

Centralized Pacman state transitions into a new `setState` method to improve clarity and consistency. Removed redundant `reset` and `startDeathAnimation` methods, merging their logic into state management. Adjusted game state transitions to align with the new approach.
This commit is contained in:
Urban Modig
2025-09-03 20:58:07 +02:00
parent 2f9106c1c9
commit 6dabc63892
2 changed files with 24 additions and 19 deletions

View File

@ -47,6 +47,7 @@ public class PacMan extends BaseAnimated {
@Setter @Setter
private PacmanState state = PacmanState.ALIVE; private PacmanState state = PacmanState.ALIVE;
private int deathFrameIdx = 0; private int deathFrameIdx = 0;
private double speed;
public PacMan(CollisionChecker collisionChecker, LevelManager levelManager) { public PacMan(CollisionChecker collisionChecker, LevelManager levelManager) {
super(ANIMATION_UPDATE_FREQUENCY, PACMAN_SPRITE_FRAMES); super(ANIMATION_UPDATE_FREQUENCY, PACMAN_SPRITE_FRAMES);
@ -57,6 +58,7 @@ public class PacMan extends BaseAnimated {
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + ((double) GameMap.MAP_TILESIZE / 2)); 13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + ((double) GameMap.MAP_TILESIZE / 2));
this.startPosition = this.position; this.startPosition = this.position;
this.sprites = loadAnimation(); this.sprites = loadAnimation();
this.speed = BASE_SPEED * levelManager.getPacmanLevelSpeed();
} }
private Sprites loadAnimation() { private Sprites loadAnimation() {
@ -144,28 +146,31 @@ public class PacMan extends BaseAnimated {
return new MyPoint(position.x + direction.dx * getSpeed(), position.y + direction.dy * getSpeed()); return new MyPoint(position.x + direction.dx * getSpeed(), position.y + direction.dy * getSpeed());
} }
public void startDeathAnimation() {
state = PacmanState.DYING;
deathFrameIdx = 0;
lastChangeNs = System.nanoTime(); // reset stopwatch right now
deathFrames = Arrays.stream(sprites.deathFrames)
.map(img -> LoadSave.rotate(img, direction.angel))
.toArray(BufferedImage[]::new);
}
private double getSpeed() { private double getSpeed() {
return BASE_SPEED * levelManager.getPacmanLevelSpeed(); return this.speed;
} }
public double distanceTo(Point point) { public double distanceTo(Point point) {
return new Point((int) position.x, (int) position.y).distance(point); return new Point((int) position.x, (int) position.y).distance(point);
} }
public void reset() { public void setState(PacmanState state) {
position = startPosition; this.state = state;
aniIndex = 0; // reset animation to start switch (state) {
state = PacmanState.ALIVE; case ALIVE -> {
deathFrameIdx = 0; position = startPosition;
aniIndex = 0; // reset animation to start
deathFrameIdx = 0;
speed = BASE_SPEED * levelManager.getPacmanLevelSpeed(); // Recalculate
}
case DYING -> {
deathFrameIdx = 0;
lastChangeNs = System.nanoTime(); // reset stopwatch right now
deathFrames = Arrays.stream(sprites.deathFrames)
.map(img -> LoadSave.rotate(img, direction.angel))
.toArray(BufferedImage[]::new);
}
}
} }
public Image getLifeIcon() { public Image getLifeIcon() {
@ -189,7 +194,7 @@ public class PacMan extends BaseAnimated {
paused = !b; paused = !b;
} }
private enum PacmanState { public enum PacmanState {
ALIVE, DYING, DEAD ALIVE, DYING, DEAD
} }

View File

@ -93,7 +93,7 @@ public class PlayingState implements GameState {
case LIFE_LOST -> { case LIFE_LOST -> {
// Freeze, then reset round (keep dot state) // Freeze, then reset round (keep dot state)
if (phaseElapsed() >= LIFE_LOST_MS) { if (phaseElapsed() >= LIFE_LOST_MS) {
pacman.reset(); pacman.setState(PacMan.PacmanState.ALIVE);
deathInProgress = false; deathInProgress = false;
ghostManager.setFrozen(false); ghostManager.setFrozen(false);
setPhase(RoundPhase.READY); setPhase(RoundPhase.READY);
@ -111,7 +111,7 @@ public class PlayingState implements GameState {
map.reset(); map.reset();
ghostManager.reset(); ghostManager.reset();
fruitManager.reset(); fruitManager.reset();
pacman.reset(); pacman.setState(PacMan.PacmanState.ALIVE);
dotsEaten = 0; dotsEaten = 0;
} }
@ -221,7 +221,7 @@ public class PlayingState implements GameState {
ghost.setMode(GhostMode.EATEN); ghost.setMode(GhostMode.EATEN);
} else { } else {
ghostManager.setFrozen(true); ghostManager.setFrozen(true);
pacman.startDeathAnimation(); pacman.setState(PacMan.PacmanState.DYING);
deathInProgress = true; deathInProgress = true;
// Pac-Man loses a life // Pac-Man loses a life
lives--; lives--;