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:
@ -47,6 +47,7 @@ public class PacMan extends BaseAnimated {
|
||||
@Setter
|
||||
private PacmanState state = PacmanState.ALIVE;
|
||||
private int deathFrameIdx = 0;
|
||||
private double speed;
|
||||
|
||||
public PacMan(CollisionChecker collisionChecker, LevelManager levelManager) {
|
||||
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));
|
||||
this.startPosition = this.position;
|
||||
this.sprites = loadAnimation();
|
||||
this.speed = BASE_SPEED * levelManager.getPacmanLevelSpeed();
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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() {
|
||||
return BASE_SPEED * levelManager.getPacmanLevelSpeed();
|
||||
return this.speed;
|
||||
}
|
||||
|
||||
public double distanceTo(Point point) {
|
||||
return new Point((int) position.x, (int) position.y).distance(point);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
public void setState(PacmanState state) {
|
||||
this.state = state;
|
||||
switch (state) {
|
||||
case ALIVE -> {
|
||||
position = startPosition;
|
||||
aniIndex = 0; // reset animation to start
|
||||
state = PacmanState.ALIVE;
|
||||
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() {
|
||||
@ -189,7 +194,7 @@ public class PacMan extends BaseAnimated {
|
||||
paused = !b;
|
||||
}
|
||||
|
||||
private enum PacmanState {
|
||||
public enum PacmanState {
|
||||
ALIVE, DYING, DEAD
|
||||
}
|
||||
|
||||
|
||||
@ -93,7 +93,7 @@ public class PlayingState implements GameState {
|
||||
case LIFE_LOST -> {
|
||||
// Freeze, then reset round (keep dot state)
|
||||
if (phaseElapsed() >= LIFE_LOST_MS) {
|
||||
pacman.reset();
|
||||
pacman.setState(PacMan.PacmanState.ALIVE);
|
||||
deathInProgress = false;
|
||||
ghostManager.setFrozen(false);
|
||||
setPhase(RoundPhase.READY);
|
||||
@ -111,7 +111,7 @@ public class PlayingState implements GameState {
|
||||
map.reset();
|
||||
ghostManager.reset();
|
||||
fruitManager.reset();
|
||||
pacman.reset();
|
||||
pacman.setState(PacMan.PacmanState.ALIVE);
|
||||
dotsEaten = 0;
|
||||
}
|
||||
|
||||
@ -221,7 +221,7 @@ public class PlayingState implements GameState {
|
||||
ghost.setMode(GhostMode.EATEN);
|
||||
} else {
|
||||
ghostManager.setFrozen(true);
|
||||
pacman.startDeathAnimation();
|
||||
pacman.setState(PacMan.PacmanState.DYING);
|
||||
deathInProgress = true;
|
||||
// Pac-Man loses a life
|
||||
lives--;
|
||||
|
||||
Reference in New Issue
Block a user