comparison behaviours/moving.py @ 56:3f6299f975fe

Added BaseBehaviour. Moved methods from MovingAgentBehaviour to BaseBehaviour.
author KarstenBock@gmx.net
date Fri, 09 Sep 2011 15:18:17 +0200
parents 8b1ad2d342d8
children 2727d6b78978
comparison
equal deleted inserted replaced
55:8b1ad2d342d8 56:3f6299f975fe
12 12
13 # You should have received a copy of the GNU General Public License 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/>. 14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
15 15
16 from fife import fife 16 from fife import fife
17 import base
18 from base import BaseBehaviour
17 19
18 _AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6) 20 class MovingAgentBehaviour (BaseBehaviour):
19
20 class MovingAgentBehaviour (fife.InstanceActionListener):
21 """Fife agent listener""" 21 """Fife agent listener"""
22 def __init__(self): 22 def __init__(self):
23 fife.InstanceActionListener.__init__(self) 23 BaseBehaviour.__init__(self)
24 self.agent = None
25 self.state = None
26 self.speed = 0 24 self.speed = 0
27 self.idle_counter = 1 25 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 26
52 def onNewMap(self, layer): 27 def onNewMap(self, layer):
53 """Sets the agent onto the new layer.""" 28 """Sets the agent onto the new layer."""
54 if self.agent is not None: 29 BaseBehaviour.onNewMap(self, layer)
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 30 self.idle_counter = 1
61 31
62 def idle(self):
63 """@return: None"""
64 self.state = _AGENT_STATE_IDLE
65 self.agent.act('stand', self.agent.getFacingLocation())
66 32
67 def approach(self, location, action=None): 33 def approach(self, location, action=None):
68 """Approaches a location and then perform an action (if set). 34 """Approaches a location and then perform an action (if set).
69 @type loc: fife.Location 35 @type loc: fife.Location
70 @param loc: the location to approach 36 @param loc: the location to approach
71 @type action: Action 37 @type action: Action
72 @param action: The action to schedule for execution after the approach. 38 @param action: The action to schedule for execution after the approach.
73 @return: None""" 39 @return: None"""
74 self.state = _AGENT_STATE_APPROACH 40 self.state = base._AGENT_STATE_APPROACH
75 self.nextAction = action 41 self.nextAction = action
76 boxLocation = tuple([int(float(i)) for i in location]) 42 boxLocation = tuple([int(float(i)) for i in location])
77 l = fife.Location(self.agent.getLocation()) 43 l = fife.Location(self.agent.getLocation())
78 l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation)) 44 l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
79 self.agent.move('run', l, self.speed + 1) 45 self.agent.move('run', l, self.speed + 1)
82 """@type instance: ??? 48 """@type instance: ???
83 @param instance: ??? 49 @param instance: ???
84 @type action: ??? 50 @type action: ???
85 @param action: ??? 51 @param action: ???
86 @return: None""" 52 @return: None"""
87 # First we reset the next behavior 53 BaseBehaviour.onInstanceActionFinished(self, instance, action)
88 act = self.nextAction
89 self.nextAction = None
90 self.idle()
91
92 if act:
93 act.execute()
94 54
95 if(action.getId() != 'stand'): 55 if(action.getId() != 'stand'):
96 self.idle_counter = 1 56 self.idle_counter = 1
97 else: 57 else:
98 self.idle_counter += 1 58 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 59
114 def run(self, location): 60 def run(self, location):
115 """Makes the PC run to a certain location 61 """Makes the PC run to a certain location
116 @type location: fife.ScreenPoint 62 @type location: fife.ScreenPoint
117 @param location: Screen position to run to. 63 @param location: Screen position to run to.
118 @return: None""" 64 @return: None"""
119 self.state = _AGENT_STATE_RUN 65 self.state = base._AGENT_STATE_RUN
120 self.nextAction = None 66 self.nextAction = None
121 self.agent.move('run', location, self.speed + 1) 67 self.agent.move('run', location, self.speed + 1)
122 68
123 def walk(self, location): 69 def walk(self, location):
124 """Makes the PC walk to a certain location. 70 """Makes the PC walk to a certain location.
125 @type location: fife.ScreenPoint 71 @type location: fife.ScreenPoint
126 @param location: Screen position to walk to. 72 @param location: Screen position to walk to.
127 @return: None""" 73 @return: None"""
128 self.state = _AGENT_STATE_RUN 74 self.state = base._AGENT_STATE_RUN
129 self.nextAction = None 75 self.nextAction = None
130 self.agent.move('walk', location, self.speed - 1) 76 self.agent.move('walk', location, self.speed - 1)
131 77