Rudimentary colisioncheck

This commit is contained in:
Urban Modig
2025-08-21 15:14:20 +02:00
parent e8112f1cbb
commit 712d58e8e3
3 changed files with 43 additions and 2 deletions

View File

@ -27,6 +27,7 @@ public class Ghost {
private final GhostCollisionChecker collisionChecker; private final GhostCollisionChecker collisionChecker;
private final GhostStrategy chaseStrategy; private final GhostStrategy chaseStrategy;
private final Point startPos;
@Getter @Getter
private Point position; private Point position;
@ -50,7 +51,7 @@ public class Ghost {
position = new Point( position = new Point(
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X + (GameMap.MAP_TILESIZE / 2), 13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X + (GameMap.MAP_TILESIZE / 2),
4 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + (GameMap.MAP_TILESIZE / 2) ); 4 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + (GameMap.MAP_TILESIZE / 2) );
startPos = position;
this.currentStrategy = chaseStrategy; this.currentStrategy = chaseStrategy;
} }
@ -195,4 +196,11 @@ public class Ghost {
} }
} }
public boolean isFrightened() {
return false;
}
public void resetPosition() {
position = startPos;
}
} }

View File

@ -22,6 +22,7 @@ public class PacMan {
private static final int COLLISION_BOX_SIZE = 16; private static final int COLLISION_BOX_SIZE = 16;
private static final int COLLISION_BOX_OFFSET = (PACMAN_SIZE - COLLISION_BOX_SIZE) / 2; private static final int COLLISION_BOX_OFFSET = (PACMAN_SIZE - COLLISION_BOX_SIZE) / 2;
private final Game game; private final Game game;
private final Point startPosition;
private int aniTick = 0; private int aniTick = 0;
private int aniIndex = 0; private int aniIndex = 0;
private static final int ANIMATION_UPDATE_FREQUENCY = 10; private static final int ANIMATION_UPDATE_FREQUENCY = 10;
@ -39,9 +40,10 @@ 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( this.position = new Point(
26 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X, 26 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X,
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + (GameMap.MAP_TILESIZE / 2)); 13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + (GameMap.MAP_TILESIZE / 2));
this.startPosition = this.position;
loadAnimation(); loadAnimation();
} }
@ -115,4 +117,16 @@ public class PacMan {
public Point getTilePosition() { public Point getTilePosition() {
return position; return position;
} }
public double distanceTo(Point point) {
return position.distance(point);
}
public void loseLife() {
}
public void resetPosition() {
position = startPosition;
}
} }

View File

@ -20,6 +20,7 @@ public class PlayingState implements GameState {
private PacMan pacman; private PacMan pacman;
@Getter @Getter
private GameMap map; private GameMap map;
private int score;
public PlayingState(Game game, GameStateManager gameStateManager) { public PlayingState(Game game, GameStateManager gameStateManager) {
this.game = game; this.game = game;
@ -33,6 +34,7 @@ public class PlayingState implements GameState {
public void update() { public void update() {
pacman.update(); pacman.update();
ghostManager.update(pacman, map); ghostManager.update(pacman, map);
checkCollisions();
} }
@Override @Override
@ -62,4 +64,21 @@ public class PlayingState implements GameState {
pacman.setMoving(false); pacman.setMoving(false);
pacman.setDirection(Direction.NONE); pacman.setDirection(Direction.NONE);
} }
private void checkCollisions() {
for (Ghost ghost : ghostManager.getGhosts()) {
double dist = pacman.distanceTo(ghost.getPosition());
if (dist < GameMap.MAP_TILESIZE / 2.0) {
if (ghost.isFrightened()) {
// Pac-Man eats ghost
score += 200;
ghost.resetPosition();
} else {
// Pac-Man loses a life
pacman.loseLife();
pacman.resetPosition();
}
}
}
}
} }