Added HOUSE mode

This commit is contained in:
Urban Modig
2025-09-06 00:12:08 +02:00
parent d551b464b1
commit e299a4173e
6 changed files with 46 additions and 18 deletions

View File

@ -11,11 +11,10 @@ import se.urmo.game.entities.ghost.mode.FrightenedGhostMode;
import se.urmo.game.entities.ghost.mode.FrozenGhostMode; import se.urmo.game.entities.ghost.mode.FrozenGhostMode;
import se.urmo.game.entities.ghost.mode.GhostMode; import se.urmo.game.entities.ghost.mode.GhostMode;
import se.urmo.game.entities.ghost.mode.GhostState; import se.urmo.game.entities.ghost.mode.GhostState;
import se.urmo.game.entities.ghost.mode.HouseGhostMode;
import se.urmo.game.entities.ghost.mode.ScatterGhostMode; import se.urmo.game.entities.ghost.mode.ScatterGhostMode;
import se.urmo.game.entities.ghost.strategy.GhostStrategy; import se.urmo.game.entities.ghost.strategy.GhostStrategy;
import se.urmo.game.entities.pacman.PacMan; import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.graphics.SpriteLocation;
import se.urmo.game.graphics.SpriteSheetManager;
import se.urmo.game.main.GhostManager; import se.urmo.game.main.GhostManager;
import se.urmo.game.main.LevelManager; import se.urmo.game.main.LevelManager;
import se.urmo.game.map.GameMap; import se.urmo.game.map.GameMap;
@ -23,7 +22,6 @@ import se.urmo.game.util.Direction;
import se.urmo.game.util.MyPoint; import se.urmo.game.util.MyPoint;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;
@ -36,8 +34,6 @@ public class Ghost extends BaseAnimated {
@Getter @Getter
private final GhostCollisionChecker collisionChecker; private final GhostCollisionChecker collisionChecker;
@Getter @Getter
private final BufferedImage[] baseAnimation;
@Getter
private final int animation; private final int animation;
@Getter @Getter
private final LevelManager levelManager; private final LevelManager levelManager;
@ -61,29 +57,24 @@ public class Ghost extends BaseAnimated {
@Getter @Getter
private static final MyPoint startPosition = new MyPoint( private static final MyPoint startPosition = new MyPoint(
GameMap.colToScreen(13) + ((double) GameMap.MAP_TILESIZE / 2), GameMap.colToScreen(13) + ((double) GameMap.MAP_TILESIZE / 2),
GameMap.rowToScreen(4) + ((double) GameMap.MAP_TILESIZE / 2)); GameMap.rowToScreen(12) + ((double) GameMap.MAP_TILESIZE / 2));
;
public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy chaseStrategy, GhostStrategy scaterStrategy, int animation, LevelManager levelManager) { public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy chaseStrategy, GhostStrategy scaterStrategy, int animation, LevelManager levelManager) {
super(ANIMATION_UPDATE_FREQUENCY, GhostManager.MAX_SPRITE_FRAMES); super(ANIMATION_UPDATE_FREQUENCY, GhostManager.MAX_SPRITE_FRAMES);
this.collisionChecker = collisionChecker; this.collisionChecker = collisionChecker;
this.scaterStrategy = scaterStrategy; this.scaterStrategy = scaterStrategy;
this.baseAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(animation);
this.animation = animation; this.animation = animation;
this.levelManager = levelManager; this.levelManager = levelManager;
this.position = startPosition;
states.put(GhostMode.CHASE, new ChaseGhostMode(this, chaseStrategy)); states.put(GhostMode.CHASE, new ChaseGhostMode(this, chaseStrategy));
states.put(GhostMode.SCATTER, new ScatterGhostMode(this, scaterStrategy)); states.put(GhostMode.SCATTER, new ScatterGhostMode(this, scaterStrategy));
states.put(GhostMode.FRIGHTENED, new FrightenedGhostMode(this)); states.put(GhostMode.FRIGHTENED, new FrightenedGhostMode(this));
states.put(GhostMode.EATEN, new EatenGhostMode(this)); states.put(GhostMode.EATEN, new EatenGhostMode(this));
states.put(GhostMode.FROZEN, new FrozenGhostMode(this)); states.put(GhostMode.FROZEN, new FrozenGhostMode(this));
states.put(GhostMode.HOUSE, new HouseGhostMode(this));
reset();
} }
public void draw(Graphics g) { public void draw(Graphics g) {
MyPoint position = currentState.getPosition();
g.drawImage( g.drawImage(
currentState.getAnimation()[aniIndex], currentState.getAnimation()[aniIndex],
(int) position.x - GHOST_SIZE / 2, (int) position.x - GHOST_SIZE / 2,
@ -98,7 +89,6 @@ public class Ghost extends BaseAnimated {
public void setMode(GhostMode mode) { public void setMode(GhostMode mode) {
currentState = states.get(mode); currentState = states.get(mode);
//currentState.enter(this);
} }
public boolean isFrightened() { public boolean isFrightened() {
@ -111,7 +101,6 @@ public class Ghost extends BaseAnimated {
public void reset() { public void reset() {
currentState = states.get(GhostMode.CHASE); currentState = states.get(GhostMode.CHASE);
//currentState.enter(this);
((ChaseGhostMode) currentState).resetPosition(); ((ChaseGhostMode) currentState).resetPosition();
} }
} }

View File

@ -5,5 +5,6 @@ public enum GhostMode {
SCATTER, SCATTER,
FRIGHTENED, FRIGHTENED,
EATEN, EATEN,
HOUSE,
FROZEN FROZEN
} }

View File

@ -10,7 +10,6 @@ import java.awt.image.BufferedImage;
public interface GhostState { public interface GhostState {
void update(Ghost ghost, PacMan pacman, GameMap map); void update(Ghost ghost, PacMan pacman, GameMap map);
//void enter(Ghost ghost);
MyPoint getPosition(); MyPoint getPosition();

View File

@ -0,0 +1,26 @@
package se.urmo.game.entities.ghost.mode;
import lombok.extern.slf4j.Slf4j;
import se.urmo.game.entities.ghost.Ghost;
import se.urmo.game.entities.ghost.strategy.HouseStrategy;
import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.map.GameMap;
import java.awt.Point;
@Slf4j
public class HouseGhostMode extends AbstractGhostModeImpl {
public HouseGhostMode(Ghost ghost) {
super(ghost, new HouseStrategy(), ghost.getLevelManager(), ghost.getAnimation());
}
@Override
public void update(Ghost ghost, PacMan pacman, GameMap map) {
if (getPosition().asPoint().distance(new Point(232, 154)) < 1) {
log.debug("Ghost left home, switching to chase mode");
ghost.setMode(GhostMode.CHASE);
return;
}
updatePosition(ghost, pacman, map);
}
}

View File

@ -0,0 +1,14 @@
package se.urmo.game.entities.ghost.strategy;
import se.urmo.game.entities.ghost.Ghost;
import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.map.GameMap;
import java.awt.Point;
public class HouseStrategy implements GhostStrategy {
@Override
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
return new Point(232, 154);
}
}

View File

@ -48,11 +48,10 @@ public class GhostManager {
ghosts.forEach(animationManager::register); ghosts.forEach(animationManager::register);
setMode(GhostMode.CHASE); setMode(GhostMode.HOUSE);
} }
public void setMode(GhostMode mode) { public void setMode(GhostMode mode) {
log.debug("Mode changed to {}", mode);
for (Ghost g : ghosts) { for (Ghost g : ghosts) {
g.setMode(mode); g.setMode(mode);
} }