annotate src/parpg/behaviours/moving.py @ 88:d89e88a90c9e

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