diff --git a/src/main/java/se/urmo/game/entities/ghost/Ghost.java b/src/main/java/se/urmo/game/entities/ghost/Ghost.java index 7303a88..277c037 100644 --- a/src/main/java/se/urmo/game/entities/ghost/Ghost.java +++ b/src/main/java/se/urmo/game/entities/ghost/Ghost.java @@ -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.GhostMode; 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.strategy.GhostStrategy; 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.LevelManager; import se.urmo.game.map.GameMap; @@ -23,7 +22,6 @@ import se.urmo.game.util.Direction; import se.urmo.game.util.MyPoint; import java.awt.Graphics; -import java.awt.image.BufferedImage; import java.util.EnumMap; import java.util.Map; @@ -36,8 +34,6 @@ public class Ghost extends BaseAnimated { @Getter private final GhostCollisionChecker collisionChecker; @Getter - private final BufferedImage[] baseAnimation; - @Getter private final int animation; @Getter private final LevelManager levelManager; @@ -61,29 +57,24 @@ public class Ghost extends BaseAnimated { @Getter private static final MyPoint startPosition = new MyPoint( 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) { super(ANIMATION_UPDATE_FREQUENCY, GhostManager.MAX_SPRITE_FRAMES); this.collisionChecker = collisionChecker; this.scaterStrategy = scaterStrategy; - this.baseAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(animation); this.animation = animation; this.levelManager = levelManager; + this.position = startPosition; states.put(GhostMode.CHASE, new ChaseGhostMode(this, chaseStrategy)); states.put(GhostMode.SCATTER, new ScatterGhostMode(this, scaterStrategy)); states.put(GhostMode.FRIGHTENED, new FrightenedGhostMode(this)); states.put(GhostMode.EATEN, new EatenGhostMode(this)); states.put(GhostMode.FROZEN, new FrozenGhostMode(this)); - - reset(); + states.put(GhostMode.HOUSE, new HouseGhostMode(this)); } public void draw(Graphics g) { - MyPoint position = currentState.getPosition(); g.drawImage( currentState.getAnimation()[aniIndex], (int) position.x - GHOST_SIZE / 2, @@ -98,7 +89,6 @@ public class Ghost extends BaseAnimated { public void setMode(GhostMode mode) { currentState = states.get(mode); - //currentState.enter(this); } public boolean isFrightened() { @@ -111,7 +101,6 @@ public class Ghost extends BaseAnimated { public void reset() { currentState = states.get(GhostMode.CHASE); - //currentState.enter(this); ((ChaseGhostMode) currentState).resetPosition(); } } diff --git a/src/main/java/se/urmo/game/entities/ghost/mode/GhostMode.java b/src/main/java/se/urmo/game/entities/ghost/mode/GhostMode.java index 0e9892f..d9b0b92 100644 --- a/src/main/java/se/urmo/game/entities/ghost/mode/GhostMode.java +++ b/src/main/java/se/urmo/game/entities/ghost/mode/GhostMode.java @@ -5,5 +5,6 @@ public enum GhostMode { SCATTER, FRIGHTENED, EATEN, + HOUSE, FROZEN } diff --git a/src/main/java/se/urmo/game/entities/ghost/mode/GhostState.java b/src/main/java/se/urmo/game/entities/ghost/mode/GhostState.java index 13cd1b3..09d5a01 100644 --- a/src/main/java/se/urmo/game/entities/ghost/mode/GhostState.java +++ b/src/main/java/se/urmo/game/entities/ghost/mode/GhostState.java @@ -10,7 +10,6 @@ import java.awt.image.BufferedImage; public interface GhostState { void update(Ghost ghost, PacMan pacman, GameMap map); - //void enter(Ghost ghost); MyPoint getPosition(); diff --git a/src/main/java/se/urmo/game/entities/ghost/mode/HouseGhostMode.java b/src/main/java/se/urmo/game/entities/ghost/mode/HouseGhostMode.java new file mode 100644 index 0000000..cf0a820 --- /dev/null +++ b/src/main/java/se/urmo/game/entities/ghost/mode/HouseGhostMode.java @@ -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); + } +} diff --git a/src/main/java/se/urmo/game/entities/ghost/strategy/HouseStrategy.java b/src/main/java/se/urmo/game/entities/ghost/strategy/HouseStrategy.java new file mode 100644 index 0000000..c0c2bcc --- /dev/null +++ b/src/main/java/se/urmo/game/entities/ghost/strategy/HouseStrategy.java @@ -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); + } +} diff --git a/src/main/java/se/urmo/game/main/GhostManager.java b/src/main/java/se/urmo/game/main/GhostManager.java index 6356ebc..a7f60b3 100644 --- a/src/main/java/se/urmo/game/main/GhostManager.java +++ b/src/main/java/se/urmo/game/main/GhostManager.java @@ -48,11 +48,10 @@ public class GhostManager { ghosts.forEach(animationManager::register); - setMode(GhostMode.CHASE); + setMode(GhostMode.HOUSE); } public void setMode(GhostMode mode) { - log.debug("Mode changed to {}", mode); for (Ghost g : ghosts) { g.setMode(mode); }