Minor fixes

This commit is contained in:
Urban Modig
2025-09-04 23:12:16 +02:00
parent c475d3cb02
commit ebbc82b70e
6 changed files with 12 additions and 15 deletions

View File

@ -1,5 +1,6 @@
package se.urmo.game.entities.ghost; package se.urmo.game.entities.ghost;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import se.urmo.game.collision.GhostCollisionChecker; import se.urmo.game.collision.GhostCollisionChecker;
import se.urmo.game.entities.BaseAnimated; import se.urmo.game.entities.BaseAnimated;
@ -39,6 +40,7 @@ public class Ghost extends BaseAnimated {
private final LevelManager levelManager; private final LevelManager levelManager;
private static final BufferedImage[] eatenAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(9); private static final BufferedImage[] eatenAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(9);
private double speed; private double speed;
@Getter
private MyPoint position; private MyPoint position;
private final GhostStrategy scaterStrategy; private final GhostStrategy scaterStrategy;
@ -236,10 +238,6 @@ public class Ghost extends BaseAnimated {
this.speed = BASE_SPEED * levelManager.getGhostSpeed(); this.speed = BASE_SPEED * levelManager.getGhostSpeed();
} }
public Point getPosition() {
return new Point((int) position.x, (int) position.y);
}
public boolean isEaten() { public boolean isEaten() {
return mode == GhostMode.EATEN; return mode == GhostMode.EATEN;
} }

View File

@ -1,7 +1,7 @@
package se.urmo.game.entities.ghost.strategy; package se.urmo.game.entities.ghost.strategy;
import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.entities.ghost.Ghost; import se.urmo.game.entities.ghost.Ghost;
import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.map.GameMap; import se.urmo.game.map.GameMap;
import java.awt.Point; import java.awt.Point;
@ -10,7 +10,7 @@ public class ClydeStrategy implements GhostStrategy {
@Override @Override
public Point chooseTarget(Ghost clyde, PacMan pacman, GameMap map) { public Point chooseTarget(Ghost clyde, PacMan pacman, GameMap map) {
Point pacTile = pacman.getPosition(); Point pacTile = pacman.getPosition();
Point clydeTile = clyde.getPosition(); // ghosts current tile Point clydeTile = clyde.getPosition().asPoint(); // ghosts current tile
double distance = pacTile.distance(clydeTile); double distance = pacTile.distance(clydeTile);

View File

@ -1,7 +1,7 @@
package se.urmo.game.entities.ghost.strategy; package se.urmo.game.entities.ghost.strategy;
import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.entities.ghost.Ghost; import se.urmo.game.entities.ghost.Ghost;
import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.map.GameMap; import se.urmo.game.map.GameMap;
import se.urmo.game.util.Direction; import se.urmo.game.util.Direction;
@ -16,18 +16,18 @@ public class FearStrategy implements GhostStrategy {
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) { public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
// Frightened ghosts do not target Pacman. // Frightened ghosts do not target Pacman.
// Instead, they pick a random adjacent valid tile. // Instead, they pick a random adjacent valid tile.
Point ghostPos = ghost.getPosition(); Point ghostPos = ghost.getPosition().asPoint();
List<Direction> neighbors = map.directionAlternatives(ghostPos.x, ghostPos.y); List<Direction> neighbors = map.directionAlternatives(ghostPos.x, ghostPos.y);
if (neighbors.isEmpty()) { if (neighbors.isEmpty()) {
return ghost.getPosition(); // stuck return ghost.getPosition().asPoint(); // stuck
} }
//Transform directions to actual Points //Transform directions to actual Points
List<Point> potentialTargets = neighbors.stream() List<Point> potentialTargets = neighbors.stream()
.map(d -> new Point( .map(d -> new Point(
ghost.getPosition().x + d.dx * GameMap.MAP_TILESIZE, (int) (ghost.getPosition().x + d.dx * GameMap.MAP_TILESIZE),
ghost.getPosition().y + d.dy * GameMap.MAP_TILESIZE)).toList(); (int) (ghost.getPosition().y + d.dy * GameMap.MAP_TILESIZE))).toList();
// Pick a random valid neighbor // Pick a random valid neighbor
return potentialTargets.get(random.nextInt(neighbors.size())); return potentialTargets.get(random.nextInt(neighbors.size()));

View File

@ -1,7 +1,7 @@
package se.urmo.game.entities.ghost.strategy; package se.urmo.game.entities.ghost.strategy;
import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.entities.ghost.Ghost; import se.urmo.game.entities.ghost.Ghost;
import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.map.GameMap; import se.urmo.game.map.GameMap;
import se.urmo.game.util.Direction; import se.urmo.game.util.Direction;
@ -27,7 +27,7 @@ public class InkyStrategy implements GhostStrategy {
}; };
// 2. Vector from blinky to that tile // 2. Vector from blinky to that tile
Point blinkyPos = blinky.getPosition(); Point blinkyPos = blinky.getPosition().asPoint();
int vx = ahead.x - blinkyPos.x; int vx = ahead.x - blinkyPos.x;
int vy = ahead.y - blinkyPos.y; int vy = ahead.y - blinkyPos.y;

View File

@ -44,7 +44,6 @@ public class PacMan extends BaseAnimated {
private BufferedImage[] deathFrames; // working copy private BufferedImage[] deathFrames; // working copy
private long lastChangeNs; private long lastChangeNs;
// animation state // animation state
@Setter
private PacmanState state = PacmanState.ALIVE; private PacmanState state = PacmanState.ALIVE;
private int deathFrameIdx = 0; private int deathFrameIdx = 0;
private double speed; private double speed;

View File

@ -211,7 +211,7 @@ public class PlayingState implements GameState {
for (Ghost ghost : ghostManager.getGhosts()) { for (Ghost ghost : ghostManager.getGhosts()) {
if (deathInProgress) return; // guard if (deathInProgress) return; // guard
//if(overlap(pacman, ghost) //if(overlap(pacman, ghost)
double dist = pacman.distanceTo(ghost.getPosition()); double dist = pacman.distanceTo(ghost.getPosition().asPoint());
if (dist < GameMap.MAP_TILESIZE / 2.0) { if (dist < GameMap.MAP_TILESIZE / 2.0) {
if (ghost.isEaten()) return; if (ghost.isEaten()) return;
if (ghost.isFrightened()) { if (ghost.isFrightened()) {