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.
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user