Added Scatter-strategy
This commit is contained in:
@ -1,12 +1,15 @@
|
||||
package se.urmo.game.state;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import se.urmo.game.collision.GhostCollisionChecker;
|
||||
import se.urmo.game.entities.BlinkyStrategy;
|
||||
import se.urmo.game.entities.Ghost;
|
||||
import se.urmo.game.entities.GhostMode;
|
||||
import se.urmo.game.entities.PacMan;
|
||||
import se.urmo.game.entities.PinkyStrategy;
|
||||
import se.urmo.game.entities.ScatterToTopRight;
|
||||
import se.urmo.game.map.GameMap;
|
||||
import se.urmo.game.util.LoadSave;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
@ -14,21 +17,34 @@ import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class GhostManager {
|
||||
public static final int SPRITE_SHEET_ROWS = 10;
|
||||
public static final int MAX_SPRITE_FRAMES = 4;
|
||||
@Getter
|
||||
private final List<Ghost> ghosts = new ArrayList<>();
|
||||
private BufferedImage[][] image;
|
||||
private GhostMode globalMode = GhostMode.CHASE;
|
||||
private GhostMode globalMode;
|
||||
|
||||
private long lastModeSwitchTime;
|
||||
private int phaseIndex = 0;
|
||||
|
||||
// cycle in milliseconds: {scatter, chase, scatter, chase, ...}
|
||||
private final int[] cycleDurations = {
|
||||
7000, 20000, // scatter 7s, chase 20s
|
||||
7000, 20000, // scatter 7s, chase 20s
|
||||
5000, 20000, // scatter 5s, chase 20s
|
||||
5000, Integer.MAX_VALUE // scatter 5s, then chase forever
|
||||
};
|
||||
|
||||
public GhostManager(GhostCollisionChecker ghostCollisionChecker) {
|
||||
loadAnimation();
|
||||
// Create ghosts with their strategies
|
||||
//ghosts.add(new Ghost(ghostCollisionChecker, new BlinkyStrategy(),image[0]));
|
||||
//ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(), image[1]));
|
||||
ghosts.add(new Ghost(ghostCollisionChecker, new BlinkyStrategy(),new ScatterToTopLeft(), image[0]));
|
||||
ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(),new ScatterToTopRight(), image[1]));
|
||||
//ghosts.add(new Ghost(240, 200, new InkyStrategy(), loader.getSprite("inky")));
|
||||
//ghosts.add(new Ghost(260, 200, new ClydeStrategy(), loader.getSprite("clyde")));
|
||||
setMode(GhostMode.CHASE);
|
||||
}
|
||||
|
||||
private void loadAnimation() {
|
||||
@ -44,14 +60,28 @@ public class GhostManager {
|
||||
|
||||
public void setMode(GhostMode mode) {
|
||||
this.globalMode = mode;
|
||||
log.debug("Mode changed to {}", globalMode);
|
||||
for (Ghost g : ghosts) {
|
||||
g.setMode(mode);
|
||||
}
|
||||
}
|
||||
|
||||
public void update(PacMan pacman) {
|
||||
public void update(PacMan pacman, GameMap map) {
|
||||
long now = System.currentTimeMillis();
|
||||
if (phaseIndex < cycleDurations.length) {
|
||||
int duration = cycleDurations[phaseIndex];
|
||||
if (now - lastModeSwitchTime >= duration) {
|
||||
phaseIndex++;
|
||||
if (phaseIndex < cycleDurations.length) {
|
||||
GhostMode newMode = (phaseIndex % 2 == 0) ? GhostMode.SCATTER : GhostMode.CHASE;
|
||||
setMode(newMode);
|
||||
}
|
||||
lastModeSwitchTime = now;
|
||||
}
|
||||
}
|
||||
|
||||
for (Ghost g : ghosts) {
|
||||
g.update(pacman);
|
||||
g.update(pacman, map);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user