Minot pacman-changes
This commit is contained in:
@ -31,6 +31,7 @@ public class PacMan extends BaseAnimated {
|
|||||||
private static final int ANIMATION_UPDATE_FREQUENCY = 10;
|
private static final int ANIMATION_UPDATE_FREQUENCY = 10;
|
||||||
private static final double BASE_SPEED = 0.40;
|
private static final double BASE_SPEED = 0.40;
|
||||||
private static final long FRAME_NS = 80_000_000L; // 80 ms
|
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 MyPoint startPosition;
|
||||||
private final CollisionChecker collisionChecker;
|
private final CollisionChecker collisionChecker;
|
||||||
private final LevelManager levelManager;
|
private final LevelManager levelManager;
|
||||||
@ -40,7 +41,6 @@ public class PacMan extends BaseAnimated {
|
|||||||
@Setter
|
@Setter
|
||||||
@Getter
|
@Getter
|
||||||
private Direction direction = Direction.NONE;
|
private Direction direction = Direction.NONE;
|
||||||
private final double pacmanLevelSpeed;
|
|
||||||
private BufferedImage[] deathFrames; // working copy
|
private BufferedImage[] deathFrames; // working copy
|
||||||
private long lastChangeNs;
|
private long lastChangeNs;
|
||||||
// animation state
|
// animation state
|
||||||
@ -49,7 +49,7 @@ public class PacMan extends BaseAnimated {
|
|||||||
private int deathFrameIdx = 0;
|
private int deathFrameIdx = 0;
|
||||||
|
|
||||||
public PacMan(CollisionChecker collisionChecker, LevelManager levelManager) {
|
public PacMan(CollisionChecker collisionChecker, LevelManager levelManager) {
|
||||||
super(ANIMATION_UPDATE_FREQUENCY, 4);
|
super(ANIMATION_UPDATE_FREQUENCY, PACMAN_SPRITE_FRAMES);
|
||||||
this.collisionChecker = collisionChecker;
|
this.collisionChecker = collisionChecker;
|
||||||
this.levelManager = levelManager;
|
this.levelManager = levelManager;
|
||||||
this.position = new MyPoint(
|
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));
|
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + ((double) GameMap.MAP_TILESIZE / 2));
|
||||||
this.startPosition = this.position;
|
this.startPosition = this.position;
|
||||||
this.sprites = loadAnimation();
|
this.sprites = loadAnimation();
|
||||||
|
|
||||||
this.pacmanLevelSpeed = this.levelManager.getPacmanLevelSpeed();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Sprites loadAnimation() {
|
private Sprites loadAnimation() {
|
||||||
BufferedImage[][] spriteMap = new BufferedImage[6][4];
|
BufferedImage[][] spriteMap = new BufferedImage[6][PACMAN_SPRITE_FRAMES];
|
||||||
BufferedImage[] deathFrames;
|
BufferedImage[] deathFrames;
|
||||||
|
|
||||||
BufferedImage[] animation = SpriteSheetManager.get(SpriteLocation.PACMAN).getAnimation(0);
|
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))
|
.map(i -> LoadSave.rotate(i, Direction.LEFT.angel))
|
||||||
.toArray(BufferedImage[]::new);
|
.toArray(BufferedImage[]::new);
|
||||||
spriteMap[Direction.DOWN.ordinal()] = Arrays.stream(animation)
|
spriteMap[Direction.DOWN.ordinal()] = Arrays.stream(animation)
|
||||||
.map(i -> LoadSave.rotate(i, 90))
|
.map(i -> LoadSave.rotate(i, Direction.DOWN.angel))
|
||||||
.toArray(BufferedImage[]::new);
|
.toArray(BufferedImage[]::new);
|
||||||
spriteMap[Direction.UP.ordinal()] = Arrays.stream(animation)
|
spriteMap[Direction.UP.ordinal()] = Arrays.stream(animation)
|
||||||
.map(i -> LoadSave.rotate(i, 270))
|
.map(i -> LoadSave.rotate(i, Direction.UP.angel))
|
||||||
.toArray(BufferedImage[]::new);
|
.toArray(BufferedImage[]::new);
|
||||||
deathFrames = Stream.concat(
|
deathFrames = Stream.concat(
|
||||||
Arrays.stream(SpriteSheetManager.get(SpriteLocation.PACMAN).getAnimation(1)),
|
Arrays.stream(SpriteSheetManager.get(SpriteLocation.PACMAN).getAnimation(1)),
|
||||||
@ -125,7 +122,7 @@ public class PacMan extends BaseAnimated {
|
|||||||
if (state != PacmanState.DYING) return;
|
if (state != PacmanState.DYING) return;
|
||||||
|
|
||||||
long now = System.nanoTime();
|
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++;
|
deathFrameIdx++;
|
||||||
lastChangeNs += FRAME_NS; // carry over exact cadence
|
lastChangeNs += FRAME_NS; // carry over exact cadence
|
||||||
}
|
}
|
||||||
@ -135,15 +132,7 @@ public class PacMan extends BaseAnimated {
|
|||||||
if (state != PacmanState.ALIVE) return;
|
if (state != PacmanState.ALIVE) return;
|
||||||
|
|
||||||
if (moving) {
|
if (moving) {
|
||||||
MyPoint mpoint = switch (direction) {
|
MyPoint destination = collisionChecker.getValidDestination(direction, getNewPosition(), COLLISION_BOX_SIZE, COLLISION_BOX_SIZE);
|
||||||
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);
|
|
||||||
|
|
||||||
if (destination != null) {
|
if (destination != null) {
|
||||||
position = destination;
|
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() {
|
public void startDeathAnimation() {
|
||||||
log.info("Starting death animation");
|
|
||||||
state = PacmanState.DYING;
|
state = PacmanState.DYING;
|
||||||
deathFrameIdx = 0;
|
deathFrameIdx = 0;
|
||||||
lastChangeNs = System.nanoTime(); // reset stopwatch right now
|
lastChangeNs = System.nanoTime(); // reset stopwatch right now
|
||||||
@ -162,24 +153,16 @@ public class PacMan extends BaseAnimated {
|
|||||||
.toArray(BufferedImage[]::new);
|
.toArray(BufferedImage[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDeathDone() {
|
|
||||||
return state == PacmanState.DEAD;
|
|
||||||
}
|
|
||||||
|
|
||||||
private double getSpeed() {
|
private double getSpeed() {
|
||||||
return BASE_SPEED * pacmanLevelSpeed;
|
return BASE_SPEED * levelManager.getPacmanLevelSpeed();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double distanceTo(Point point) {
|
public double distanceTo(Point point) {
|
||||||
return new Point((int) position.x, (int) position.y).distance(point);
|
return new Point((int) position.x, (int) position.y).distance(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetPosition() {
|
|
||||||
position = startPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
resetPosition();
|
position = startPosition;
|
||||||
aniIndex = 0; // reset animation to start
|
aniIndex = 0; // reset animation to start
|
||||||
state = PacmanState.ALIVE;
|
state = PacmanState.ALIVE;
|
||||||
deathFrameIdx = 0;
|
deathFrameIdx = 0;
|
||||||
@ -206,15 +189,6 @@ public class PacMan extends BaseAnimated {
|
|||||||
paused = !b;
|
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 {
|
private enum PacmanState {
|
||||||
ALIVE, DYING, DEAD
|
ALIVE, DYING, DEAD
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user