intersection works
This commit is contained in:
@ -2,7 +2,6 @@ package se.urmo.game.entities;
|
||||
|
||||
import se.urmo.game.collision.GhostCollisionChecker;
|
||||
import se.urmo.game.main.Game;
|
||||
import se.urmo.game.main.GamePanel;
|
||||
import se.urmo.game.map.GameMap;
|
||||
import se.urmo.game.util.Direction;
|
||||
import se.urmo.game.util.LoadSave;
|
||||
@ -12,6 +11,7 @@ import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Point;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.List;
|
||||
|
||||
public class Ghost {
|
||||
private static final int COLLISION_BOX_SIZE = 16;
|
||||
@ -24,8 +24,8 @@ public class Ghost {
|
||||
private static final BufferedImage COLLISION_BOX = MiscUtil.createOutlinedBox(COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, Color.black, 2);
|
||||
|
||||
private final Game game;
|
||||
private final GhostCollisionChecker ghostCollisionChecker;
|
||||
private final Point position;
|
||||
private final GhostCollisionChecker collisionChecker;
|
||||
private Point position;
|
||||
|
||||
private boolean moving = true;
|
||||
private int aniTick = 0;
|
||||
@ -35,9 +35,9 @@ public class Ghost {
|
||||
private int movementTick = 0;
|
||||
|
||||
|
||||
public Ghost(Game game, GhostCollisionChecker ghostCollisionChecker) {
|
||||
public Ghost(Game game, GhostCollisionChecker collisionChecker, GhostStrategy strategy) {
|
||||
this.game = game;
|
||||
this.ghostCollisionChecker = ghostCollisionChecker;
|
||||
this.collisionChecker = collisionChecker;
|
||||
position = new Point(13 * 16 + 8 + GameMap.OFFSET_X, 4 * 16 + GameMap.OFFSET_Y);
|
||||
loadAnimation();
|
||||
}
|
||||
@ -67,16 +67,57 @@ public class Ghost {
|
||||
public void update() {
|
||||
updateAnimationTick();
|
||||
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;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
position.x += direction.dx * GHOST_SPEED;
|
||||
//Point target = strategy.chooseTarget(pacman, this, blinky);
|
||||
|
||||
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();
|
||||
|
||||
if(destination != null) {
|
||||
position = destination;
|
||||
}
|
||||
movementTick = 0;
|
||||
} else movementTick++;
|
||||
}
|
||||
|
||||
private Direction chooseDirection(List<Direction> options, Point target) {
|
||||
Direction best = options.get(0);
|
||||
double bestDist = Double.MAX_VALUE;
|
||||
|
||||
for (Direction d : options) {
|
||||
int nx = position.x + d.dx * GameMap.MAP_TILESIZE;
|
||||
int ny = position.y + d.dy * GameMap.MAP_TILESIZE;
|
||||
double dist = target.distance(nx, ny);
|
||||
if (dist < bestDist) {
|
||||
bestDist = dist;
|
||||
best = d;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
private void updateAnimationTick() {
|
||||
if (moving) {
|
||||
aniTick++;
|
||||
|
||||
Reference in New Issue
Block a user