From f21ab509ec52e0418ecadf1e5207a4d2769a048b Mon Sep 17 00:00:00 2001 From: fireclawthefox Date: Sun, 23 Feb 2020 14:54:09 +0100 Subject: [PATCH] FSM: support "any" state transitions Closes #344 --- direct/src/fsm/FSM.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/direct/src/fsm/FSM.py b/direct/src/fsm/FSM.py index eb67f55c0a..8236b64a75 100644 --- a/direct/src/fsm/FSM.py +++ b/direct/src/fsm/FSM.py @@ -153,6 +153,12 @@ class FSM(DirectObject): # must be approved by some filter function. defaultTransitions = None + # An enum class for special states like the DEFAULT or ANY state, + # that should be treatened by the FSM in a special way + class EnumStates(): + ANY = 1 + DEFAULT = 2 + def __init__(self, name): self.fsmLock = RLock() self._name = name @@ -382,6 +388,27 @@ class FSM(DirectObject): # accept it. return (request,) + args + elif FSM.EnumStates.ANY in self.defaultTransitions.get(self.state, []): + # Whenever we have a '*' as our to transition, we allow + # to transit to any other state + return (request,) + args + + elif request in self.defaultTransitions.get(FSM.EnumStates.ANY, []): + # If the requested state is in the default transitions + # from any state list, we also alow to transit to the + # new state + return (request,) + args + + elif FSM.EnumStates.ANY in self.defaultTransitions.get(FSM.EnumStates.ANY, []): + # This is like we had set the defaultTransitions to None. + # Any state can transit to any other state + return (request,) + args + + elif request in self.defaultTransitions.get(FSM.EnumStates.DEFAULT, []): + # This is the fallback state that we use whenever no + # other trnasition was possible + return (request,) + args + # If self.defaultTransitions is not None, it is an error # to request a direct state transition (capital letter # request) not listed in defaultTransitions and not