Mercurial > parpg-source
annotate behaviours/base.py @ 159:1b66e1ce226b
script commands are stored in the System now instead of the the script object.
author | KarstenBock@gmx.net |
---|---|
date | Sun, 13 Nov 2011 13:37:24 +0100 |
parents | 4c11d9d8b19d |
children |
rev | line source |
---|---|
86
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
1 # This file is part of PARPG. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
2 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
3 # PARPG is free software: you can redistribute it and/or modify |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
4 # it under the terms of the GNU General Public License as published by |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
5 # the Free Software Foundation, either version 3 of the License, or |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
6 # (at your option) any later version. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
7 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
8 # PARPG is distributed in the hope that it will be useful, |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
11 # GNU General Public License for more details. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
12 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
13 # You should have received a copy of the GNU General Public License |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
15 |
103
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
16 from collections import deque |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
17 |
86
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
18 from fife import fife |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
19 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
20 _AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
21 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
22 class BaseBehaviour (fife.InstanceActionListener): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
23 """Fife agent listener""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
24 def __init__(self): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
25 fife.InstanceActionListener.__init__(self) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
26 self.agent = None |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
27 self.state = None |
103
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
28 self.animation_queue = deque() |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
29 self.nextAction = None |
86
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
30 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
31 def attachToLayer(self, agent_ID, layer): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
32 """Attaches to a certain layer |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
33 @type agent_ID: String |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
34 @param agent_ID: ID of the layer to attach to. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
35 @type layer: Fife layer |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
36 @param layer: Layer of the agent to attach the behaviour to |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
37 @return: None""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
38 self.agent = layer.getInstance(agent_ID) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
39 self.agent.addActionListener(self) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
40 self.state = _AGENT_STATE_NONE |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
41 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
42 def getX(self): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
43 """Get the NPC's x position on the map. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
44 @rtype: integer" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
45 @return: the x coordinate of the NPC's location""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
46 return self.agent.getLocation().getLayerCoordinates().x |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
47 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
48 def getY(self): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
49 """Get the NPC's y position on the map. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
50 @rtype: integer |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
51 @return: the y coordinate of the NPC's location""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
52 return self.agent.getLocation().getLayerCoordinates().y |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
53 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
54 def onNewMap(self, layer): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
55 """Sets the agent onto the new layer.""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
56 if self.agent is not None: |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
57 self.agent.removeActionListener(self) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
58 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
59 self.agent = layer.getInstance(self.parent.general.identifier) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
60 self.agent.addActionListener(self) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
61 self.state = _AGENT_STATE_NONE |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
62 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
63 def idle(self): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
64 """@return: None""" |
103
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
65 self.state = _AGENT_STATE_IDLE |
86
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
66 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
67 def onInstanceActionFinished(self, instance, action): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
68 """@type instance: ??? |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
69 @param instance: ??? |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
70 @type action: ??? |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
71 @param action: ??? |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
72 @return: None""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
73 # First we reset the next behavior |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
74 act = self.nextAction |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
75 self.nextAction = None |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
76 self.idle() |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
77 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
78 if act: |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
79 act.execute() |
103
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
80 try: |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
81 animtion = self.animation_queue.popleft() |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
82 self.animate(**animtion) |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
83 except IndexError: |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
84 self.idle() |
86
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
85 |
151
4c11d9d8b19d
Added onInstanceActionFrame to BaseBehaviour. This fixes the "blinking Objects" problem.
KarstenBock@gmx.net
parents:
103
diff
changeset
|
86 def onInstanceActionFrame(self, instance, action, frame): |
4c11d9d8b19d
Added onInstanceActionFrame to BaseBehaviour. This fixes the "blinking Objects" problem.
KarstenBock@gmx.net
parents:
103
diff
changeset
|
87 pass |
4c11d9d8b19d
Added onInstanceActionFrame to BaseBehaviour. This fixes the "blinking Objects" problem.
KarstenBock@gmx.net
parents:
103
diff
changeset
|
88 |
86
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
89 def getLocation(self): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
90 """Get the agents position as a fife.Location object. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
91 @rtype: fife.Location |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
92 @return: the location of the agent""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
93 return self.agent.getLocation() |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
94 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
95 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
96 def talk(self, pc): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
97 """Makes the agent ready to talk to the PC |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
98 @return: None""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
99 self.state = _AGENT_STATE_TALK |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
100 self.pc = pc.behaviour.agent |
103
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
101 self.clear_animations() |
86
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
102 self.idle() |
103
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
103 |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
104 def animate(self, action, direction = None, repeating = False): |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
105 """Perform an animation""" |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
106 direction = direction or self.agent.getFacingLocation() |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
107 self.agent.act(action, direction, repeating) |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
108 |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
109 def queue_animation(self, action, direction = None, repeating = False): |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
110 """Add an action to the queue""" |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
111 self.animation_queue.append({"action": action, "direction": direction, |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
112 "repeating": repeating}) |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
113 |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
114 def clear_animations(self): |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
115 """Remove all actions from the queue""" |
57f1cff9a75d
Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents:
86
diff
changeset
|
116 self.animation_queue.clear() |