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,7 +88,10 @@ public class Ghost extends BaseAnimated {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setMode(GhostMode mode) {
|
public void setMode(GhostMode mode) {
|
||||||
currentState = states.get(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() {
|
public boolean isFrightened() {
|
||||||
@ -99,6 +102,14 @@ public class Ghost extends BaseAnimated {
|
|||||||
return states.get(GhostMode.EATEN) == currentState;
|
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() {
|
public void reset() {
|
||||||
currentState = states.get(GhostMode.CHASE);
|
currentState = states.get(GhostMode.CHASE);
|
||||||
((ChaseGhostMode) currentState).resetPosition();
|
((ChaseGhostMode) currentState).resetPosition();
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
package se.urmo.game.entities.ghost.mode;
|
package se.urmo.game.entities.ghost.mode;
|
||||||
|
|
||||||
public enum GhostMode {
|
public enum GhostMode {
|
||||||
CHASE,
|
|
||||||
SCATTER,
|
|
||||||
FRIGHTENED,
|
FRIGHTENED,
|
||||||
EATEN,
|
EATEN,
|
||||||
HOUSE,
|
HOUSE,
|
||||||
FROZEN
|
FROZEN,
|
||||||
|
CHASE,
|
||||||
|
SCATTER
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user