Changed ghost/packman-collision

This commit is contained in:
Urban Modig
2025-09-06 20:42:33 +02:00
parent a8dc81984f
commit ec89422e81
3 changed files with 15 additions and 13 deletions

View File

@ -21,6 +21,7 @@ import se.urmo.game.map.GameMap;
import se.urmo.game.util.Direction;
import se.urmo.game.util.MyPoint;
import java.awt.Color;
import java.awt.Graphics;
import java.util.EnumMap;
import java.util.Map;
@ -47,7 +48,7 @@ public class Ghost extends BaseAnimated {
// Movement-related state
@Getter
@Setter
protected MyPoint position;
protected MyPoint position; // center of sprite
@Getter
@Setter
protected Direction direction;
@ -85,9 +86,10 @@ public class Ghost extends BaseAnimated {
(int) position.y - GHOST_SIZE / 2,
GHOST_SIZE,
GHOST_SIZE, null);
// g.setColor(Color.YELLOW);
g.setColor(Color.YELLOW);
// g.fillRect((int) Ghost.startPosition.x, (int) Ghost.startPosition.y, 2, 2);
// g.fillRect((int) Ghost.houseEntrance.x, (int) Ghost.houseEntrance.y, 2, 2);
// g.fillRect((int) position.x, (int) position.y, 4, 4);
}
public void update(PacMan pacman, GameMap map) {

View File

@ -37,7 +37,7 @@ public class PacMan extends BaseAnimated {
private final LevelManager levelManager;
private final Sprites sprites;
private boolean moving = false;
private MyPoint position;
private MyPoint position; //
@Setter
@Getter
private Direction direction = Direction.NONE;

View File

@ -211,9 +211,9 @@ public class PlayingState implements GameState {
private void checkCollisions() {
for (Ghost ghost : ghostManager.getGhosts()) {
if (deathInProgress) return; // guard
//if(overlap(pacman, ghost)
double dist = pacman.distanceTo(ghost.getPosition().asPoint());
if (dist < GameMap.MAP_TILESIZE / 2.0) {
if (overlaps(pacman, ghost)) {
//double dist = pacman.distanceTo(ghost.getPosition().asPoint());
//if (dist < GameMap.MAP_TILESIZE / 2.0) {
if (ghost.isEaten()) return;
if (ghost.isFrightened()) {
log.debug("Pacman eats ghost");
@ -232,13 +232,13 @@ public class PlayingState implements GameState {
}
}
// private boolean overlaps(PacMan p, Ghost g) {
// // center-distance or AABB; center distance keeps the arcade feel
// double dx = p.getCenterX() - g.getCenterX();
// double dy = p.getCenterY() - g.getCenterY();
// double r = map.getTileSize() * 0.45; // tune threshold
// return (dx*dx + dy*dy) <= r*r;
// }
private boolean overlaps(PacMan p, Ghost g) {
// center-distance or AABB; center distance keeps the arcade feel
double dx = p.getPosition().x - g.getPosition().x;
double dy = p.getPosition().y - g.getPosition().y;
double r = GameMap.MAP_TILESIZE * 1.0; // tune threshold
return (dx * dx + dy * dy) <= r * r;
}
private void endGame() {
gameOverState.setScore(score);