annotate src/parpg/behaviours/moving.py @ 190:a22e92090018

The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
author KarstenBock@gmx.net
date Sat, 12 Nov 2011 20:54:25 +0100
parents ecac92680bef
children
rev   line source
133
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
1 # This file is part of PARPG.
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
2
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
3 # PARPG is free software: you can redistribute it and/or modify
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
4 # it under the terms of the GNU General Public License as published by
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
6 # (at your option) any later version.
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
7
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
8 # PARPG is distributed in the hope that it will be useful,
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
11 # GNU General Public License for more details.
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
12
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
13 # You should have received a copy of the GNU General Public License
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
15
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
16 from fife import fife
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
17 import base
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
18 from base import BaseBehaviour
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
19
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
20 class MovingAgentBehaviour (BaseBehaviour):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
21 """Fife agent listener"""
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
22 def __init__(self):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
23 BaseBehaviour.__init__(self)
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
24 self.speed = 0
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
25 self.idle_counter = 1
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
26
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
27 def onNewMap(self, layer):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
28 """Sets the agent onto the new layer."""
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
29 BaseBehaviour.onNewMap(self, layer)
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
30 self.idle_counter = 1
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
31
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
32
190
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
33 def approach(self, location_or_agent, action=None):
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
34 """Approaches a location or another agent and then perform an action
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
35 (if set).
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
36 @type loc: fife.Location
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
37 @param loc: the location or agent to approach
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
38 @type action: Action
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
39 @param action: The action to schedule for execution after the
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
40 approach.
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
41 @return: None"""
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
42
133
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
43 self.state = base._AGENT_STATE_APPROACH
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
44 self.nextAction = action
190
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
45 if isinstance(location_or_agent, fife.Instance):
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
46 agent = location_or_agent
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
47 self.agent.follow('run', agent, self.speed + 1)
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
48 else:
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
49 location = location_or_agent
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
50 boxLocation = tuple([int(float(i)) for i in location])
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
51 l = fife.Location(self.getLocation())
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
52 l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
53 self.agent.move('run', l, self.speed + 1)
133
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
54
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
55 def onInstanceActionFinished(self, instance, action):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
56 """@type instance: ???
190
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
57 @param instance: ???
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
58 @type action: ???
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
59 @param action: ???
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
60 @return: None"""
133
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
61 BaseBehaviour.onInstanceActionFinished(self, instance, action)
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
62
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
63 if(action.getId() != 'stand'):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
64 self.idle_counter = 1
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
65 else:
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
66 self.idle_counter += 1
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
67
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
68 def idle(self):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
69 """@return: None"""
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
70 BaseBehaviour.idle(self)
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
71 self.animate('stand')
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
72
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
73 def run(self, location):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
74 """Makes the PC run to a certain location
190
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
75 @type location: fife.ScreenPoint
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
76 @param location: Screen position to run to.
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
77 @return: None"""
133
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
78 self.state = base._AGENT_STATE_RUN
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
79 self.clear_animations()
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
80 self.nextAction = None
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
81 self.agent.move('run', location, self.speed + 1)
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
82
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
83 def walk(self, location):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
84 """Makes the PC walk to a certain location.
190
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
85 @type location: fife.ScreenPoint
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
86 @param location: Screen position to walk to.
a22e92090018 The approach method of MovingAgentBehaviour now accepts a locatior or another instance, and uses the follow method to move to the instance position.
KarstenBock@gmx.net
parents: 133
diff changeset
87 @return: None"""
133
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
88 self.state = base._AGENT_STATE_RUN
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
89 self.clear_animations()
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
90 self.nextAction = None
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 88
diff changeset
91 self.agent.move('walk', location, self.speed - 1)
82
7cb53edfb95f Renamed BaseBehaviour to MovingAgentBehaviour
KarstenBock@gmx.net
parents:
diff changeset
92