source: trunk/game/scripts/objects/actors.py @ 362

Revision 362, 10.8 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #142: Patch by eliedebrauwer, allows a sequence of actions (e.g. walk to an NPC and start to talk) to be aborted. fixes[s:trac, t:142]

  • Property svn:eol-style set to native
Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17import fife
18from scripts import world
19from base import *
20
21"""All actors go here. Concrete classes only."""
22
23__all__ = ["PlayerCharacter", "NonPlayerCharacter",]
24
25TDS = Setting()
26_AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6)
27
28class ActorBehaviour (fife.InstanceActionListener):
29    """Fife agent listener
30    """
31    def __init__(self, layer):
32        fife.InstanceActionListener.__init__(self)
33        self.layer = layer
34   
35    def attachToLayer(self, agent_ID):
36        """ Attaches to a certain layer
37            @type agent_ID: String
38            @param agent_ID: ID of the layer to attach to.
39            @return: None
40        """
41        self.agent = self.layer.getInstance(agent_ID)
42        self.agent.addActionListener(self)
43        self.state = _AGENT_STATE_NONE
44        self.speed = float(TDS.readSetting("PCSpeed"))-1 # TODO: rework/improve
45       
46    def getX(self):
47        """Get the NPC's x position on the map.
48           @rtype: integer"
49           @return: the x coordinate of the NPC's location"""
50        return self.agent.getLocation().getLayerCoordinates().x
51
52    def getY(self):
53        """Get the NPC's y position on the map.
54           @rtype: integer
55           @return: the y coordinate of the NPC's location"""
56        return self.agent.getLocation().getLayerCoordinates().y
57       
58    def onInstanceActionFinished(self, instance, action):
59        pass
60
61   
62class PCBehaviour (ActorBehaviour):
63    def __init__(self, parent = None, layer = None):
64        super(PCBehaviour, self).__init__(layer)
65       
66        self.parent = parent
67        self.idle_counter = 1
68        self.speed = float(TDS.readSetting("PCSpeed")) # TODO: rework/improve
69        self.nextAction = None
70        self.agent = None
71       
72    def onInstanceActionFinished(self, instance, action):
73        """@type instance: ???
74           @param instance: ???
75           @type action: ???
76           @param action: ???
77           @return: None"""
78       
79        # First we reset the next behavior
80        act = self.nextAction
81        self.nextAction = None 
82        self.idle()
83       
84        if act:
85            act.execute()
86           
87        if(action.getId() != 'stand'):
88            self.idle_counter = 1
89        else:
90            self.idle_counter += 1
91           
92    def onNewMap(self, layer):
93        """Sets the agent onto the new layer.
94        """
95        if self.agent is not None:
96            self.agent.removeActionListener(self)
97           
98        self.agent = layer.getInstance(self.parent.ID)
99        self.agent.addActionListener(self)
100        self.state = _AGENT_STATE_NONE
101        self.idle_counter = 1
102   
103    def idle(self):
104        """@return: None"""
105        self.state = _AGENT_STATE_IDLE
106        self.agent.act('stand', self.agent.getFacingLocation())
107
108class PlayerCharacter (GameObject, Living, CharStats):
109    """
110    PC class
111    """
112    def __init__ (self, ID, agent_layer = None, **kwargs):
113        GameObject.__init__( self, ID, **kwargs )
114        Living.__init__( self, **kwargs )
115        CharStats.__init__( self, **kwargs )
116
117        self.is_PC = True
118        self.inventory = set(('beer',))
119        self.peopleIknow = set()
120        self.state = _AGENT_STATE_NONE
121        self.layer_id = agent_layer.getId()
122        self.createBehaviour(agent_layer)
123   
124    def meet(self, npc):
125        """Record that the PC has met a certain NPC
126        @type npc: str
127        @param npc: The NPC's name or id"""
128        if npc in self.peopleIknow:
129            raise RuntimeError("I already know %s" % npc)
130        self.peopleIknow.add(npc)
131
132    def met(self, npc):
133        """Indicate whether the PC has met this npc before
134        @type npc: str
135        @param npc: The NPC's name or id
136        @return: None"""
137        return npc in self.peopleIknow
138
139    def createBehaviour(self, layer):
140        """Creates the behaviour for this actor.
141            @return None """
142        self.behaviour = PCBehaviour(self, layer)
143   
144    def setup(self):
145        """@return: None"""
146        self.behaviour.attachToLayer(self.ID)
147
148    def start(self):
149        """@return: None"""
150        self.behaviour.idle()
151   
152    def run(self, location):
153        """Makes the PC run to a certain location
154           @type location: fife.ScreenPoint
155           @param location: Screen position to run to.
156           @return: None"""
157        self.state = _AGENT_STATE_RUN
158        self.behaviour.nextAction = None
159        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
160
161    def walk(self, location):
162        """Makes the PC walk to a certain location.
163           @type location: fife.ScreenPoint
164           @param location: Screen position to walk to.
165           @return: None"""
166        self.state = _AGENT_STATE_RUN
167        self.behaviour.nextAction = None 
168        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
169
170    def teleport(self, location):
171        """Teleports a PC instantly to the given location.
172           @type location: fife.Location
173           @param location: Target coordinates for PC.
174           @return: None"""
175        self.state = _AGENT_STATE_IDLE
176        self.behaviour.nextAction = None 
177        self.behaviour.agent.setLocation(location)
178
179    def approach(self, location, action = None):
180        """Approaches a location and then perform an action (if set).
181           @type loc: fife.Location
182           @param loc: the location to approach
183           @type action: Action
184           @param action: The action to schedule for execution after the approach.
185           @return: None"""
186        self.state = _AGENT_STATE_APPROACH
187        self.behaviour.nextAction = action
188        boxLocation = tuple([int(float(i)) for i in location])
189        l = fife.Location(self.behaviour.agent.getLocation())
190        l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
191        self.behaviour.agent.move('run', l, self.behaviour.speed+1)
192
193
194class NPCBehaviour(ActorBehaviour):
195    def __init__(self, Parent = None, Layer = None):
196        super(NPCBehaviour, self).__init__(Layer)
197       
198        self.parent = Parent
199        self.state = _AGENT_STATE_NONE
200       
201        # hard code this for now
202        self.distRange = (2, 4)
203       
204    def getTargetLocation(self):
205        """@rtype: fife.Location
206           @return: NPC's position"""
207        x = self.getX()
208        y = self.getY()
209        if self.state == _AGENT_STATE_WANDER:
210            """ Random Target Location """
211            l = [0, 0]
212            for i in range(len(l)):
213                sign = randrange(0, 2)
214                dist = randrange(self.distRange[0], self.distRange[1])
215                if sign == 0:
216                    dist *= -1
217                l[i] = dist
218            x += l[0]
219            y += l[1]
220            # Random walk is
221            # rl = randint(-1, 1);ud = randint(-1, 1);x += rl;y += ud
222        l = fife.Location(self.agent.getLocation())
223        l.setLayerCoordinates(fife.ModelCoordinate(x, y))
224        return l
225
226    def onInstanceActionFinished(self, instance, action):
227        """What the NPC does when it has finished an action.
228           Called by the engine and required for InstanceActionListeners.
229           @type instance: fife.Instance
230           @param instance: self.agent (the NPC listener is listening for this
231            instance)
232           @type action: ???
233           @param action: ???
234           @return: None"""
235        if self.state == _AGENT_STATE_WANDER:
236            self.target_loc = self.getTargetLocation()
237        self.idle()
238       
239   
240    def idle(self):
241        """Controls the NPC when it is idling. Different actions
242           based on the NPC's state.
243           @return: None"""
244        if self.state == _AGENT_STATE_NONE:
245            self.state = _AGENT_STATE_IDLE
246            self.agent.act('stand', self.agent.getFacingLocation())
247        elif self.state == _AGENT_STATE_IDLE:
248            self.target_loc = self.getTargetLocation()
249            self.state = _AGENT_STATE_WANDER
250            self.agent.act('stand', self.agent.getFacingLocation())
251        elif self.state == _AGENT_STATE_WANDER:
252            self.parent.wander(self.target_loc)
253            self.state = _AGENT_STATE_NONE
254        elif self.state == _AGENT_STATE_TALK:
255            self.agent.act('stand', self.pc.getLocation())
256
257class NonPlayerCharacter(GameObject, Living, Scriptable, CharStats):
258    """
259    NPC class
260    """
261    def __init__(self, ID, agent_layer=None, name='NPC', \
262                 text = 'A nonplayer character', **kwargs):
263        # init game object
264        GameObject.__init__(self, ID, name=name, **kwargs)
265        Living.__init__(self, **kwargs)
266        Scriptable.__init__(self, **kwargs)
267        CharStats.__init__(self, **kwargs)
268
269        self.is_NPC = True
270        self.inventory = None
271        self.layer_id = agent_layer.getId()
272        self.createBehaviour(agent_layer)       
273        self.dialogue = kwargs.get('dialogue')
274
275    def createBehaviour(self, layer):
276        """ Creates the behaviour for this actor.
277            @return None """
278        self.behaviour = NPCBehaviour(self, layer)
279
280    def getLocation(self):
281        """ Get the NPC's position as a fife.Location object. Basically a
282            wrapper.
283            @rtype: fife.Location
284            @return: the location of the NPC"""
285        return self.behaviour.agent.getLocation()
286   
287    def wander(self, location):
288        """Nice slow movement for random walking.
289           @type location: fife.Location
290           @param location: Where the NPC will walk to.
291           @return: None"""
292        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
293
294    def run(self, location):
295        """Faster movement than walk.
296           @type location: fife.Location
297           @param location: Where the NPC will run to."""
298        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
299
300    def talk(self, pc):
301        """ Makes the NPC ready to talk to the PC
302            @return: None"""
303        self.behaviour.state = _AGENT_STATE_TALK
304        self.behaviour.pc = pc.behaviour.agent
305        self.behaviour.idle()
306   
307    def setup(self):
308        """@return: None"""
309        self.behaviour.attachToLayer(self.ID)
310
311    def start(self):
312        """@return: None"""
313        self.behaviour.idle()
Note: See TracBrowser for help on using the repository browser.