Minot pacman-changes

This commit is contained in:
Urban Modig
2025-09-03 00:27:44 +02:00
parent 4638484b97
commit c05398201f

View File

@ -31,6 +31,7 @@ public class PacMan extends BaseAnimated {
private static final int ANIMATION_UPDATE_FREQUENCY = 10;
private static final double BASE_SPEED = 0.40;
private static final long FRAME_NS = 80_000_000L; // 80 ms
public static final int PACMAN_SPRITE_FRAMES = 4;
private final MyPoint startPosition;
private final CollisionChecker collisionChecker;
private final LevelManager levelManager;
@ -40,7 +41,6 @@ public class PacMan extends BaseAnimated {
@Setter
@Getter
private Direction direction = Direction.NONE;
private final double pacmanLevelSpeed;
private BufferedImage[] deathFrames; // working copy
private long lastChangeNs;
// animation state
@ -49,7 +49,7 @@ public class PacMan extends BaseAnimated {
private int deathFrameIdx = 0;
public PacMan(CollisionChecker collisionChecker, LevelManager levelManager) {
super(ANIMATION_UPDATE_FREQUENCY, 4);
super(ANIMATION_UPDATE_FREQUENCY, PACMAN_SPRITE_FRAMES);
this.collisionChecker = collisionChecker;
this.levelManager = levelManager;
this.position = new MyPoint(
@ -57,13 +57,10 @@ public class PacMan extends BaseAnimated {
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + ((double) GameMap.MAP_TILESIZE / 2));
this.startPosition = this.position;
this.sprites = loadAnimation();
this.pacmanLevelSpeed = this.levelManager.getPacmanLevelSpeed();
}
private Sprites loadAnimation() {
BufferedImage[][] spriteMap = new BufferedImage[6][4];
BufferedImage[][] spriteMap = new BufferedImage[6][PACMAN_SPRITE_FRAMES];
BufferedImage[] deathFrames;
BufferedImage[] animation = SpriteSheetManager.get(SpriteLocation.PACMAN).getAnimation(0);
@ -72,10 +69,10 @@ public class PacMan extends BaseAnimated {
.map(i -> LoadSave.rotate(i, Direction.LEFT.angel))
.toArray(BufferedImage[]::new);
spriteMap[Direction.DOWN.ordinal()] = Arrays.stream(animation)
.map(i -> LoadSave.rotate(i, 90))
.map(i -> LoadSave.rotate(i, Direction.DOWN.angel))
.toArray(BufferedImage[]::new);
spriteMap[Direction.UP.ordinal()] = Arrays.stream(animation)
.map(i -> LoadSave.rotate(i, 270))
.map(i -> LoadSave.rotate(i, Direction.UP.angel))
.toArray(BufferedImage[]::new);
deathFrames = Stream.concat(
Arrays.stream(SpriteSheetManager.get(SpriteLocation.PACMAN).getAnimation(1)),
@ -125,7 +122,7 @@ public class PacMan extends BaseAnimated {
if (state != PacmanState.DYING) return;
long now = System.nanoTime();
while (now - lastChangeNs >= FRAME_NS && deathFrameIdx < sprites.deathFrames.length - 1) {
while (now - lastChangeNs >= FRAME_NS && deathFrameIdx < sprites.deathFrames.length - 1) { // FRAME_NS has passed and not all frames has been drawn
deathFrameIdx++;
lastChangeNs += FRAME_NS; // carry over exact cadence
}
@ -135,15 +132,7 @@ public class PacMan extends BaseAnimated {
if (state != PacmanState.ALIVE) return;
if (moving) {
MyPoint mpoint = switch (direction) {
case RIGHT -> new MyPoint(position.x + getSpeed(), position.y);
case LEFT -> new MyPoint(position.x - getSpeed(), position.y);
case UP -> new MyPoint(position.x, position.y - getSpeed());
case DOWN -> new MyPoint(position.x, position.y + getSpeed());
default -> throw new IllegalStateException("Unexpected value: " + direction);
};
MyPoint destination = collisionChecker.getValidDestination(direction, mpoint, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE);
MyPoint destination = collisionChecker.getValidDestination(direction, getNewPosition(), COLLISION_BOX_SIZE, COLLISION_BOX_SIZE);
if (destination != null) {
position = destination;
@ -151,9 +140,11 @@ public class PacMan extends BaseAnimated {
}
}
// called by PlayingState when collision (non-frightened)
private MyPoint getNewPosition() {
return new MyPoint(position.x + direction.dx * getSpeed(), position.y + direction.dy * getSpeed());
}
public void startDeathAnimation() {
log.info("Starting death animation");
state = PacmanState.DYING;
deathFrameIdx = 0;
lastChangeNs = System.nanoTime(); // reset stopwatch right now
@ -162,24 +153,16 @@ public class PacMan extends BaseAnimated {
.toArray(BufferedImage[]::new);
}
public boolean isDeathDone() {
return state == PacmanState.DEAD;
}
private double getSpeed() {
return BASE_SPEED * pacmanLevelSpeed;
return BASE_SPEED * levelManager.getPacmanLevelSpeed();
}
public double distanceTo(Point point) {
return new Point((int) position.x, (int) position.y).distance(point);
}
public void resetPosition() {
position = startPosition;
}
public void reset() {
resetPosition();
position = startPosition;
aniIndex = 0; // reset animation to start
state = PacmanState.ALIVE;
deathFrameIdx = 0;
@ -206,15 +189,6 @@ public class PacMan extends BaseAnimated {
paused = !b;
}
public void playDeathAnimation() {
// g.drawImage(
// spriteSheets[daIndex][aniIndex],
// (int) position.x - PACMAN_OFFSET,
// (int) position.y - PACMAN_OFFSET,
// PACMAN_SIZE,
// PACMAN_SIZE, null);
}
private enum PacmanState {
ALIVE, DYING, DEAD
}