Ghost movement working - first rev
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
package se.urmo.game.entities;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import se.urmo.game.collision.GhostCollisionChecker;
|
||||
import se.urmo.game.main.Game;
|
||||
import se.urmo.game.map.GameMap;
|
||||
@ -13,6 +14,7 @@ import java.awt.Point;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class Ghost {
|
||||
private static final int COLLISION_BOX_SIZE = 16;
|
||||
private static final int GHOST_SPEED = 1;
|
||||
@ -25,19 +27,20 @@ public class Ghost {
|
||||
|
||||
private final Game game;
|
||||
private final GhostCollisionChecker collisionChecker;
|
||||
private final GhostStrategy strategy;
|
||||
private Point position;
|
||||
|
||||
private boolean moving = true;
|
||||
private int aniTick = 0;
|
||||
private int aniIndex = 0;
|
||||
private BufferedImage[] animation;
|
||||
private Direction direction = Direction.LEFT;
|
||||
private int movementTick = 0;
|
||||
|
||||
|
||||
public Ghost(Game game, GhostCollisionChecker collisionChecker, GhostStrategy strategy) {
|
||||
this.game = game;
|
||||
this.collisionChecker = collisionChecker;
|
||||
this.strategy = strategy;
|
||||
position = new Point(13 * 16 + 8 + GameMap.OFFSET_X, 4 * 16 + GameMap.OFFSET_Y);
|
||||
loadAnimation();
|
||||
}
|
||||
@ -64,36 +67,25 @@ public class Ghost {
|
||||
g.drawImage(COLLISION_BOX, position.x, position.y, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, null);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
public void update(PacMan pacman) {
|
||||
updateAnimationTick();
|
||||
updatePosition(pacman);
|
||||
}
|
||||
|
||||
private void updatePosition(PacMan pacman) {
|
||||
if(movementTick >= GHOST_MOVEMENT_UPDATE_FREQUENCY) {
|
||||
// if intersection - decide direction
|
||||
// else if direction isPassible
|
||||
List<Direction> i = collisionChecker.isIntersection(position);
|
||||
if(!i.isEmpty()){
|
||||
// Change direction
|
||||
if(i.contains(Direction.DOWN)) direction = Direction.DOWN;
|
||||
else if(i.contains(Direction.RIGHT)) direction = Direction.RIGHT;
|
||||
else if(i.contains(Direction.UP)) direction = Direction.UP;
|
||||
else direction = Direction.LEFT;
|
||||
log.info("Evaluating possible directions");
|
||||
Direction intendedDirection = chooseDirection(
|
||||
collisionChecker.calculateDirectionAlternatives(position),
|
||||
strategy.chooseTarget(pacman, null, null));
|
||||
|
||||
}
|
||||
|
||||
//Point target = strategy.chooseTarget(pacman, this, blinky);
|
||||
log.info("selecting direction {}", intendedDirection);
|
||||
Point newPosition = new Point(
|
||||
position.x += intendedDirection.dx * GHOST_SPEED,
|
||||
position.y += intendedDirection.dy * GHOST_SPEED);
|
||||
|
||||
Point newPosition = switch (direction){
|
||||
case RIGHT -> new Point(position.x += GHOST_SPEED, position.y);
|
||||
case LEFT -> new Point(position.x -= GHOST_SPEED, position.y);
|
||||
case DOWN -> new Point(position.x, position.y += GHOST_SPEED);
|
||||
case UP -> new Point(position.x, position.y -= GHOST_SPEED);
|
||||
default -> throw new IllegalStateException("Illegal direction");
|
||||
};
|
||||
|
||||
Point destination = collisionChecker.getValidDestination(direction, newPosition, GHOST_SIZE, GHOST_SIZE);
|
||||
|
||||
// if (position.x + direction.dx * GHOST_SPEED < GameMap.OFFSET_X) direction = direction.opposite();
|
||||
// if (position.x + GHOST_SIZE + (direction.dx * GHOST_SPEED) > GamePanel.SCREEN_WIDTH - GameMap.OFFSET_X)
|
||||
// direction = direction.opposite();
|
||||
Point destination = collisionChecker.getValidDestination(intendedDirection, newPosition, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE);
|
||||
|
||||
if(destination != null) {
|
||||
position = destination;
|
||||
|
||||
Reference in New Issue
Block a user