annotate src/parpg/behaviours/base.py @ 144:a6b25c2cedab

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