From 64dcba25847198258127c95fb43639c7d53b8190 Mon Sep 17 00:00:00 2001 From: Urban Modig Date: Mon, 18 Aug 2025 13:21:54 +0200 Subject: [PATCH] Reworked pacman position. Now centers in the middle of the sprite --- .../urmo/game/collision/CollisionChecker.java | 35 +++++++++++-------- .../java/se/urmo/game/entities/PacMan.java | 12 ++++--- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/main/java/se/urmo/game/collision/CollisionChecker.java b/src/main/java/se/urmo/game/collision/CollisionChecker.java index 9092621..b223b2b 100644 --- a/src/main/java/se/urmo/game/collision/CollisionChecker.java +++ b/src/main/java/se/urmo/game/collision/CollisionChecker.java @@ -18,21 +18,16 @@ public class CollisionChecker { } public Point getValidDestination(Direction direction, Point position, int agent_width, int agent_height) { - List boundaries = switch (direction) { - case RIGHT -> List.of( - new Point(position.x + agent_width, position.y), // TOPRIGHT - new Point(position.x + agent_width, position.y + agent_height)); // BOTTOMRIGHT - case LEFT -> List.of( - position, // TOPLEFT - new Point(position.x, position.y + agent_height)); // BOTTOMLEFT - case UP -> List.of( - position, // TOPLEFT - 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; + case NONE -> Collections.emptyList(); + case RIGHT, LEFT -> List.of( + new Point(position.x + (direction.dx * agent_width/2), position.y - agent_height/2), + new Point(position.x + (direction.dx * agent_width/2), position.y + agent_height/2) + ); + case UP, DOWN -> List.of( + new Point(position.x - agent_width/2, position.y + (direction.dy * agent_height/2)), + new Point(position.x + agent_width/2, position.y + (direction.dy * agent_height/2)) + ); }; log.debug("{} boundaries for {} are {}", direction, position, boundaries); @@ -47,6 +42,16 @@ public class CollisionChecker { 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) { int x = pos.x; int y = pos.y; @@ -54,7 +59,7 @@ public class CollisionChecker { int height = map.getHeight(); // 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 return new Point(x, y); diff --git a/src/main/java/se/urmo/game/entities/PacMan.java b/src/main/java/se/urmo/game/entities/PacMan.java index b12f99e..15002f2 100644 --- a/src/main/java/se/urmo/game/entities/PacMan.java +++ b/src/main/java/se/urmo/game/entities/PacMan.java @@ -38,7 +38,9 @@ public class PacMan { public PacMan(Game game, CollisionChecker collisionChecker) { this.game = game; 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(); } @@ -67,11 +69,13 @@ public class PacMan { public void draw(Graphics g) { g.drawImage( movmentImages[direction==Direction.NONE?0:direction.ordinal()][aniIndex], - position.x - COLLISION_BOX_OFFSET, - position.y - COLLISION_BOX_OFFSET, + position.x - PACMAN_SIZE / 2, + position.y - PACMAN_SIZE / 2, PACMAN_SIZE, 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() {