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