Mercurial > parpg-source
comparison 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 |
comparison
equal
deleted
inserted
replaced
102:2eb316546eae | 103:57f1cff9a75d |
---|---|
11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
12 | 12 |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. | 14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 |
16 from collections import deque | |
17 | |
16 from fife import fife | 18 from fife import fife |
17 | 19 |
18 _AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6) | 20 _AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6) |
19 | 21 |
20 class BaseBehaviour (fife.InstanceActionListener): | 22 class BaseBehaviour (fife.InstanceActionListener): |
21 """Fife agent listener""" | 23 """Fife agent listener""" |
22 def __init__(self): | 24 def __init__(self): |
23 fife.InstanceActionListener.__init__(self) | 25 fife.InstanceActionListener.__init__(self) |
24 self.agent = None | 26 self.agent = None |
25 self.state = None | 27 self.state = None |
28 self.animation_queue = deque() | |
29 self.nextAction = None | |
26 | 30 |
27 def attachToLayer(self, agent_ID, layer): | 31 def attachToLayer(self, agent_ID, layer): |
28 """Attaches to a certain layer | 32 """Attaches to a certain layer |
29 @type agent_ID: String | 33 @type agent_ID: String |
30 @param agent_ID: ID of the layer to attach to. | 34 @param agent_ID: ID of the layer to attach to. |
56 self.agent.addActionListener(self) | 60 self.agent.addActionListener(self) |
57 self.state = _AGENT_STATE_NONE | 61 self.state = _AGENT_STATE_NONE |
58 | 62 |
59 def idle(self): | 63 def idle(self): |
60 """@return: None""" | 64 """@return: None""" |
61 self.state = _AGENT_STATE_IDLE | 65 self.state = _AGENT_STATE_IDLE |
62 | 66 |
63 def onInstanceActionFinished(self, instance, action): | 67 def onInstanceActionFinished(self, instance, action): |
64 """@type instance: ??? | 68 """@type instance: ??? |
65 @param instance: ??? | 69 @param instance: ??? |
66 @type action: ??? | 70 @type action: ??? |
71 self.nextAction = None | 75 self.nextAction = None |
72 self.idle() | 76 self.idle() |
73 | 77 |
74 if act: | 78 if act: |
75 act.execute() | 79 act.execute() |
80 try: | |
81 animtion = self.animation_queue.popleft() | |
82 self.animate(**animtion) | |
83 except IndexError: | |
84 self.idle() | |
76 | 85 |
77 | 86 |
78 def getLocation(self): | 87 def getLocation(self): |
79 """Get the agents position as a fife.Location object. | 88 """Get the agents position as a fife.Location object. |
80 @rtype: fife.Location | 89 @rtype: fife.Location |
85 def talk(self, pc): | 94 def talk(self, pc): |
86 """Makes the agent ready to talk to the PC | 95 """Makes the agent ready to talk to the PC |
87 @return: None""" | 96 @return: None""" |
88 self.state = _AGENT_STATE_TALK | 97 self.state = _AGENT_STATE_TALK |
89 self.pc = pc.behaviour.agent | 98 self.pc = pc.behaviour.agent |
99 self.clear_animations() | |
90 self.idle() | 100 self.idle() |
91 | 101 |
102 def animate(self, action, direction = None, repeating = False): | |
103 """Perform an animation""" | |
104 direction = direction or self.agent.getFacingLocation() | |
105 self.agent.act(action, direction, repeating) | |
106 | |
107 def queue_animation(self, action, direction = None, repeating = False): | |
108 """Add an action to the queue""" | |
109 self.animation_queue.append({"action": action, "direction": direction, | |
110 "repeating": repeating}) | |
111 | |
112 def clear_animations(self): | |
113 """Remove all actions from the queue""" | |
114 self.animation_queue.clear() |