Implement ghost "eaten" behavior and refine fright mechanics

Added a new "eaten" mode for ghosts, including animations, movement strategy, and logic to reset ghosts after being eaten. Adjusted scoring for frightened ghosts using a fright multiplier and introduced enhancements like streamlined direction prioritization and position alignment. Also temporarily disabled non-essential ghosts for testing purposes.
This commit is contained in:
Urban Modig
2025-09-03 12:53:26 +02:00
parent d4b980f522
commit 11a550e997
5 changed files with 65 additions and 19 deletions

View File

@ -4,6 +4,7 @@ import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import se.urmo.game.collision.GhostCollisionChecker;
import se.urmo.game.entities.BaseAnimated;
import se.urmo.game.entities.ghost.strategy.EatenStrategy;
import se.urmo.game.entities.ghost.strategy.FearStrategy;
import se.urmo.game.entities.ghost.strategy.GhostStrategy;
import se.urmo.game.entities.pacman.PacMan;
@ -37,6 +38,8 @@ public class Ghost extends BaseAnimated {
private final BufferedImage[] fearAnimation;
private final BufferedImage[] baseAnimation;
private final LevelManager levelManager;
private final BufferedImage[] eatenAnimation;
private double speed;
private MyPoint position;
private final GhostStrategy scaterStrategy;
@ -50,6 +53,7 @@ public class Ghost extends BaseAnimated {
private boolean isBlinking = false;
@Setter
private boolean frozen;
private GhostStrategy eatenStrategy;
public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy strategy, GhostStrategy scaterStrategy, int animation, LevelManager levelManager) {
@ -59,6 +63,7 @@ public class Ghost extends BaseAnimated {
this.scaterStrategy = scaterStrategy;
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),
@ -67,6 +72,8 @@ public class Ghost extends BaseAnimated {
this.currentStrategy = chaseStrategy;
this.animation = baseAnimation;
this.levelManager = levelManager;
this.eatenStrategy = new EatenStrategy();
this.speed = BASE_SPEED * levelManager.getGhostSpeed();
}
public void draw(Graphics g) {
@ -83,6 +90,11 @@ public class Ghost extends BaseAnimated {
if (mode == GhostMode.FRIGHTENED) {
updateInFrightendMode();
}
if (mode == GhostMode.EATEN) {
if (position.asPoint().distance(startPos.asPoint()) < 10) {
setMode(GhostMode.CHASE);
}
}
updatePosition(pacman, map);
}
@ -98,11 +110,12 @@ public class Ghost extends BaseAnimated {
}
private void updatePosition(PacMan pacman, GameMap map) {
if (map.isAligned(new Point((int) position.x, (int) position.y))) {
if (map.isAligned(position.asPoint())) {
prevDirection = direction;
direction = chooseDirection(
prioritize(collisionChecker.calculateDirectionAlternatives(position)),
prioritizeDirections(collisionChecker.calculateDirectionAlternatives(position)),
currentStrategy.chooseTarget(this, pacman, map));
log.debug("Ghost moving to {}", direction);
}
moveTo(getNewPosition());
@ -122,7 +135,7 @@ public class Ghost extends BaseAnimated {
}
private double getSpeed() {
return BASE_SPEED * levelManager.getGhostSpeed();
return speed;
}
private void moveTo(MyPoint newPosition) {
@ -140,7 +153,7 @@ public class Ghost extends BaseAnimated {
* @param directions a list of potential movement directions
* @return a map where keys are valid directions and values are their associated priority
*/
private Map<Direction, Integer> prioritize(List<Direction> directions) {
private Map<Direction, Integer> prioritizeDirections(List<Direction> directions) {
return directions.stream()
.filter(d -> d != Direction.NONE)
.collect(Collectors.toMap(
@ -186,6 +199,7 @@ public class Ghost extends BaseAnimated {
best = d;
}
}
log.debug("Ghost comming from {}, choosing {}, from {} (dist={})", prevDirection, best, directions, bestDist);
return best;
}
@ -195,6 +209,7 @@ public class Ghost extends BaseAnimated {
case CHASE -> {
animation = baseAnimation;
currentStrategy = chaseStrategy;
speed = BASE_SPEED * levelManager.getGhostSpeed();
}
case SCATTER -> currentStrategy = scaterStrategy;
case FRIGHTENED -> {
@ -203,7 +218,11 @@ public class Ghost extends BaseAnimated {
animation = fearAnimation;
currentStrategy = fearStrategy;
}
case EATEN -> currentStrategy = null;
case EATEN -> {
//speed = 1.2;
currentStrategy = eatenStrategy;
animation = eatenAnimation;
}
}
}
@ -211,8 +230,9 @@ public class Ghost extends BaseAnimated {
return mode == GhostMode.FRIGHTENED;
}
public void resetPosition() {
public void reset() {
position = startPos;
this.speed = BASE_SPEED * levelManager.getGhostSpeed();
}
public Point getPosition() {
@ -223,4 +243,7 @@ public class Ghost extends BaseAnimated {
mode = GhostMode.CHASE;
}
public boolean isEaten() {
return mode == GhostMode.EATEN;
}
}