Implemented frightmode
This commit is contained in:
@ -3,6 +3,7 @@ package se.urmo.game.entities;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import se.urmo.game.collision.GhostCollisionChecker;
|
||||
import se.urmo.game.main.Game;
|
||||
import se.urmo.game.map.GameMap;
|
||||
import se.urmo.game.state.GhostManager;
|
||||
import se.urmo.game.util.Direction;
|
||||
@ -18,16 +19,20 @@ import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class Ghost {
|
||||
private static final int WARNING_THRESHOLD = 180; // 3 seconds of warning
|
||||
private static final int COLLISION_BOX_SIZE = 16;
|
||||
private static final int GHOST_MOVEMENT_UPDATE_FREQUENCY = 2;
|
||||
public static final int GHOST_SIZE = 32;
|
||||
private static final int ANIMATION_UPDATE_FREQUENCY = 25;
|
||||
private static final int COLLISION_BOX_OFFSET = COLLISION_BOX_SIZE / 2;
|
||||
private static final BufferedImage COLLISION_BOX = MiscUtil.createOutlinedBox(COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, Color.black, 2);
|
||||
private static final int FRIGHTENED_DURATION_TICKS = 10 * Game.UPS_SET;
|
||||
|
||||
private final GhostCollisionChecker collisionChecker;
|
||||
private final GhostStrategy chaseStrategy;
|
||||
private final Point startPos;
|
||||
private final BufferedImage[] fearAnimation;
|
||||
private final BufferedImage[] baseAnimation;
|
||||
@Getter
|
||||
private Point position;
|
||||
|
||||
@ -36,23 +41,29 @@ public class Ghost {
|
||||
private int aniIndex = 0;
|
||||
private final GhostStrategy scaterStrategy;
|
||||
private GhostStrategy currentStrategy;
|
||||
private final BufferedImage[] animation;
|
||||
private BufferedImage[] animation;
|
||||
private int movementTick = 0;
|
||||
private Direction direction;
|
||||
private Direction prevDirection;
|
||||
private GhostMode mode;
|
||||
private final GhostStrategy fearStrategy = new FearStrategy();
|
||||
private int frightenedTimer = 0;
|
||||
private boolean isBlinking = false;
|
||||
|
||||
|
||||
public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy strategy, GhostStrategy scaterStrategy, BufferedImage[] animation) {
|
||||
|
||||
public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy strategy, GhostStrategy scaterStrategy, BufferedImage[] animation, BufferedImage[] fearAnimation) {
|
||||
this.collisionChecker = collisionChecker;
|
||||
this.chaseStrategy = strategy;
|
||||
this.scaterStrategy = scaterStrategy;
|
||||
this.animation = animation;
|
||||
this.baseAnimation = animation;
|
||||
this.fearAnimation = fearAnimation;
|
||||
position = new Point(
|
||||
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X + (GameMap.MAP_TILESIZE / 2),
|
||||
4 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + (GameMap.MAP_TILESIZE / 2) );
|
||||
startPos = position;
|
||||
this.currentStrategy = chaseStrategy;
|
||||
this.animation = baseAnimation;
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
@ -71,17 +82,36 @@ public class Ghost {
|
||||
|
||||
public void update(PacMan pacman, GameMap map) {
|
||||
updateAnimationTick();
|
||||
if(mode == GhostMode.FRIGHTENED){
|
||||
updateInFrightendMode();
|
||||
}
|
||||
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) {
|
||||
// only move ghost on update interval - this is basically ghost speed;
|
||||
if (movementTick >= GHOST_MOVEMENT_UPDATE_FREQUENCY) {
|
||||
chooseDirection(pacman, map);
|
||||
if (map.isAligned(position)) {
|
||||
//log.info("Evaluating possible directions");
|
||||
prevDirection = direction;
|
||||
direction = chooseDirection(
|
||||
prioritize(collisionChecker.calculateDirectionAlternatives(position)),
|
||||
currentStrategy.chooseTarget(this, pacman, map));
|
||||
//log.info("selecting direction {}", direction);
|
||||
}
|
||||
|
||||
Point newPosition = getNewPosition();
|
||||
|
||||
move(newPosition);
|
||||
moveTo(getNewPosition());
|
||||
|
||||
movementTick = 0;
|
||||
} else movementTick++;
|
||||
@ -89,6 +119,7 @@ public class Ghost {
|
||||
|
||||
/**
|
||||
* Given a position and a direction - calculate the new position
|
||||
* Moves one pixel in the given direction
|
||||
*
|
||||
* @return new position
|
||||
*/
|
||||
@ -101,25 +132,7 @@ public class Ghost {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Choose a new direction when 'aligned' ie when in the exact middle of a tile
|
||||
* else continue with the existing direction.
|
||||
*
|
||||
* @param pacman
|
||||
* @param map
|
||||
*/
|
||||
private void chooseDirection(PacMan pacman, GameMap map) {
|
||||
if (isAlligned(position)) {
|
||||
log.info("Evaluating possible directions");
|
||||
prevDirection = direction;
|
||||
direction = chooseDirection(
|
||||
prioritize(collisionChecker.calculateDirectionAlternatives(position)),
|
||||
currentStrategy.chooseTarget(this, pacman, map));
|
||||
log.info("selecting direction {}", direction);
|
||||
}
|
||||
}
|
||||
|
||||
private void move(Point newPosition) {
|
||||
private void moveTo(Point newPosition) {
|
||||
Point destination = collisionChecker.canMoveTo(direction, newPosition);
|
||||
|
||||
if (destination != null) {
|
||||
@ -137,14 +150,7 @@ public class Ghost {
|
||||
|
||||
}
|
||||
|
||||
private boolean isAlligned(Point pos) {
|
||||
int row = pos.x % GameMap.MAP_TILESIZE;
|
||||
int col = pos.y % GameMap.MAP_TILESIZE;
|
||||
return row == GameMap.MAP_TILESIZE / 2 && col == GameMap.MAP_TILESIZE / 2;
|
||||
}
|
||||
|
||||
private Direction chooseDirection(Map<Direction, Integer> options, Point target) {
|
||||
|
||||
// Find the lowest priority
|
||||
int lowestPriority = options.values().stream()
|
||||
.mapToInt(Integer::intValue)
|
||||
@ -189,15 +195,23 @@ public class Ghost {
|
||||
public void setMode(GhostMode mode) {
|
||||
this.mode = mode;
|
||||
switch (mode) {
|
||||
case CHASE -> currentStrategy = chaseStrategy;
|
||||
case CHASE -> {
|
||||
animation = baseAnimation;
|
||||
currentStrategy = chaseStrategy;
|
||||
}
|
||||
case SCATTER -> currentStrategy = scaterStrategy;
|
||||
case FRIGHTENED -> currentStrategy = null;
|
||||
case FRIGHTENED -> {
|
||||
frightenedTimer = FRIGHTENED_DURATION_TICKS;
|
||||
isBlinking = false;
|
||||
animation = fearAnimation;
|
||||
currentStrategy = fearStrategy;
|
||||
}
|
||||
case EATEN -> currentStrategy = null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isFrightened() {
|
||||
return false;
|
||||
return mode == GhostMode.FRIGHTENED;
|
||||
}
|
||||
|
||||
public void resetPosition() {
|
||||
|
||||
Reference in New Issue
Block a user