43 lines
1.4 KiB
Java
43 lines
1.4 KiB
Java
package se.urmo.game.entities;
|
|
|
|
import se.urmo.game.map.GameMap;
|
|
import se.urmo.game.util.Direction;
|
|
|
|
import java.awt.Point;
|
|
|
|
public class InkyStrategy implements GhostStrategy {
|
|
private final Ghost blinky;
|
|
|
|
public InkyStrategy(Ghost blinky) {
|
|
this.blinky = blinky;
|
|
}
|
|
@Override
|
|
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
|
|
// 1. Two tiles ahead of pacman
|
|
Direction pacmanDir = pacman.getDirection();
|
|
Point pacmanPos = pacman.getPosition();
|
|
Point ahead = switch (pacmanDir){
|
|
case RIGHT -> new Point(pacmanPos.x + 8 * GameMap.MAP_TILESIZE, pacmanPos.y);
|
|
case LEFT -> new Point(pacmanPos.x - 8 * GameMap.MAP_TILESIZE, pacmanPos.y);
|
|
case DOWN -> new Point(pacmanPos.x, pacmanPos.y + 8 * GameMap.MAP_TILESIZE);
|
|
case UP -> new Point(pacmanPos.x, pacmanPos.y - 8 * GameMap.MAP_TILESIZE);
|
|
case NONE -> pacmanPos;
|
|
};
|
|
|
|
// 2. Vector from blinky to that tile
|
|
Point blinkyPos = blinky.getPosition();
|
|
int vx = ahead.x - blinkyPos.x;
|
|
int vy = ahead.y - blinkyPos.y;
|
|
|
|
// 3. Double the vector
|
|
Point target = new Point(
|
|
blinkyPos.x + 2 * vx,
|
|
blinkyPos.y + 2 * vy
|
|
);
|
|
|
|
return target;
|
|
// (Optional: clamp inside map boundaries)
|
|
//return map.clampToBounds(target);
|
|
}
|
|
}
|