Improved ghost-move - now ghoest doesnt get stuck
This commit is contained in:
@ -5,7 +5,6 @@ import se.urmo.game.map.GameMap;
|
|||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ -23,7 +22,8 @@ public class GhostCollisionChecker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Point canMoveTo(Direction dir, Point pos) {
|
public Point canMoveTo(Direction dir, Point pos) {
|
||||||
Point pp = new Point(pos.x + dir.dx * GameMap.MAP_TILESIZE/2, pos.y + dir.dy * GameMap.MAP_TILESIZE/2);
|
// -1 is because else we endup in next tile
|
||||||
|
Point pp = new Point(pos.x + dir.dx * (GameMap.MAP_TILESIZE/2 - 1), pos.y + dir.dy * (GameMap.MAP_TILESIZE/2 -1) );
|
||||||
|
|
||||||
return ! map.isSolid(pp.x, pp.y) ? pos : null;
|
return ! map.isSolid(pp.x, pp.y) ? pos : null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import java.awt.Graphics;
|
|||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class Ghost {
|
public class Ghost {
|
||||||
@ -35,6 +36,8 @@ public class Ghost {
|
|||||||
private int aniIndex = 0;
|
private int aniIndex = 0;
|
||||||
private BufferedImage[] animation;
|
private BufferedImage[] animation;
|
||||||
private int movementTick = 0;
|
private int movementTick = 0;
|
||||||
|
private Direction direction;
|
||||||
|
private Direction prevDirection;
|
||||||
|
|
||||||
|
|
||||||
public Ghost(Game game, GhostCollisionChecker collisionChecker, GhostStrategy strategy) {
|
public Ghost(Game game, GhostCollisionChecker collisionChecker, GhostStrategy strategy) {
|
||||||
@ -79,26 +82,25 @@ public class Ghost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updatePosition(PacMan pacman) {
|
private void updatePosition(PacMan pacman) {
|
||||||
if(movementTick >= GHOST_MOVEMENT_UPDATE_FREQUENCY) {
|
if (movementTick >= GHOST_MOVEMENT_UPDATE_FREQUENCY) {
|
||||||
if(!isAlligned(position)){
|
if (isAlligned(position)) {
|
||||||
position = align(position);
|
log.info("Evaluating possible directions");
|
||||||
log.debug("Aligned: {}", position);
|
prevDirection = direction;
|
||||||
return;
|
direction = chooseDirection(
|
||||||
|
collisionChecker.calculateDirectionAlternatives(position),
|
||||||
|
strategy.chooseTarget(pacman, null, null),
|
||||||
|
prevDirection);
|
||||||
|
log.info("selecting direction {}", direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("Evaluating possible directions");
|
|
||||||
Direction intendedDirection = chooseDirection(
|
|
||||||
collisionChecker.calculateDirectionAlternatives(position),
|
|
||||||
strategy.chooseTarget(pacman, null, null));
|
|
||||||
|
|
||||||
|
|
||||||
log.info("selecting direction {}", intendedDirection);
|
|
||||||
Point newPosition = new Point(
|
Point newPosition = new Point(
|
||||||
position.x + intendedDirection.dx * GHOST_SPEED,
|
position.x + direction.dx * GHOST_SPEED,
|
||||||
position.y + intendedDirection.dy * GHOST_SPEED);
|
position.y + direction.dy * GHOST_SPEED);
|
||||||
|
log.debug("Next position {}", newPosition);
|
||||||
|
|
||||||
Point destination = collisionChecker.canMoveTo(intendedDirection, newPosition);
|
Point destination = collisionChecker.canMoveTo(direction, newPosition);
|
||||||
if(destination != null) {
|
|
||||||
|
if (destination != null) {
|
||||||
position = destination;
|
position = destination;
|
||||||
}
|
}
|
||||||
movementTick = 0;
|
movementTick = 0;
|
||||||
@ -108,19 +110,18 @@ public class Ghost {
|
|||||||
private boolean isAlligned(Point pos) {
|
private boolean isAlligned(Point pos) {
|
||||||
int row = pos.x % GameMap.MAP_TILESIZE;
|
int row = pos.x % GameMap.MAP_TILESIZE;
|
||||||
int col = pos.y % GameMap.MAP_TILESIZE;
|
int col = pos.y % GameMap.MAP_TILESIZE;
|
||||||
return row == GameMap.MAP_TILESIZE/2 || col == GameMap.MAP_TILESIZE /2;
|
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) {
|
private Direction chooseDirection(List<Direction> options, Point target, Direction prevDirection) {
|
||||||
Direction best = options.getFirst();
|
List<Direction> l = options.stream()
|
||||||
|
// remove any option to go back
|
||||||
|
.filter(d -> !(prevDirection != null && d.equals(prevDirection.opposite())))
|
||||||
|
.toList();
|
||||||
|
Direction best = l.getFirst();
|
||||||
double bestDist = Double.MAX_VALUE;
|
double bestDist = Double.MAX_VALUE;
|
||||||
|
|
||||||
for (Direction d : options) {
|
for (Direction d : l) {
|
||||||
int nx = position.x + d.dx * GameMap.MAP_TILESIZE;
|
int nx = position.x + d.dx * GameMap.MAP_TILESIZE;
|
||||||
int ny = position.y + d.dy * GameMap.MAP_TILESIZE;
|
int ny = position.y + d.dy * GameMap.MAP_TILESIZE;
|
||||||
double dist = target.distance(nx, ny);
|
double dist = target.distance(nx, ny);
|
||||||
|
|||||||
Reference in New Issue
Block a user