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:
@ -4,6 +4,7 @@ import lombok.Setter;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import se.urmo.game.collision.GhostCollisionChecker;
|
import se.urmo.game.collision.GhostCollisionChecker;
|
||||||
import se.urmo.game.entities.BaseAnimated;
|
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.FearStrategy;
|
||||||
import se.urmo.game.entities.ghost.strategy.GhostStrategy;
|
import se.urmo.game.entities.ghost.strategy.GhostStrategy;
|
||||||
import se.urmo.game.entities.pacman.PacMan;
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
@ -37,6 +38,8 @@ public class Ghost extends BaseAnimated {
|
|||||||
private final BufferedImage[] fearAnimation;
|
private final BufferedImage[] fearAnimation;
|
||||||
private final BufferedImage[] baseAnimation;
|
private final BufferedImage[] baseAnimation;
|
||||||
private final LevelManager levelManager;
|
private final LevelManager levelManager;
|
||||||
|
private final BufferedImage[] eatenAnimation;
|
||||||
|
private double speed;
|
||||||
private MyPoint position;
|
private MyPoint position;
|
||||||
|
|
||||||
private final GhostStrategy scaterStrategy;
|
private final GhostStrategy scaterStrategy;
|
||||||
@ -50,6 +53,7 @@ public class Ghost extends BaseAnimated {
|
|||||||
private boolean isBlinking = false;
|
private boolean isBlinking = false;
|
||||||
@Setter
|
@Setter
|
||||||
private boolean frozen;
|
private boolean frozen;
|
||||||
|
private GhostStrategy eatenStrategy;
|
||||||
|
|
||||||
|
|
||||||
public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy strategy, GhostStrategy scaterStrategy, int animation, LevelManager levelManager) {
|
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.scaterStrategy = scaterStrategy;
|
||||||
this.baseAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(animation);
|
this.baseAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(animation);
|
||||||
this.fearAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(8);
|
this.fearAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(8);
|
||||||
|
this.eatenAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(9);
|
||||||
|
|
||||||
position = new MyPoint(
|
position = new MyPoint(
|
||||||
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X + ((double) GameMap.MAP_TILESIZE / 2),
|
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.currentStrategy = chaseStrategy;
|
||||||
this.animation = baseAnimation;
|
this.animation = baseAnimation;
|
||||||
this.levelManager = levelManager;
|
this.levelManager = levelManager;
|
||||||
|
this.eatenStrategy = new EatenStrategy();
|
||||||
|
this.speed = BASE_SPEED * levelManager.getGhostSpeed();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(Graphics g) {
|
public void draw(Graphics g) {
|
||||||
@ -83,6 +90,11 @@ public class Ghost extends BaseAnimated {
|
|||||||
if (mode == GhostMode.FRIGHTENED) {
|
if (mode == GhostMode.FRIGHTENED) {
|
||||||
updateInFrightendMode();
|
updateInFrightendMode();
|
||||||
}
|
}
|
||||||
|
if (mode == GhostMode.EATEN) {
|
||||||
|
if (position.asPoint().distance(startPos.asPoint()) < 10) {
|
||||||
|
setMode(GhostMode.CHASE);
|
||||||
|
}
|
||||||
|
}
|
||||||
updatePosition(pacman, map);
|
updatePosition(pacman, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,11 +110,12 @@ public class Ghost extends BaseAnimated {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updatePosition(PacMan pacman, GameMap map) {
|
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;
|
prevDirection = direction;
|
||||||
direction = chooseDirection(
|
direction = chooseDirection(
|
||||||
prioritize(collisionChecker.calculateDirectionAlternatives(position)),
|
prioritizeDirections(collisionChecker.calculateDirectionAlternatives(position)),
|
||||||
currentStrategy.chooseTarget(this, pacman, map));
|
currentStrategy.chooseTarget(this, pacman, map));
|
||||||
|
log.debug("Ghost moving to {}", direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
moveTo(getNewPosition());
|
moveTo(getNewPosition());
|
||||||
@ -122,7 +135,7 @@ public class Ghost extends BaseAnimated {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private double getSpeed() {
|
private double getSpeed() {
|
||||||
return BASE_SPEED * levelManager.getGhostSpeed();
|
return speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveTo(MyPoint newPosition) {
|
private void moveTo(MyPoint newPosition) {
|
||||||
@ -140,7 +153,7 @@ public class Ghost extends BaseAnimated {
|
|||||||
* @param directions a list of potential movement directions
|
* @param directions a list of potential movement directions
|
||||||
* @return a map where keys are valid directions and values are their associated priority
|
* @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()
|
return directions.stream()
|
||||||
.filter(d -> d != Direction.NONE)
|
.filter(d -> d != Direction.NONE)
|
||||||
.collect(Collectors.toMap(
|
.collect(Collectors.toMap(
|
||||||
@ -186,6 +199,7 @@ public class Ghost extends BaseAnimated {
|
|||||||
best = d;
|
best = d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.debug("Ghost comming from {}, choosing {}, from {} (dist={})", prevDirection, best, directions, bestDist);
|
||||||
return best;
|
return best;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,6 +209,7 @@ public class Ghost extends BaseAnimated {
|
|||||||
case CHASE -> {
|
case CHASE -> {
|
||||||
animation = baseAnimation;
|
animation = baseAnimation;
|
||||||
currentStrategy = chaseStrategy;
|
currentStrategy = chaseStrategy;
|
||||||
|
speed = BASE_SPEED * levelManager.getGhostSpeed();
|
||||||
}
|
}
|
||||||
case SCATTER -> currentStrategy = scaterStrategy;
|
case SCATTER -> currentStrategy = scaterStrategy;
|
||||||
case FRIGHTENED -> {
|
case FRIGHTENED -> {
|
||||||
@ -203,7 +218,11 @@ public class Ghost extends BaseAnimated {
|
|||||||
animation = fearAnimation;
|
animation = fearAnimation;
|
||||||
currentStrategy = fearStrategy;
|
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;
|
return mode == GhostMode.FRIGHTENED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetPosition() {
|
public void reset() {
|
||||||
position = startPos;
|
position = startPos;
|
||||||
|
this.speed = BASE_SPEED * levelManager.getGhostSpeed();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getPosition() {
|
public Point getPosition() {
|
||||||
@ -223,4 +243,7 @@ public class Ghost extends BaseAnimated {
|
|||||||
mode = GhostMode.CHASE;
|
mode = GhostMode.CHASE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isEaten() {
|
||||||
|
return mode == GhostMode.EATEN;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,16 @@
|
|||||||
|
package se.urmo.game.entities.ghost.strategy;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
|
import java.awt.Point;
|
||||||
|
|
||||||
|
public class EatenStrategy implements GhostStrategy {
|
||||||
|
@Override
|
||||||
|
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
|
||||||
|
return new Point(
|
||||||
|
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X + (GameMap.MAP_TILESIZE / 2),
|
||||||
|
4 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + (GameMap.MAP_TILESIZE / 2));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,12 +6,6 @@ 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.GhostMode;
|
import se.urmo.game.entities.ghost.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;
|
||||||
@ -48,9 +42,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);
|
||||||
|
|
||||||
@ -97,10 +91,14 @@ public class GhostManager {
|
|||||||
public void reset() {
|
public void reset() {
|
||||||
phaseIndex = 0;
|
phaseIndex = 0;
|
||||||
setMode(GhostMode.SCATTER);
|
setMode(GhostMode.SCATTER);
|
||||||
ghosts.forEach(Ghost::resetPosition);
|
ghosts.forEach(Ghost::reset);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFrozen(boolean frozen) {
|
public void setFrozen(boolean frozen) {
|
||||||
this.ghosts.forEach(ghost -> ghost.setFrozen(frozen));
|
this.ghosts.forEach(ghost -> ghost.setFrozen(frozen));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int isFrightened() {
|
||||||
|
return (int) ghosts.stream().filter(Ghost::isFrightened).count();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -52,6 +52,7 @@ public class PlayingState implements GameState {
|
|||||||
private RoundPhase phase = RoundPhase.PLAYING;
|
private RoundPhase phase = RoundPhase.PLAYING;
|
||||||
private long phaseStartMs = System.currentTimeMillis();
|
private long phaseStartMs = System.currentTimeMillis();
|
||||||
private boolean deathInProgress;
|
private boolean deathInProgress;
|
||||||
|
private int frightMultiplier;
|
||||||
|
|
||||||
public PlayingState(GameStateManager gameStateManager, GameOverState gameOverState) {
|
public PlayingState(GameStateManager gameStateManager, GameOverState gameOverState) {
|
||||||
this.gameStateManager = gameStateManager;
|
this.gameStateManager = gameStateManager;
|
||||||
@ -129,6 +130,7 @@ public class PlayingState implements GameState {
|
|||||||
boolean wasRemoved = map.removeTileImage(pacmanScreenPos);
|
boolean wasRemoved = map.removeTileImage(pacmanScreenPos);
|
||||||
if (wasRemoved && tile.getTileType() == TileType.LARGE_PELLET) {
|
if (wasRemoved && tile.getTileType() == TileType.LARGE_PELLET) {
|
||||||
ghostManager.setFrightMode();
|
ghostManager.setFrightMode();
|
||||||
|
frightMultiplier = 1;
|
||||||
}
|
}
|
||||||
if (wasRemoved) {
|
if (wasRemoved) {
|
||||||
dotsEaten++;
|
dotsEaten++;
|
||||||
@ -211,11 +213,12 @@ public class PlayingState implements GameState {
|
|||||||
//if(overlap(pacman, ghost)
|
//if(overlap(pacman, ghost)
|
||||||
double dist = pacman.distanceTo(ghost.getPosition());
|
double dist = pacman.distanceTo(ghost.getPosition());
|
||||||
if (dist < GameMap.MAP_TILESIZE / 2.0) {
|
if (dist < GameMap.MAP_TILESIZE / 2.0) {
|
||||||
|
if (ghost.isEaten()) return;
|
||||||
if (ghost.isFrightened()) {
|
if (ghost.isFrightened()) {
|
||||||
// Pac-Man eats ghost
|
// Pac-Man eats ghost
|
||||||
score += 200;
|
score += 200 * (1 << (ghostManager.getGhosts().size() - ghostManager.isFrightened()));
|
||||||
ghost.resetPosition();
|
|
||||||
ghost.setMode(GhostMode.CHASE); // end frightend
|
ghost.setMode(GhostMode.EATEN);
|
||||||
} else {
|
} else {
|
||||||
ghostManager.setFrozen(true);
|
ghostManager.setFrozen(true);
|
||||||
pacman.startDeathAnimation();
|
pacman.startDeathAnimation();
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package se.urmo.game.util;
|
package se.urmo.game.util;
|
||||||
|
|
||||||
|
import java.awt.Point;
|
||||||
|
|
||||||
public class MyPoint {
|
public class MyPoint {
|
||||||
public final double x;
|
public final double x;
|
||||||
public final double y;
|
public final double y;
|
||||||
@ -9,6 +11,10 @@ public class MyPoint {
|
|||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Point asPoint() {
|
||||||
|
return new Point((int) x, (int) y);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "MyPoint{" +
|
return "MyPoint{" +
|
||||||
|
|||||||
Reference in New Issue
Block a user