Refactored ghost movement - second rev
This commit is contained in:
@ -22,7 +22,7 @@ public class Ghost {
|
||||
|
||||
private static int GHOST_SIZE = 32;
|
||||
private static final int ANIMATION_UPDATE_FREQUENCY = 25;
|
||||
private static final int COLLISION_BOX_OFFSET = (GHOST_SIZE - COLLISION_BOX_SIZE) / 2;
|
||||
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 final Game game;
|
||||
@ -41,7 +41,9 @@ public class Ghost {
|
||||
this.game = game;
|
||||
this.collisionChecker = collisionChecker;
|
||||
this.strategy = strategy;
|
||||
position = new Point(13 * 16 + 8 + GameMap.OFFSET_X, 4 * 16 + GameMap.OFFSET_Y);
|
||||
position = new Point(
|
||||
13 * GameMap.MAP_TILESIZE + (GameMap.MAP_TILESIZE / 2) + GameMap.OFFSET_X,
|
||||
4 * GameMap.MAP_TILESIZE + (GameMap.MAP_TILESIZE / 2) + GameMap.OFFSET_Y);
|
||||
loadAnimation();
|
||||
}
|
||||
|
||||
@ -60,11 +62,15 @@ public class Ghost {
|
||||
public void draw(Graphics g) {
|
||||
g.drawImage(
|
||||
animation[aniIndex],
|
||||
position.x - COLLISION_BOX_OFFSET,
|
||||
position.y - COLLISION_BOX_OFFSET,
|
||||
position.x - GHOST_SIZE / 2,
|
||||
position.y - GHOST_SIZE / 2,
|
||||
GHOST_SIZE,
|
||||
GHOST_SIZE, null);
|
||||
g.drawImage(COLLISION_BOX, position.x, position.y, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, null);
|
||||
g.drawImage(COLLISION_BOX,
|
||||
position.x - COLLISION_BOX_OFFSET,
|
||||
position.y - COLLISION_BOX_OFFSET,
|
||||
COLLISION_BOX_SIZE,
|
||||
COLLISION_BOX_SIZE, null);
|
||||
}
|
||||
|
||||
public void update(PacMan pacman) {
|
||||
@ -74,6 +80,12 @@ public class Ghost {
|
||||
|
||||
private void updatePosition(PacMan pacman) {
|
||||
if(movementTick >= GHOST_MOVEMENT_UPDATE_FREQUENCY) {
|
||||
if(!isAlligned(position)){
|
||||
position = align(position);
|
||||
log.debug("Aligned: {}", position);
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("Evaluating possible directions");
|
||||
Direction intendedDirection = chooseDirection(
|
||||
collisionChecker.calculateDirectionAlternatives(position),
|
||||
@ -82,11 +94,10 @@ public class Ghost {
|
||||
|
||||
log.info("selecting direction {}", intendedDirection);
|
||||
Point newPosition = new Point(
|
||||
position.x += intendedDirection.dx * GHOST_SPEED,
|
||||
position.y += intendedDirection.dy * GHOST_SPEED);
|
||||
|
||||
Point destination = collisionChecker.getValidDestination(intendedDirection, newPosition, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE);
|
||||
position.x + intendedDirection.dx * GHOST_SPEED,
|
||||
position.y + intendedDirection.dy * GHOST_SPEED);
|
||||
|
||||
Point destination = collisionChecker.canMoveTo(intendedDirection, newPosition);
|
||||
if(destination != null) {
|
||||
position = destination;
|
||||
}
|
||||
@ -94,8 +105,19 @@ public class Ghost {
|
||||
} else movementTick++;
|
||||
}
|
||||
|
||||
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 Point align(Point point){
|
||||
int row = point.x / GameMap.MAP_TILESIZE;
|
||||
int col = point.y / GameMap.MAP_TILESIZE;
|
||||
return new Point(row * GameMap.MAP_TILESIZE + GameMap.MAP_TILESIZE /2, col * GameMap.MAP_TILESIZE + GameMap.MAP_TILESIZE /2);
|
||||
}
|
||||
|
||||
private Direction chooseDirection(List<Direction> options, Point target) {
|
||||
Direction best = options.get(0);
|
||||
Direction best = options.getFirst();
|
||||
double bestDist = Double.MAX_VALUE;
|
||||
|
||||
for (Direction d : options) {
|
||||
|
||||
Reference in New Issue
Block a user