Tidying up Ghost
This commit is contained in:
@ -74,30 +74,58 @@ public class Ghost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updatePosition(PacMan pacman, GameMap map) {
|
private void updatePosition(PacMan pacman, GameMap map) {
|
||||||
|
// only move ghost on update interval - this is basically ghost speed;
|
||||||
if (movementTick >= GHOST_MOVEMENT_UPDATE_FREQUENCY) {
|
if (movementTick >= GHOST_MOVEMENT_UPDATE_FREQUENCY) {
|
||||||
if (isAlligned(position)) {
|
chooseDirection(pacman, map);
|
||||||
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 = new Point(
|
Point newPosition = getNewPosition();
|
||||||
position.x + direction.dx,
|
|
||||||
position.y + direction.dy);
|
|
||||||
log.debug("Next position {}", newPosition);
|
|
||||||
|
|
||||||
Point destination = collisionChecker.canMoveTo(direction, newPosition);
|
move(newPosition);
|
||||||
|
|
||||||
if (destination != null) {
|
|
||||||
position = destination;
|
|
||||||
}
|
|
||||||
movementTick = 0;
|
movementTick = 0;
|
||||||
} else movementTick++;
|
} else movementTick++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a position and a direction - calculate the new position
|
||||||
|
*
|
||||||
|
* @return new position
|
||||||
|
*/
|
||||||
|
private Point getNewPosition() {
|
||||||
|
Point point = new Point(
|
||||||
|
position.x + direction.dx,
|
||||||
|
position.y + direction.dy);
|
||||||
|
log.debug("Next position {}", point);
|
||||||
|
return point;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
||||||
|
Point destination = collisionChecker.canMoveTo(direction, newPosition);
|
||||||
|
|
||||||
|
if (destination != null) {
|
||||||
|
position = destination;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Map<Direction, Integer> prioritize(List<Direction> directions) {
|
private Map<Direction, Integer> prioritize(List<Direction> directions) {
|
||||||
return directions.stream()
|
return directions.stream()
|
||||||
.filter(d -> d != Direction.NONE)
|
.filter(d -> d != Direction.NONE)
|
||||||
|
|||||||
Reference in New Issue
Block a user