Added mode-management and second ghost
This commit is contained in:
63
src/main/java/se/urmo/game/state/GhostManager.java
Normal file
63
src/main/java/se/urmo/game/state/GhostManager.java
Normal file
@ -0,0 +1,63 @@
|
||||
package se.urmo.game.state;
|
||||
|
||||
import lombok.Getter;
|
||||
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.util.LoadSave;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
|
||||
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(240, 200, new InkyStrategy(), loader.getSprite("inky")));
|
||||
//ghosts.add(new Ghost(260, 200, new ClydeStrategy(), loader.getSprite("clyde")));
|
||||
}
|
||||
|
||||
private void loadAnimation() {
|
||||
image = new BufferedImage[SPRITE_SHEET_ROWS][MAX_SPRITE_FRAMES];
|
||||
|
||||
BufferedImage img = LoadSave.GetSpriteAtlas("sprites/PacManAssets-Ghosts.png");
|
||||
for (int row = 0; row < SPRITE_SHEET_ROWS; row++) {
|
||||
for (int col = 0; col < MAX_SPRITE_FRAMES; col++) {
|
||||
image[row][col] = img.getSubimage(Ghost.GHOST_SIZE * col, Ghost.GHOST_SIZE * row, Ghost.GHOST_SIZE, Ghost.GHOST_SIZE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setMode(GhostMode mode) {
|
||||
this.globalMode = mode;
|
||||
for (Ghost g : ghosts) {
|
||||
g.setMode(mode);
|
||||
}
|
||||
}
|
||||
|
||||
public void update(PacMan pacman) {
|
||||
for (Ghost g : ghosts) {
|
||||
g.update(pacman);
|
||||
}
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g) {
|
||||
for (Ghost ghost : ghosts) {
|
||||
ghost.draw(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user