From b9a43be3c6057ad23acdcfb8824e5971433f3407 Mon Sep 17 00:00:00 2001 From: Urban Modig Date: Fri, 22 Aug 2025 11:00:19 +0200 Subject: [PATCH] Basic score count working update takes movement into account --- .../urmo/game/collision/CollisionChecker.java | 4 --- .../se/urmo/game/entities/BlinkyStrategy.java | 2 +- .../se/urmo/game/entities/ClydeStrategy.java | 2 +- .../java/se/urmo/game/entities/Ghost.java | 2 +- .../se/urmo/game/entities/InkyStrategy.java | 2 +- .../java/se/urmo/game/entities/PacMan.java | 33 +++++++++---------- .../se/urmo/game/entities/PinkyStrategy.java | 2 +- src/main/java/se/urmo/game/map/GameMap.java | 30 +++++++++++++---- .../java/se/urmo/game/state/PlayingState.java | 14 ++++++-- 9 files changed, 55 insertions(+), 36 deletions(-) diff --git a/src/main/java/se/urmo/game/collision/CollisionChecker.java b/src/main/java/se/urmo/game/collision/CollisionChecker.java index f94dab0..c8303b7 100644 --- a/src/main/java/se/urmo/game/collision/CollisionChecker.java +++ b/src/main/java/se/urmo/game/collision/CollisionChecker.java @@ -65,8 +65,4 @@ public class CollisionChecker { return new Point(x, y); } - - public void removeTile(Point destination) { - map.removeTileImage(destination); - } } diff --git a/src/main/java/se/urmo/game/entities/BlinkyStrategy.java b/src/main/java/se/urmo/game/entities/BlinkyStrategy.java index 5d8236e..09d9e9b 100644 --- a/src/main/java/se/urmo/game/entities/BlinkyStrategy.java +++ b/src/main/java/se/urmo/game/entities/BlinkyStrategy.java @@ -7,6 +7,6 @@ import java.awt.Point; public class BlinkyStrategy implements GhostStrategy { @Override public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) { - return pacman.getTilePosition(); + return pacman.getPosition(); } } diff --git a/src/main/java/se/urmo/game/entities/ClydeStrategy.java b/src/main/java/se/urmo/game/entities/ClydeStrategy.java index 781f43e..2aaec5d 100644 --- a/src/main/java/se/urmo/game/entities/ClydeStrategy.java +++ b/src/main/java/se/urmo/game/entities/ClydeStrategy.java @@ -7,7 +7,7 @@ import java.awt.Point; public class ClydeStrategy implements GhostStrategy { @Override public Point chooseTarget(Ghost clyde, PacMan pacman, GameMap map) { - Point pacTile = pacman.getTilePosition(); + Point pacTile = pacman.getPosition(); Point clydeTile = clyde.getPosition(); // ghost’s current tile double distance = pacTile.distance(clydeTile); diff --git a/src/main/java/se/urmo/game/entities/Ghost.java b/src/main/java/se/urmo/game/entities/Ghost.java index af25d7c..752d202 100644 --- a/src/main/java/se/urmo/game/entities/Ghost.java +++ b/src/main/java/se/urmo/game/entities/Ghost.java @@ -96,7 +96,7 @@ public class Ghost { Point point = new Point( position.x + direction.dx, position.y + direction.dy); - log.debug("Next position {}", point); + //log.debug("Next position {}", point); return point; } diff --git a/src/main/java/se/urmo/game/entities/InkyStrategy.java b/src/main/java/se/urmo/game/entities/InkyStrategy.java index 7b874f6..93f2c9e 100644 --- a/src/main/java/se/urmo/game/entities/InkyStrategy.java +++ b/src/main/java/se/urmo/game/entities/InkyStrategy.java @@ -15,7 +15,7 @@ public class InkyStrategy implements GhostStrategy { public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) { // 1. Two tiles ahead of pacman Direction pacmanDir = pacman.getDirection(); - Point pacmanPos = pacman.getTilePosition(); + 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); diff --git a/src/main/java/se/urmo/game/entities/PacMan.java b/src/main/java/se/urmo/game/entities/PacMan.java index 37a18d7..c8dd0f8 100644 --- a/src/main/java/se/urmo/game/entities/PacMan.java +++ b/src/main/java/se/urmo/game/entities/PacMan.java @@ -30,6 +30,7 @@ public class PacMan { @Setter private boolean moving; private final BufferedImage[][] movmentImages = new BufferedImage[4][4]; + @Getter private Point position; private static final BufferedImage COLLISION_BOX = MiscUtil.createOutlinedBox(COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, Color.yellow, 2); private final CollisionChecker collisionChecker; @@ -82,21 +83,21 @@ public class PacMan { public void update() { updateAnimationTick(); - if(direction == Direction.NONE) return; + //if(direction == Direction.NONE) return; + if(moving) { + Point newPosition = switch (direction) { + case RIGHT -> new Point(position.x + speed, position.y); + case LEFT -> new Point(position.x - speed, position.y); + case UP -> new Point(position.x, position.y - speed); + case DOWN -> new Point(position.x, position.y + speed); + default -> throw new IllegalStateException("Unexpected value: " + direction); + }; + log.debug("At: {},trying to move {} to {}", position, direction.name(), newPosition); + Point destination = collisionChecker.getValidDestination(direction, newPosition, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE); - Point newPosition = switch (direction){ - case RIGHT -> new Point(position.x + speed, position.y); - case LEFT -> new Point(position.x - speed, position.y); - case UP -> new Point(position.x , position.y - speed); - case DOWN -> new Point(position.x, position.y + speed); - default -> throw new IllegalStateException("Unexpected value: " + direction); - }; - log.debug("At: {},trying to move {} to {}", position, direction.name(), newPosition); - Point destination = collisionChecker.getValidDestination(direction, newPosition, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE); - - if(destination != null) { - collisionChecker.removeTile(destination); - position = destination; + if (destination != null) { + position = destination; + } } } @@ -114,10 +115,6 @@ public class PacMan { } } - public Point getTilePosition() { - return position; - } - public double distanceTo(Point point) { return position.distance(point); } diff --git a/src/main/java/se/urmo/game/entities/PinkyStrategy.java b/src/main/java/se/urmo/game/entities/PinkyStrategy.java index bc16a42..a2770dc 100644 --- a/src/main/java/se/urmo/game/entities/PinkyStrategy.java +++ b/src/main/java/se/urmo/game/entities/PinkyStrategy.java @@ -9,7 +9,7 @@ public class PinkyStrategy implements GhostStrategy{ @Override public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) { Direction pacmanDir = pacman.getDirection(); - Point pacmanPos = pacman.getTilePosition(); + Point pacmanPos = pacman.getPosition(); return 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); diff --git a/src/main/java/se/urmo/game/map/GameMap.java b/src/main/java/se/urmo/game/map/GameMap.java index 070b8b7..056cb44 100644 --- a/src/main/java/se/urmo/game/map/GameMap.java +++ b/src/main/java/se/urmo/game/map/GameMap.java @@ -176,15 +176,30 @@ public class GameMap { if (col >= columns() || col < 0 ) return true; MapTile mapTile = mapData[row][col]; boolean solid = mapTile.isSolid(); - log.debug("[{}][{}] is {} ({})", row, col, solid ? "solid" : " not solid", mapTile.getValue()); + // log.debug("[{}][{}] is {} ({})", row, col, solid ? "solid" : " not solid", mapTile.getValue()); return solid; } - public void removeTileImage(Point destination) { - int row = screenToRow(destination); - int col = screenToCol(destination); + public boolean removeTileImage(Point screen) { + if (screen == null || mapData == null) { + return false; + } + + int row = screenToRow(screen); + int col = screenToCol(screen); + + if (row < 0 || row >= mapData.length || col < 0 || col >= mapData[0].length) { + return false; + } + MapTile tile = mapData[row][col]; - if(tile.getValue() == 0) tile.setImage(null); + if (tile != null && tile.getValue() == 0 && tile.getImage() != null) { + tile.setImage(null); + return true; + } + + return false; + } private static int screenToCol(Point point) { @@ -230,11 +245,12 @@ public class GameMap { return new Point(screen.x - OFFSET_X, screen.y - OFFSET_Y); } - private static int screenToCol(int screenX) { + public static int screenToCol(int screenX) { return (screenX - OFFSET_X) / MAP_TILESIZE; } - private static int screenToRow(int screenY) { + public static int screenToRow(int screenY) { return (screenY - OFFSET_Y) / MAP_TILESIZE; } + } diff --git a/src/main/java/se/urmo/game/state/PlayingState.java b/src/main/java/se/urmo/game/state/PlayingState.java index fa8a2e5..7d94e02 100644 --- a/src/main/java/se/urmo/game/state/PlayingState.java +++ b/src/main/java/se/urmo/game/state/PlayingState.java @@ -1,9 +1,9 @@ package se.urmo.game.state; import lombok.Getter; +import lombok.extern.slf4j.Slf4j; import se.urmo.game.collision.CollisionChecker; import se.urmo.game.collision.GhostCollisionChecker; -import se.urmo.game.entities.BlinkyStrategy; import se.urmo.game.entities.Ghost; import se.urmo.game.entities.PacMan; import se.urmo.game.main.Game; @@ -14,6 +14,7 @@ import java.awt.*; import java.awt.event.KeyEvent; import java.io.InputStream; +@Slf4j public class PlayingState implements GameState { private final Game game; private final GameStateManager gameStateManager; @@ -39,6 +40,16 @@ public class PlayingState implements GameState { pacman.update(); ghostManager.update(pacman, map); checkCollisions(); + handleDots(); + } + + private void handleDots() { + Point pacmanScreenPos = pacman.getPosition(); + boolean wasRemoved = map.removeTileImage(pacmanScreenPos); + + if(wasRemoved){ + score+=10; + } } @Override @@ -84,7 +95,6 @@ public class PlayingState implements GameState { @Override public void keyReleased(KeyEvent e) { pacman.setMoving(false); - pacman.setDirection(Direction.NONE); } private void checkCollisions() {