annotate behaviours/base.py @ 137:53faff63037f

createItem now calls UpdateObjectDB after it created the item.
author KarstenBock@gmx.net
date Sun, 09 Oct 2011 14:03:25 +0200
parents 57f1cff9a75d
children 4c11d9d8b19d
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
a9cc5559ec2a Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 61
diff changeset
86
a9cc5559ec2a Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 61
diff changeset
87 def getLocation(self):
a9cc5559ec2a Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 61
diff changeset
88 """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
89 @rtype: fife.Location
a9cc5559ec2a Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 61
diff changeset
90 @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
91 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
92
a9cc5559ec2a Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 61
diff changeset
93
a9cc5559ec2a Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 61
diff changeset
94 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
95 """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
96 @return: None"""
a9cc5559ec2a Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 61
diff changeset
97 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
98 self.pc = pc.behaviour.agent
103
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
99 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
100 self.idle()
103
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
101
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
102 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
103 """Perform an animation"""
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
104 direction = direction or self.agent.getFacingLocation()
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
105 self.agent.act(action, direction, repeating)
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
106
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
107 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
108 """Add an action to the queue"""
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
109 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
110 "repeating": repeating})
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
111
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
112 def clear_animations(self):
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
113 """Remove all actions from the queue"""
57f1cff9a75d Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 86
diff changeset
114 self.animation_queue.clear()