annotate behaviours/moving.py @ 55:8b1ad2d342d8

Renamed BaseBehaviour to MovingAgentBehaviour
author KarstenBock@gmx.net
date Fri, 09 Sep 2011 15:05:23 +0200
parents
children 3f6299f975fe
rev   line source
55
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
1 # This file is part of PARPG.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
2
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
3 # PARPG is free software: you can redistribute it and/or modify
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
6 # (at your option) any later version.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
7
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
8 # PARPG is distributed in the hope that it will be useful,
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
11 # GNU General Public License for more details.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
12
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
15
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
16 from fife import fife
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
17
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
18 _AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6)
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
19
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
20 class MovingAgentBehaviour (fife.InstanceActionListener):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
21 """Fife agent listener"""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
22 def __init__(self):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
23 fife.InstanceActionListener.__init__(self)
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
24 self.agent = None
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
25 self.state = None
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
26 self.speed = 0
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
27 self.idle_counter = 1
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
28
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
29 def attachToLayer(self, agent_ID, layer):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
30 """Attaches to a certain layer
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
31 @type agent_ID: String
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
32 @param agent_ID: ID of the layer to attach to.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
33 @type layer: Fife layer
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
34 @param layer: Layer of the agent to attach the behaviour to
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
35 @return: None"""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
36 self.agent = layer.getInstance(agent_ID)
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
37 self.agent.addActionListener(self)
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
38 self.state = _AGENT_STATE_NONE
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
39
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
40 def getX(self):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
41 """Get the NPC's x position on the map.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
42 @rtype: integer"
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
43 @return: the x coordinate of the NPC's location"""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
44 return self.agent.getLocation().getLayerCoordinates().x
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
45
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
46 def getY(self):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
47 """Get the NPC's y position on the map.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
48 @rtype: integer
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
49 @return: the y coordinate of the NPC's location"""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
50 return self.agent.getLocation().getLayerCoordinates().y
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
51
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
52 def onNewMap(self, layer):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
53 """Sets the agent onto the new layer."""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
54 if self.agent is not None:
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
55 self.agent.removeActionListener(self)
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
56
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
57 self.agent = layer.getInstance(self.parent.fifeagent.identifier)
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
58 self.agent.addActionListener(self)
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
59 self.state = _AGENT_STATE_NONE
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
60 self.idle_counter = 1
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
61
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
62 def idle(self):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
63 """@return: None"""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
64 self.state = _AGENT_STATE_IDLE
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
65 self.agent.act('stand', self.agent.getFacingLocation())
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
66
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
67 def approach(self, location, action=None):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
68 """Approaches a location and then perform an action (if set).
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
69 @type loc: fife.Location
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
70 @param loc: the location to approach
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
71 @type action: Action
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
72 @param action: The action to schedule for execution after the approach.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
73 @return: None"""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
74 self.state = _AGENT_STATE_APPROACH
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
75 self.nextAction = action
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
76 boxLocation = tuple([int(float(i)) for i in location])
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
77 l = fife.Location(self.agent.getLocation())
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
78 l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
79 self.agent.move('run', l, self.speed + 1)
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
80
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
81 def onInstanceActionFinished(self, instance, action):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
82 """@type instance: ???
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
83 @param instance: ???
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
84 @type action: ???
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
85 @param action: ???
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
86 @return: None"""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
87 # First we reset the next behavior
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
88 act = self.nextAction
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
89 self.nextAction = None
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
90 self.idle()
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
91
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
92 if act:
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
93 act.execute()
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
94
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
95 if(action.getId() != 'stand'):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
96 self.idle_counter = 1
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
97 else:
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
98 self.idle_counter += 1
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
99
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
100 def getLocation(self):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
101 """Get the agents position as a fife.Location object.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
102 @rtype: fife.Location
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
103 @return: the location of the agent"""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
104 return self.agent.getLocation()
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
105
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
106
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
107 def talk(self, pc):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
108 """Makes the agent ready to talk to the PC
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
109 @return: None"""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
110 self.state = _AGENT_STATE_TALK
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
111 self.pc = pc.behaviour.agent
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
112 self.idle()
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
113
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
114 def run(self, location):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
115 """Makes the PC run to a certain location
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
116 @type location: fife.ScreenPoint
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
117 @param location: Screen position to run to.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
118 @return: None"""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
119 self.state = _AGENT_STATE_RUN
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
120 self.nextAction = None
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
121 self.agent.move('run', location, self.speed + 1)
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
122
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
123 def walk(self, location):
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
124 """Makes the PC walk to a certain location.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
125 @type location: fife.ScreenPoint
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
126 @param location: Screen position to walk to.
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
127 @return: None"""
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
128 self.state = _AGENT_STATE_RUN
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
129 self.nextAction = None
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
130 self.agent.move('walk', location, self.speed - 1)
8b1ad2d342d8 Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
131