Refactor ghost behavior and improve mode transition logic
Replaced hardcoded coordinates with `Ghost.getHouseEntrance` for clarity and flexibility. Introduced `requestModeChange` to streamline state transitions. Updated collision and movement logic to handle ghost-specific tile interactions, enhancing gameplay accuracy.
This commit is contained in:
@ -58,6 +58,10 @@ public class Ghost extends BaseAnimated {
|
||||
private static final MyPoint startPosition = new MyPoint(
|
||||
GameMap.colToScreen(13) + ((double) GameMap.MAP_TILESIZE / 2),
|
||||
GameMap.rowToScreen(12) + ((double) GameMap.MAP_TILESIZE / 2));
|
||||
@Getter
|
||||
private static final MyPoint houseEntrance = new MyPoint(
|
||||
GameMap.colToScreen(13) + ((double) GameMap.MAP_TILESIZE / 2),
|
||||
GameMap.rowToScreen(10) + ((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);
|
||||
@ -81,6 +85,9 @@ public class Ghost extends BaseAnimated {
|
||||
(int) position.y - GHOST_SIZE / 2,
|
||||
GHOST_SIZE,
|
||||
GHOST_SIZE, null);
|
||||
// g.setColor(Color.YELLOW);
|
||||
// g.fillRect((int) Ghost.startPosition.x, (int) Ghost.startPosition.y, 2, 2);
|
||||
// g.fillRect((int) Ghost.houseEntrance.x, (int) Ghost.houseEntrance.y, 2, 2);
|
||||
}
|
||||
|
||||
public void update(PacMan pacman, GameMap map) {
|
||||
@ -94,6 +101,15 @@ public class Ghost extends BaseAnimated {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by a state itself to request a transition to any other state
|
||||
* This bypasses the priority system since the state itself is requesting the change
|
||||
*/
|
||||
public void requestModeChange(GhostMode mode) {
|
||||
currentState = states.get(mode);
|
||||
}
|
||||
|
||||
|
||||
public boolean isFrightened() {
|
||||
return states.get(GhostMode.FRIGHTENED) == currentState;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user