diff behaviours/base.py @ 103:57f1cff9a75d

Added animation queue and method the base behaviour class.
author KarstenBock@gmx.net
date Fri, 30 Sep 2011 14:04:29 +0200
parents a9cc5559ec2a
children 4c11d9d8b19d
line wrap: on
line diff
--- a/behaviours/base.py	Thu Sep 29 18:15:12 2011 +0200
+++ b/behaviours/base.py	Fri Sep 30 14:04:29 2011 +0200
@@ -13,6 +13,8 @@
 #   You should have received a copy of the GNU General Public License
 #   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
 
+from collections import deque
+
 from fife import fife
 
 _AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6)
@@ -23,6 +25,8 @@
         fife.InstanceActionListener.__init__(self)
         self.agent = None
         self.state = None
+        self.animation_queue = deque()
+        self.nextAction = None 
     
     def attachToLayer(self, agent_ID, layer):
         """Attaches to a certain layer
@@ -58,7 +62,7 @@
     
     def idle(self):
         """@return: None"""
-        self.state = _AGENT_STATE_IDLE
+        self.state = _AGENT_STATE_IDLE        
         
     def onInstanceActionFinished(self, instance, action):
         """@type instance: ???
@@ -73,6 +77,11 @@
         
         if act:
             act.execute()
+        try:
+            animtion = self.animation_queue.popleft()
+            self.animate(**animtion)
+        except IndexError:
+            self.idle()
           
             
     def getLocation(self):
@@ -87,5 +96,19 @@
            @return: None"""
         self.state = _AGENT_STATE_TALK
         self.pc = pc.behaviour.agent
+        self.clear_animations()
         self.idle()
-        
\ No newline at end of file
+        
+    def animate(self, action, direction = None, repeating = False):
+        """Perform an animation"""
+        direction = direction or self.agent.getFacingLocation()
+        self.agent.act(action, direction, repeating)
+        
+    def queue_animation(self, action, direction = None, repeating = False):
+        """Add an action to the queue"""
+        self.animation_queue.append({"action": action, "direction": direction,
+                                  "repeating": repeating})
+        
+    def clear_animations(self):
+        """Remove all actions from the queue"""
+        self.animation_queue.clear()