Browse Source

added ability to have an ordered list of states

Jason Persampieri 21 years ago
parent
commit
79de5d6173
1 changed files with 24 additions and 1 deletions
  1. 24 1
      direct/src/fsm/FSM.py

+ 24 - 1
direct/src/fsm/FSM.py

@@ -227,6 +227,29 @@ class FSM(DirectObject.DirectObject):
         return self.defaultFilter(request, args)
         return self.defaultFilter(request, args)
         
         
 
 
+    def setStateArray(self, stateArray):
+        """array of unique states to iterate through"""
+        self.stateArray = stateArray
+
+    def requestNext(self, *args):
+        """request the 'next' state in the predefined state array"""
+        assert (self.state in self.stateArray)
+
+        curIndex = self.stateArray.index(self.state)
+        newIndex = (curIndex + 1) % len(self.stateArray)
+
+        self.request(self.stateArray[newIndex], args)
+
+    def requestPrev(self, *args):
+        """request the 'previous' state in the predefined state array"""
+        assert (self.state in self.stateArray)
+
+        curIndex = self.stateArray.index(self.state)
+        newIndex = (curIndex - 1) % len(self.stateArray)
+
+        self.request(self.stateArray[newIndex], args)
+        
+
     def __setState(self, newState, *args):
     def __setState(self, newState, *args):
         # Internal function to change unconditionally to the indicated
         # Internal function to change unconditionally to the indicated
         # state.
         # state.
@@ -246,7 +269,7 @@ class FSM(DirectObject.DirectObject):
         # Calls the appropriate enter or exit function when
         # Calls the appropriate enter or exit function when
         # transitioning between states, if it exists.
         # transitioning between states, if it exists.
         assert(self.state == None)
         assert(self.state == None)
-        
+
         func = getattr(self, name, None)
         func = getattr(self, name, None)
         if func:
         if func:
             func(*args)
             func(*args)