Ghost movement working - first rev

This commit is contained in:
Urban Modig
2025-08-17 15:03:54 +02:00
parent 1bbba216d2
commit 05285529c5
10 changed files with 95 additions and 63 deletions

View File

@ -0,0 +1,10 @@
package se.urmo.game.entities;
import java.awt.Point;
public class BlinkyStrategy implements GhostStrategy {
@Override
public Point chooseTarget(PacMan pacman, Ghost self, Ghost blinky) {
return pacman.getTilePosition();
}
}

View File

@ -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;

View File

@ -0,0 +1,7 @@
package se.urmo.game.entities;
import java.awt.Point;
public interface GhostStrategy {
Point chooseTarget(PacMan pacman, Ghost self, Ghost blinky);
}

View File

@ -109,4 +109,8 @@ public class PacMan {
public void setDirection(Direction direction) {
this.direction = direction;
}
public Point getTilePosition() {
return position;
}
}