Reworked pacman position. Now centers in the middle of the sprite

This commit is contained in:
Urban Modig
2025-08-18 13:21:54 +02:00
parent e62776b616
commit 64dcba2584
2 changed files with 28 additions and 19 deletions

View File

@ -18,21 +18,16 @@ public class CollisionChecker {
} }
public Point getValidDestination(Direction direction, Point position, int agent_width, int agent_height) { public Point getValidDestination(Direction direction, Point position, int agent_width, int agent_height) {
List<Point> boundaries = switch (direction) { List<Point> boundaries = switch (direction) {
case RIGHT -> List.of( case NONE -> Collections.emptyList();
new Point(position.x + agent_width, position.y), // TOPRIGHT case RIGHT, LEFT -> List.of(
new Point(position.x + agent_width, position.y + agent_height)); // BOTTOMRIGHT new Point(position.x + (direction.dx * agent_width/2), position.y - agent_height/2),
case LEFT -> List.of( new Point(position.x + (direction.dx * agent_width/2), position.y + agent_height/2)
position, // TOPLEFT );
new Point(position.x, position.y + agent_height)); // BOTTOMLEFT case UP, DOWN -> List.of(
case UP -> List.of( new Point(position.x - agent_width/2, position.y + (direction.dy * agent_height/2)),
position, // TOPLEFT new Point(position.x + agent_width/2, position.y + (direction.dy * agent_height/2))
new Point(position.x + agent_width, position.y)); // TOPRIGHT );
case DOWN -> List.of(
new Point(position.x, position.y + agent_height), // BOTTOMLEFT
new Point(position.x + agent_width, position.y + agent_height)); // BOTTOMRIGHT
default -> Collections.EMPTY_LIST;
}; };
log.debug("{} boundaries for {} are {}", direction, position, boundaries); log.debug("{} boundaries for {} are {}", direction, position, boundaries);
@ -47,6 +42,16 @@ public class CollisionChecker {
return null; // Blocked return null; // Blocked
} }
/**
* Applies specific rules to movement
* This for instance makes sure the tunnel left/right works.
*
* @param dir
* @param pos
* @param agent_width
* @param agent_height
* @return
*/
public Point normalizePosition(Direction dir, Point pos, int agent_width, int agent_height) { public Point normalizePosition(Direction dir, Point pos, int agent_width, int agent_height) {
int x = pos.x; int x = pos.x;
int y = pos.y; int y = pos.y;
@ -54,7 +59,7 @@ public class CollisionChecker {
int height = map.getHeight(); int height = map.getHeight();
// tunnel // tunnel
if (x < GameMap.OFFSET_X) x = width - agent_width - GameMap.OFFSET_X; // right if (x < GameMap.OFFSET_X) x = width - agent_width/2 - GameMap.OFFSET_X; // right
if (x >= (width - GameMap.OFFSET_X)) x = GameMap.OFFSET_X; // left if (x >= (width - GameMap.OFFSET_X)) x = GameMap.OFFSET_X; // left
return new Point(x, y); return new Point(x, y);

View File

@ -38,7 +38,9 @@ public class PacMan {
public PacMan(Game game, CollisionChecker collisionChecker) { public PacMan(Game game, CollisionChecker collisionChecker) {
this.game = game; this.game = game;
this.collisionChecker = collisionChecker; this.collisionChecker = collisionChecker;
position = new Point(26 * GameMap.MAP_TILESIZE + 8 + GameMap.OFFSET_X, 13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y); position = new Point(
26 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X,
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + (GameMap.MAP_TILESIZE / 2));
loadAnimation(); loadAnimation();
} }
@ -67,11 +69,13 @@ public class PacMan {
public void draw(Graphics g) { public void draw(Graphics g) {
g.drawImage( g.drawImage(
movmentImages[direction==Direction.NONE?0:direction.ordinal()][aniIndex], movmentImages[direction==Direction.NONE?0:direction.ordinal()][aniIndex],
position.x - COLLISION_BOX_OFFSET, position.x - PACMAN_SIZE / 2,
position.y - COLLISION_BOX_OFFSET, position.y - PACMAN_SIZE / 2,
PACMAN_SIZE, PACMAN_SIZE,
PACMAN_SIZE, null); PACMAN_SIZE, null);
g.drawImage(COLLISION_BOX, position.x, position.y, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, null); g.drawImage(COLLISION_BOX, position.x - COLLISION_BOX_OFFSET, position.y - COLLISION_BOX_OFFSET, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, null);
g.setColor(Color.BLUE);
g.fillRect(position.x-1, position.y-1, 3, 3);
} }
public void update() { public void update() {