Skip to content

Commit

Permalink
Minor StateMachine refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
nightm4re94 committed Oct 6, 2024
1 parent fbe9a99 commit 57fdf7d
Showing 1 changed file with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.gurkenlabs.litiengine.IUpdateable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* Represents a state machine that manages the states and transitions of an entity.
Expand Down Expand Up @@ -46,14 +47,11 @@ public void update() {
currentState.perform();
List<Transition> transitions = currentState.getTransitions();
Collections.sort(transitions);

for (Transition transition : transitions) {
if (transition.conditionsFullfilled()) {
currentState.exit();
currentState = transition.getNextState();
currentState.enter();
return;
}
Optional<Transition> transition = transitions.stream().filter(Transition::conditionsFullfilled).findFirst();
if (transition.isPresent()) {
currentState.exit();
setState(transition.get().getNextState());
currentState.enter();
}
}
}

0 comments on commit 57fdf7d

Please sign in to comment.