Ghost refactoring
This commit is contained in:
@ -0,0 +1,138 @@
|
|||||||
|
package se.urmo.game.entities.ghost;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import se.urmo.game.entities.ghost.strategy.GhostStrategy;
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.graphics.SpriteLocation;
|
||||||
|
import se.urmo.game.graphics.SpriteSheetManager;
|
||||||
|
import se.urmo.game.main.LevelManager;
|
||||||
|
import se.urmo.game.map.GameMap;
|
||||||
|
import se.urmo.game.util.Direction;
|
||||||
|
import se.urmo.game.util.MyPoint;
|
||||||
|
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public abstract class AbstractGhostModeImpl implements GhostState {
|
||||||
|
private final Ghost ghost;
|
||||||
|
protected GhostStrategy strategy;
|
||||||
|
protected final LevelManager levelManager;
|
||||||
|
protected int animation;
|
||||||
|
|
||||||
|
public AbstractGhostModeImpl(Ghost ghost, GhostStrategy strategy, LevelManager levelManager, int animation) {
|
||||||
|
this.ghost = ghost;
|
||||||
|
this.strategy = strategy;
|
||||||
|
this.levelManager = levelManager;
|
||||||
|
this.animation = animation;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update method to be implemented by each mode
|
||||||
|
*/
|
||||||
|
public abstract void update(Ghost ghost, PacMan pacman, GameMap map);
|
||||||
|
|
||||||
|
public double getSpeed() {
|
||||||
|
return Ghost.BASE_SPEED * levelManager.getGhostSpeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyPoint getPosition() {
|
||||||
|
return ghost.getPosition();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Direction getDirection() {
|
||||||
|
return ghost.getDirection();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BufferedImage[] getAnimation() {
|
||||||
|
return SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(animation);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void updatePosition(Ghost ghost, PacMan pacman, GameMap map) {
|
||||||
|
if (map.isAligned(getPosition().asPoint())) {
|
||||||
|
ghost.setPrevDirection(getDirection());
|
||||||
|
ghost.setDirection(chooseDirection(
|
||||||
|
ghost,
|
||||||
|
prioritizeDirections(
|
||||||
|
ghost.getCollisionChecker().calculateDirectionAlternatives(getPosition())),
|
||||||
|
getStrategy().chooseTarget(ghost, pacman, map)));
|
||||||
|
|
||||||
|
log.debug("Ghost moving to {}", getPosition());
|
||||||
|
}
|
||||||
|
|
||||||
|
moveTo(ghost, getNewPosition(getSpeed()));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected GhostStrategy getStrategy() {
|
||||||
|
return strategy;
|
||||||
|
}
|
||||||
|
|
||||||
|
private MyPoint getNewPosition(double speed) {
|
||||||
|
MyPoint position = ghost.getPosition();
|
||||||
|
Direction direction = ghost.getDirection();
|
||||||
|
return new MyPoint(
|
||||||
|
position.x + direction.dx * speed,
|
||||||
|
position.y + direction.dy * speed);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void moveTo(Ghost ghost, MyPoint newPosition) {
|
||||||
|
MyPoint destination = ghost.getCollisionChecker().canMoveTo(
|
||||||
|
getDirection(), newPosition.x, newPosition.y);
|
||||||
|
if (destination != null) {
|
||||||
|
ghost.setPosition(destination);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<Direction, Integer> prioritizeDirections(List<Direction> directions) {
|
||||||
|
return directions.stream()
|
||||||
|
.filter(d -> d != Direction.NONE)
|
||||||
|
.collect(Collectors.toMap(
|
||||||
|
d -> d,
|
||||||
|
d -> (ghost.getPrevDirection() != null &&
|
||||||
|
d == ghost.getPrevDirection().opposite()) ? 2 : 1
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Direction chooseDirection(Ghost ghost, Map<Direction, Integer> options, Point target) {
|
||||||
|
// Find the lowest priority
|
||||||
|
int lowestPriority = options.values().stream()
|
||||||
|
.mapToInt(Integer::intValue)
|
||||||
|
.min()
|
||||||
|
.orElse(Integer.MAX_VALUE);
|
||||||
|
|
||||||
|
// Collect all directions that have this priority
|
||||||
|
List<Direction> directions = options.entrySet().stream()
|
||||||
|
.filter(entry -> entry.getValue() == lowestPriority)
|
||||||
|
.map(Map.Entry::getKey)
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
// Calculate the direction that has the lowest distance to the target
|
||||||
|
Direction best = directions.getFirst();
|
||||||
|
double bestDist = Double.MAX_VALUE;
|
||||||
|
|
||||||
|
MyPoint position = getPosition();
|
||||||
|
for (Direction d : directions) {
|
||||||
|
double nx = position.x + d.dx * GameMap.MAP_TILESIZE;
|
||||||
|
double ny = position.y + d.dy * GameMap.MAP_TILESIZE;
|
||||||
|
double dist = target.distance(nx, ny);
|
||||||
|
if (dist < bestDist) {
|
||||||
|
bestDist = dist;
|
||||||
|
best = d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("Ghost coming from {}, choosing {}, from {} (dist={})",
|
||||||
|
ghost.getPrevDirection(), best, directions, bestDist);
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetPosition() {
|
||||||
|
ghost.setPosition(Ghost.getStartPosition());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
package se.urmo.game.entities.ghost;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import se.urmo.game.entities.ghost.strategy.GhostStrategy;
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class ChaseGhostMode extends AbstractGhostModeImpl {
|
||||||
|
|
||||||
|
public ChaseGhostMode(Ghost ghost, GhostStrategy strategy) {
|
||||||
|
super(ghost, strategy, ghost.getLevelManager(), ghost.getAnimation());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(Ghost ghost, PacMan pacman, GameMap map) {
|
||||||
|
// Use the common position update method with chase strategy
|
||||||
|
updatePosition(ghost, pacman, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public void enter(Ghost ghost) {
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getSpeed() {
|
||||||
|
return Ghost.BASE_SPEED * levelManager.getGhostSpeed();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
package se.urmo.game.entities.ghost;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import se.urmo.game.entities.ghost.strategy.EatenStrategy;
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.graphics.SpriteLocation;
|
||||||
|
import se.urmo.game.graphics.SpriteSheetManager;
|
||||||
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class EatenGhostMode extends AbstractGhostModeImpl {
|
||||||
|
// Eaten mode specific constants
|
||||||
|
private static final BufferedImage[] EATEN_ANIMATION = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(9);
|
||||||
|
private static final double EATEN_SPEED = 1.0; // Faster when eaten
|
||||||
|
|
||||||
|
public EatenGhostMode(Ghost ghost) {
|
||||||
|
super(ghost, new EatenStrategy(), ghost.getLevelManager(), 9);
|
||||||
|
// Eaten mode uses a specific strategy to return home
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(Ghost ghost, PacMan pacman, GameMap map) {
|
||||||
|
// Check if ghost has reached its starting position
|
||||||
|
if (getPosition().asPoint().distance(Ghost.getStartPosition().asPoint()) < 10) {
|
||||||
|
log.debug("Ghost reached home, returning to chase mode");
|
||||||
|
ghost.setMode(GhostMode.CHASE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update position using eaten strategy
|
||||||
|
updatePosition(ghost, pacman, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public void enter(Ghost ghost) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BufferedImage[] getAnimation() {
|
||||||
|
return EATEN_ANIMATION;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getSpeed() {
|
||||||
|
return EATEN_SPEED;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,86 @@
|
|||||||
|
package se.urmo.game.entities.ghost;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import se.urmo.game.entities.ghost.strategy.FearStrategy;
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.graphics.SpriteLocation;
|
||||||
|
import se.urmo.game.graphics.SpriteSheetManager;
|
||||||
|
import se.urmo.game.main.Game;
|
||||||
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class FrightenedGhostMode extends AbstractGhostModeImpl {
|
||||||
|
// Frightened mode specific constants
|
||||||
|
private static final int WARNING_THRESHOLD = 180; // 3 seconds of warning
|
||||||
|
private static final BufferedImage[] FEAR_ANIMATION = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(8);
|
||||||
|
private static final int FRIGHTENED_DURATION_TICKS = 10 * Game.UPS_SET;
|
||||||
|
|
||||||
|
// Frightened mode specific state
|
||||||
|
private int frightenedTimer = FRIGHTENED_DURATION_TICKS;
|
||||||
|
private boolean isBlinking = false;
|
||||||
|
|
||||||
|
public FrightenedGhostMode(Ghost ghost) {
|
||||||
|
super(ghost, new FearStrategy(), ghost.getLevelManager(), 8);
|
||||||
|
strategy = new FearStrategy();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(Ghost ghost, PacMan pacman, GameMap map) {
|
||||||
|
updateFrightened(ghost);
|
||||||
|
updatePosition(ghost, pacman, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BufferedImage[] getAnimation() {
|
||||||
|
return (isBlinking ? FEAR_ANIMATION : super.getAnimation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getSpeed() {
|
||||||
|
return 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the frightened state timer and handle blinking animation
|
||||||
|
*/
|
||||||
|
private void updateFrightened(Ghost ghost) {
|
||||||
|
frightenedTimer--;
|
||||||
|
|
||||||
|
// Handle blinking animation when timer is running low
|
||||||
|
if (frightenedTimer <= WARNING_THRESHOLD) {
|
||||||
|
isBlinking = (frightenedTimer / 25) % 2 == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if frightened mode should end
|
||||||
|
if (frightenedTimer <= 0) {
|
||||||
|
log.debug("Frightened mode ended");
|
||||||
|
ghost.setMode(GhostMode.CHASE);
|
||||||
|
frightenedTimer = FRIGHTENED_DURATION_TICKS;
|
||||||
|
isBlinking = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// public void enter(Ghost ghost) {
|
||||||
|
// // Initialize frightened mode-specific state
|
||||||
|
// log.debug("Entering frightened mode");
|
||||||
|
// frightenedTimer = FRIGHTENED_DURATION_TICKS;
|
||||||
|
// isBlinking = false;
|
||||||
|
// }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current time remaining in frightened mode (for external queries)
|
||||||
|
*/
|
||||||
|
public int getRemainingTime() {
|
||||||
|
return frightenedTimer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the frightened timer (for power pellet extensions)
|
||||||
|
*/
|
||||||
|
public void resetTimer() {
|
||||||
|
frightenedTimer = FRIGHTENED_DURATION_TICKS;
|
||||||
|
isBlinking = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package se.urmo.game.entities.ghost;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class FrozenGhostMode extends AbstractGhostModeImpl {
|
||||||
|
public FrozenGhostMode(Ghost ghost) {
|
||||||
|
super(ghost, null, ghost.getLevelManager(), ghost.getAnimation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(Ghost ghost, PacMan pacman, GameMap map) {
|
||||||
|
// Do not update position - frozen ghosts don't move
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public void enter(Ghost ghost) {
|
||||||
|
// log.debug("Entering frozen mode");
|
||||||
|
// }
|
||||||
|
}
|
||||||
@ -1,16 +1,14 @@
|
|||||||
package se.urmo.game.entities.ghost;
|
package se.urmo.game.entities.ghost;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
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.GhostStrategy;
|
import se.urmo.game.entities.ghost.strategy.GhostStrategy;
|
||||||
import se.urmo.game.entities.pacman.PacMan;
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
import se.urmo.game.graphics.SpriteLocation;
|
import se.urmo.game.graphics.SpriteLocation;
|
||||||
import se.urmo.game.graphics.SpriteSheetManager;
|
import se.urmo.game.graphics.SpriteSheetManager;
|
||||||
import se.urmo.game.main.Game;
|
|
||||||
import se.urmo.game.main.GhostManager;
|
import se.urmo.game.main.GhostManager;
|
||||||
import se.urmo.game.main.LevelManager;
|
import se.urmo.game.main.LevelManager;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
@ -18,64 +16,69 @@ import se.urmo.game.util.Direction;
|
|||||||
import se.urmo.game.util.MyPoint;
|
import se.urmo.game.util.MyPoint;
|
||||||
|
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Point;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.List;
|
import java.util.EnumMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class Ghost extends BaseAnimated {
|
public class Ghost extends BaseAnimated {
|
||||||
private static final double BASE_SPEED = 0.40;
|
public static final double BASE_SPEED = 0.40;
|
||||||
private static final int WARNING_THRESHOLD = 180; // 3 seconds of warning
|
|
||||||
public static final int GHOST_SIZE = 32;
|
public static final int GHOST_SIZE = 32;
|
||||||
private static final int ANIMATION_UPDATE_FREQUENCY = 25;
|
private static final int ANIMATION_UPDATE_FREQUENCY = 25;
|
||||||
private static final int FRIGHTENED_DURATION_TICKS = 10 * Game.UPS_SET;
|
|
||||||
|
|
||||||
private final GhostCollisionChecker collisionChecker;
|
|
||||||
private final GhostStrategy chaseStrategy;
|
|
||||||
private final MyPoint startPos;
|
|
||||||
private static final BufferedImage[] fearAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(8);
|
|
||||||
private final BufferedImage[] baseAnimation;
|
|
||||||
private final LevelManager levelManager;
|
|
||||||
private static final BufferedImage[] eatenAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(9);
|
|
||||||
private double speed;
|
|
||||||
@Getter
|
@Getter
|
||||||
private MyPoint position;
|
private final GhostCollisionChecker collisionChecker;
|
||||||
|
@Getter
|
||||||
|
private final BufferedImage[] baseAnimation;
|
||||||
|
@Getter
|
||||||
|
private final int animation;
|
||||||
|
@Getter
|
||||||
|
private final LevelManager levelManager;
|
||||||
|
|
||||||
|
@Getter
|
||||||
private final GhostStrategy scaterStrategy;
|
private final GhostStrategy scaterStrategy;
|
||||||
private GhostStrategy currentStrategy;
|
private GhostState currentState;
|
||||||
private BufferedImage[] animation;
|
|
||||||
private Direction direction;
|
private final Map<GhostMode, GhostState> states = new EnumMap<>(GhostMode.class);
|
||||||
private Direction prevDirection;
|
|
||||||
private GhostMode mode;
|
// Movement-related state
|
||||||
private final GhostStrategy fearStrategy = new FearStrategy();
|
@Getter
|
||||||
private int frightenedTimer = 0;
|
@Setter
|
||||||
private boolean isBlinking = false;
|
protected MyPoint position;
|
||||||
private static final GhostStrategy eatenStrategy = new EatenStrategy();
|
@Getter
|
||||||
|
@Setter
|
||||||
|
protected Direction direction;
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
protected Direction prevDirection;
|
||||||
|
@Getter
|
||||||
|
private static final MyPoint startPosition = new MyPoint(
|
||||||
|
GameMap.colToScreen(13) + ((double) GameMap.MAP_TILESIZE / 2),
|
||||||
|
GameMap.rowToScreen(4) + ((double) GameMap.MAP_TILESIZE / 2));
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy chaseStrategy, 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);
|
super(ANIMATION_UPDATE_FREQUENCY, GhostManager.MAX_SPRITE_FRAMES);
|
||||||
this.collisionChecker = collisionChecker;
|
this.collisionChecker = collisionChecker;
|
||||||
this.chaseStrategy = chaseStrategy;
|
|
||||||
this.scaterStrategy = scaterStrategy;
|
this.scaterStrategy = scaterStrategy;
|
||||||
this.currentStrategy = this.chaseStrategy;
|
|
||||||
this.baseAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(animation);
|
this.baseAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(animation);
|
||||||
|
this.animation = animation;
|
||||||
position = new MyPoint(
|
|
||||||
GameMap.colToScreen(13) + ((double) GameMap.MAP_TILESIZE / 2),
|
|
||||||
GameMap.rowToScreen(4) + ((double) GameMap.MAP_TILESIZE / 2));
|
|
||||||
startPos = position;
|
|
||||||
|
|
||||||
this.animation = baseAnimation;
|
|
||||||
this.levelManager = levelManager;
|
this.levelManager = levelManager;
|
||||||
this.speed = BASE_SPEED * levelManager.getGhostSpeed();
|
states.put(GhostMode.CHASE, new ChaseGhostMode(this, chaseStrategy));
|
||||||
|
states.put(GhostMode.SCATTER, new ScatterGhostMode(this, scaterStrategy));
|
||||||
|
states.put(GhostMode.FRIGHTENED, new FrightenedGhostMode(this));
|
||||||
|
states.put(GhostMode.EATEN, new EatenGhostMode(this));
|
||||||
|
states.put(GhostMode.FROZEN, new FrozenGhostMode(this));
|
||||||
|
|
||||||
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(Graphics g) {
|
public void draw(Graphics g) {
|
||||||
|
MyPoint position = currentState.getPosition();
|
||||||
g.drawImage(
|
g.drawImage(
|
||||||
animation[aniIndex],
|
currentState.getAnimation()[aniIndex],
|
||||||
(int) position.x - GHOST_SIZE / 2,
|
(int) position.x - GHOST_SIZE / 2,
|
||||||
(int) position.y - GHOST_SIZE / 2,
|
(int) position.y - GHOST_SIZE / 2,
|
||||||
GHOST_SIZE,
|
GHOST_SIZE,
|
||||||
@ -83,162 +86,25 @@ public class Ghost extends BaseAnimated {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void update(PacMan pacman, GameMap map) {
|
public void update(PacMan pacman, GameMap map) {
|
||||||
switch (mode) {
|
currentState.update(this, pacman, map);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateInFrightendMode() {
|
|
||||||
frightenedTimer--;
|
|
||||||
if (frightenedTimer <= WARNING_THRESHOLD) {
|
|
||||||
isBlinking = (frightenedTimer / 25) % 2 == 0;
|
|
||||||
animation = isBlinking ? fearAnimation : baseAnimation;
|
|
||||||
}
|
|
||||||
if (frightenedTimer <= 0) {
|
|
||||||
setMode(GhostMode.CHASE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updatePosition(PacMan pacman, GameMap map) {
|
|
||||||
if (map.isAligned(position.asPoint())) {
|
|
||||||
prevDirection = direction;
|
|
||||||
direction = chooseDirection(
|
|
||||||
prioritizeDirections(collisionChecker.calculateDirectionAlternatives(position)),
|
|
||||||
currentStrategy.chooseTarget(this, pacman, map));
|
|
||||||
log.debug("Ghost moving to {}", direction);
|
|
||||||
}
|
|
||||||
|
|
||||||
moveTo(getNewPosition());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Given a position and a direction - calculate the new position
|
|
||||||
* Moves one pixel in the given direction
|
|
||||||
*
|
|
||||||
* @return new position
|
|
||||||
*/
|
|
||||||
private MyPoint getNewPosition() {
|
|
||||||
return new MyPoint(
|
|
||||||
position.x + direction.dx * getSpeed(),
|
|
||||||
position.y + direction.dy * getSpeed());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void moveTo(MyPoint newPosition) {
|
|
||||||
MyPoint destination = collisionChecker.canMoveTo(direction, newPosition.x, newPosition.y);
|
|
||||||
if (destination != null) {
|
|
||||||
position = destination;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a map of directions and their associated priority values based on the given list of directions.
|
|
||||||
* Directions with a value of {@code Direction.NONE} are excluded. If a direction is opposite to the
|
|
||||||
* previous direction, it is given a higher priority value.
|
|
||||||
*
|
|
||||||
* @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> prioritizeDirections(List<Direction> directions) {
|
|
||||||
return directions.stream()
|
|
||||||
.filter(d -> d != Direction.NONE)
|
|
||||||
.collect(Collectors.toMap(
|
|
||||||
d -> d,
|
|
||||||
d -> (prevDirection != null && d == prevDirection.opposite()) ? 2 : 1
|
|
||||||
));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Selects the best movement direction for the ghost based on priority and distance to the target.
|
|
||||||
* The method evaluates the directions with the lowest priority value and chooses the one that
|
|
||||||
* minimizes the distance to the target point.
|
|
||||||
*
|
|
||||||
* @param options a map where keys represent potential movement directions and values
|
|
||||||
* represent their associated priority levels
|
|
||||||
* @param target the target point towards which the direction is evaluated
|
|
||||||
* @return the direction that has the lowest priority and minimizes the distance to the target
|
|
||||||
*/
|
|
||||||
private Direction chooseDirection(Map<Direction, Integer> options, Point target) {
|
|
||||||
// Find the lowest priority
|
|
||||||
int lowestPriority = options.values().stream()
|
|
||||||
.mapToInt(Integer::intValue)
|
|
||||||
.min()
|
|
||||||
.orElse(Integer.MAX_VALUE);
|
|
||||||
|
|
||||||
// Collect all directions that have this priority
|
|
||||||
List<Direction> directions = options.entrySet().stream()
|
|
||||||
.filter(entry -> entry.getValue() == lowestPriority)
|
|
||||||
.map(Map.Entry::getKey)
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
// Calculate the direction that has the lowest distance to the target
|
|
||||||
Direction best = directions.getFirst();
|
|
||||||
double bestDist = Double.MAX_VALUE;
|
|
||||||
|
|
||||||
for (Direction d : directions) {
|
|
||||||
double nx = position.x + d.dx * GameMap.MAP_TILESIZE;
|
|
||||||
double ny = position.y + d.dy * GameMap.MAP_TILESIZE;
|
|
||||||
double dist = target.distance(nx, ny);
|
|
||||||
if (dist < bestDist) {
|
|
||||||
bestDist = dist;
|
|
||||||
best = d;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.debug("Ghost comming from {}, choosing {}, from {} (dist={})", prevDirection, best, directions, bestDist);
|
|
||||||
return best;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMode(GhostMode mode) {
|
public void setMode(GhostMode mode) {
|
||||||
this.mode = mode;
|
currentState = states.get(mode);
|
||||||
switch (mode) {
|
//currentState.enter(this);
|
||||||
case CHASE -> {
|
|
||||||
animation = baseAnimation;
|
|
||||||
currentStrategy = chaseStrategy;
|
|
||||||
speed = BASE_SPEED * levelManager.getGhostSpeed();
|
|
||||||
}
|
|
||||||
case SCATTER -> currentStrategy = scaterStrategy;
|
|
||||||
case FRIGHTENED -> {
|
|
||||||
frightenedTimer = FRIGHTENED_DURATION_TICKS;
|
|
||||||
isBlinking = false;
|
|
||||||
animation = fearAnimation;
|
|
||||||
currentStrategy = fearStrategy;
|
|
||||||
}
|
|
||||||
case EATEN -> {
|
|
||||||
speed = 1.0;
|
|
||||||
currentStrategy = eatenStrategy;
|
|
||||||
animation = eatenAnimation;
|
|
||||||
}
|
|
||||||
case FROZEN -> {/* Do nothing */}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private double getSpeed() {
|
|
||||||
return this.speed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFrightened() {
|
public boolean isFrightened() {
|
||||||
return mode == GhostMode.FRIGHTENED;
|
return states.get(GhostMode.FRIGHTENED) == currentState;
|
||||||
}
|
|
||||||
|
|
||||||
public void reset() {
|
|
||||||
position = startPos;
|
|
||||||
this.speed = BASE_SPEED * levelManager.getGhostSpeed();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEaten() {
|
public boolean isEaten() {
|
||||||
return mode == GhostMode.EATEN;
|
return states.get(GhostMode.EATEN) == currentState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
currentState = states.get(GhostMode.CHASE);
|
||||||
|
//currentState.enter(this);
|
||||||
|
((ChaseGhostMode) currentState).resetPosition();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/main/java/se/urmo/game/entities/ghost/GhostState.java
Normal file
19
src/main/java/se/urmo/game/entities/ghost/GhostState.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package se.urmo.game.entities.ghost;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.map.GameMap;
|
||||||
|
import se.urmo.game.util.Direction;
|
||||||
|
import se.urmo.game.util.MyPoint;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
public interface GhostState {
|
||||||
|
void update(Ghost ghost, PacMan pacman, GameMap map);
|
||||||
|
//void enter(Ghost ghost);
|
||||||
|
|
||||||
|
MyPoint getPosition();
|
||||||
|
|
||||||
|
Direction getDirection();
|
||||||
|
|
||||||
|
BufferedImage[] getAnimation();
|
||||||
|
}
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
package se.urmo.game.entities.ghost;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import se.urmo.game.entities.ghost.strategy.GhostStrategy;
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class ScatterGhostMode extends AbstractGhostModeImpl {
|
||||||
|
// Time in scatter mode before returning to chase
|
||||||
|
private static final int SCATTER_DURATION = 7 * 60; // 7 seconds at 60 ticks/second
|
||||||
|
private int scatterTimer = 0;
|
||||||
|
|
||||||
|
public ScatterGhostMode(Ghost ghost, GhostStrategy scaterStrategy) {
|
||||||
|
super(ghost, scaterStrategy, ghost.getLevelManager(), ghost.getAnimation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(Ghost ghost, PacMan pacman, GameMap map) {
|
||||||
|
// Update scatter timer
|
||||||
|
updateScatterTimer(ghost);
|
||||||
|
|
||||||
|
// Use common position update with scatter strategy
|
||||||
|
updatePosition(ghost, pacman, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getSpeed() {
|
||||||
|
return Ghost.BASE_SPEED * levelManager.getGhostSpeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateScatterTimer(Ghost ghost) {
|
||||||
|
scatterTimer--;
|
||||||
|
if (scatterTimer <= 0) {
|
||||||
|
log.debug("Scatter mode timed out, returning to chase");
|
||||||
|
ghost.setMode(GhostMode.CHASE);
|
||||||
|
scatterTimer = SCATTER_DURATION;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public void enter(Ghost ghost) {
|
||||||
|
// log.debug("Entering scatter mode");
|
||||||
|
//
|
||||||
|
// // Initialize scatter mode timer
|
||||||
|
// scatterTimer = SCATTER_DURATION;
|
||||||
|
// }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the scatter timer (for extending scatter mode)
|
||||||
|
*/
|
||||||
|
public void resetTimer() {
|
||||||
|
scatterTimer = SCATTER_DURATION;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -106,11 +106,11 @@ public class GameMap {
|
|||||||
int col = screenToCol(screenX);
|
int col = screenToCol(screenX);
|
||||||
int tileY = (screenY - OFFSET_Y) % MAP_TILESIZE;
|
int tileY = (screenY - OFFSET_Y) % MAP_TILESIZE;
|
||||||
int tileX = (screenX - OFFSET_X) % MAP_TILESIZE;
|
int tileX = (screenX - OFFSET_X) % MAP_TILESIZE;
|
||||||
log.trace("Point[x={},y={}] is row={}, col={} with reminder x={},y={}", screenX, screenY, row, col, tileX, tileY);
|
//log.trace("Point[x={},y={}] is row={}, col={} with reminder x={},y={}", screenX, screenY, row, col, tileX, tileY);
|
||||||
|
|
||||||
boolean[][] mask = mapData[row][col].getCollisionMask();
|
boolean[][] mask = mapData[row][col].getCollisionMask();
|
||||||
boolean b = mask == null || !mask[tileY][tileX];
|
boolean b = mask == null || !mask[tileY][tileX];
|
||||||
log.trace(b ? " - passable" : " - not passable");
|
//log.trace(b ? " - passable" : " - not passable");
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ public class GameMap {
|
|||||||
if (row >= rows() || row < 0) return true;
|
if (row >= rows() || row < 0) return true;
|
||||||
MapTile mapTile = mapData[row][col];
|
MapTile mapTile = mapData[row][col];
|
||||||
boolean solid = mapTile.isSolid();
|
boolean solid = mapTile.isSolid();
|
||||||
log.debug("[{}][{}] {}", row, col, mapTile.getTileType());
|
//log.debug("[{}][{}] {}", row, col, mapTile.getTileType());
|
||||||
return solid;
|
return solid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,7 +144,7 @@ public class GameMap {
|
|||||||
TileType type = tile.getTileType();
|
TileType type = tile.getTileType();
|
||||||
|
|
||||||
if (type.isRemovable() && tile.getImage() != null) {
|
if (type.isRemovable() && tile.getImage() != null) {
|
||||||
log.debug("Removing tile {}", tile);
|
// log.debug("Removing tile {}", tile);
|
||||||
tile.setImage(null);
|
tile.setImage(null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user