Refactor Ghost mode handling to prioritize mode transitions
Reordered GhostMode enum for logical priority and updated `setMode` to enforce transitions only to higher-priority modes. Added `currentMode` method for determining the Ghost's active mode, ensuring cleaner and safer state management.
This commit is contained in:
@ -88,8 +88,11 @@ public class Ghost extends BaseAnimated {
|
||||
}
|
||||
|
||||
public void setMode(GhostMode mode) {
|
||||
GhostMode currentMode = currentMode();
|
||||
if (currentMode == null || mode.ordinal() < currentMode.ordinal()) { // only if new mode has higher prio
|
||||
currentState = states.get(mode);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isFrightened() {
|
||||
return states.get(GhostMode.FRIGHTENED) == currentState;
|
||||
@ -99,6 +102,14 @@ public class Ghost extends BaseAnimated {
|
||||
return states.get(GhostMode.EATEN) == currentState;
|
||||
}
|
||||
|
||||
public GhostMode currentMode() {
|
||||
return states.entrySet().stream()
|
||||
.filter(s -> s.getValue() == currentState)
|
||||
.map(Map.Entry::getKey)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
currentState = states.get(GhostMode.CHASE);
|
||||
((ChaseGhostMode) currentState).resetPosition();
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
package se.urmo.game.entities.ghost.mode;
|
||||
|
||||
public enum GhostMode {
|
||||
CHASE,
|
||||
SCATTER,
|
||||
FRIGHTENED,
|
||||
EATEN,
|
||||
HOUSE,
|
||||
FROZEN
|
||||
FROZEN,
|
||||
CHASE,
|
||||
SCATTER
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user