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

Revision 541, 12.7 KB checked in by beliar, 10 years ago (diff)
  • Change to make PARPG run with the latest FIFE revision (3373)
    • Removed argument to addCamera that FIFE no longer needs
  • Changed settings code to use the FIFE extension fife_settings
  • Additions to pylintrc
  • 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/>.
17
18from fife import fife
19from scripts import world
20from base import *
21from composed import *
22from scripts.inventory import Inventory
23
24"""All actors go here. Concrete classes only."""
25
26__all__ = ["PlayerCharacter", "NonPlayerCharacter",]
27
28TDS = Setting(app_name="PARPG",
29              settings_file="./settings.xml", 
30              settings_gui_xml="")
31_AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6)
32
33class ActorBehaviour (fife.InstanceActionListener):
34    """Fife agent listener"""
35    def __init__(self, layer):
36        fife.InstanceActionListener.__init__(self)
37        self.layer = layer
38   
39    def attachToLayer(self, agent_ID):
40        """Attaches to a certain layer
41           @type agent_ID: String
42           @param agent_ID: ID of the layer to attach to.
43           @return: None"""
44        self.agent = self.layer.getInstance(agent_ID)
45        self.agent.addActionListener(self)
46        self.state = _AGENT_STATE_NONE
47        # TODO: rework/improve
48        self.speed = TDS.get("PARPG", "PCSpeed")-1
49       
50    def getX(self):
51        """Get the NPC's x position on the map.
52           @rtype: integer"
53           @return: the x coordinate of the NPC's location"""
54        return self.agent.getLocation().getLayerCoordinates().x
55
56    def getY(self):
57        """Get the NPC's y position on the map.
58           @rtype: integer
59           @return: the y coordinate of the NPC's location"""
60        return self.agent.getLocation().getLayerCoordinates().y
61       
62    def onInstanceActionFinished(self, instance, action):
63        pass
64
65class PCBehaviour (ActorBehaviour):
66    def __init__(self, parent = None, layer = None):
67        super(PCBehaviour, self).__init__(layer)       
68        self.parent = parent
69        self.idle_counter = 1
70        # TODO: rework/improve
71        self.speed = TDS.get("PARPG", "PCSpeed")
72        self.nextAction = None
73        self.agent = None
74       
75    def onInstanceActionFinished(self, instance, action):
76        """@type instance: ???
77           @param instance: ???
78           @type action: ???
79           @param action: ???
80           @return: None"""
81        # First we reset the next behavior
82        act = self.nextAction
83        self.nextAction = None 
84        self.idle()
85       
86        if act:
87            act.execute()
88           
89        if(action.getId() != 'stand'):
90            self.idle_counter = 1
91        else:
92            self.idle_counter += 1
93           
94    def onNewMap(self, layer):
95        """Sets the agent onto the new layer."""
96        if self.agent is not None:
97            self.agent.removeActionListener(self)
98           
99        self.agent = layer.getInstance(self.parent.ID)
100        self.agent.addActionListener(self)
101        self.state = _AGENT_STATE_NONE
102        self.idle_counter = 1
103   
104    def idle(self):
105        """@return: None"""
106        self.state = _AGENT_STATE_IDLE
107        self.agent.act('stand', self.agent.getFacingLocation())
108
109class PlayerCharacter (GameObject, Living, CharStats):
110    """PC class"""
111    def __init__ (self, ID, agent_layer = None, inventory = None, **kwargs):
112        GameObject.__init__( self, ID, **kwargs )
113        Living.__init__( self, **kwargs )
114        CharStats.__init__( self, **kwargs )
115
116        self.is_PC = True
117       
118        # PC _has_ an inventory, he _is not_ one
119        if inventory == None:
120            self.inventory = Inventory()
121            self.inventory.placeItem(CarryableItem(ID=456, name="Dagger123"))
122            self.inventory.placeItem(CarryableItem(ID=555, name="Beer"))
123            self.inventory.placeItem(CarryableItem(ID = 616,
124                                                   name = "Pamphlet",
125                                                   image = "/gui/inv_images/inv_pamphlet.png"))
126        else:
127            self.inventory = inventory
128        self.peopleIknow = set()
129
130        self.state = _AGENT_STATE_NONE
131        self.layer_id = agent_layer.getId()
132        self.createBehaviour(agent_layer)
133   
134    def __getstate__(self):
135        odict = self.__dict__.copy()
136        del odict["behaviour"]
137        return odict;
138   
139    def __setstate__(self, state):
140        self.__dict__.update(state)
141
142    def meet(self, npc):
143        """Record that the PC has met a certain NPC
144           @type npc: str
145           @param npc: The NPC's name or id"""
146        if npc in self.peopleIknow:
147            # we could raise an error here, but should probably be a warn
148            # raise RuntimeError("I already know %s" % npc)
149            return
150        self.peopleIknow.add(npc)
151
152    def met(self, npc):
153        """Indicate whether the PC has met this npc before
154           @type npc: str
155           @param npc: The NPC's name or id
156           @return: None"""
157        return npc in self.peopleIknow
158
159    def createBehaviour(self, layer):
160        """Creates the behaviour for this actor.
161           @return: None"""
162        self.behaviour = PCBehaviour(self, layer)
163   
164    def setup(self):
165        """@return: None"""
166        self.behaviour.attachToLayer(self.ID)
167
168    def start(self):
169        """@return: None"""
170        self.behaviour.idle()
171   
172    def run(self, location):
173        """Makes the PC run to a certain location
174           @type location: fife.ScreenPoint
175           @param location: Screen position to run to.
176           @return: None"""
177        self.state = _AGENT_STATE_RUN
178        self.behaviour.nextAction = None
179        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
180
181    def walk(self, location):
182        """Makes the PC walk to a certain location.
183           @type location: fife.ScreenPoint
184           @param location: Screen position to walk to.
185           @return: None"""
186        self.state = _AGENT_STATE_RUN
187        self.behaviour.nextAction = None 
188        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
189
190    def teleport(self, location):
191        """Teleports a PC instantly to the given location.
192           @type location: fife.Location
193           @param location: Target coordinates for PC.
194           @return: None"""
195        self.state = _AGENT_STATE_IDLE
196        self.behaviour.nextAction = None 
197        self.behaviour.agent.setLocation(location)
198
199    def approach(self, location, action = None):
200        """Approaches a location and then perform an action (if set).
201           @type loc: fife.Location
202           @param loc: the location to approach
203           @type action: Action
204           @param action: The action to schedule for execution after the approach.
205           @return: None"""
206        self.state = _AGENT_STATE_APPROACH
207        self.behaviour.nextAction = action
208        boxLocation = tuple([int(float(i)) for i in location])
209        l = fife.Location(self.behaviour.agent.getLocation())
210        l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
211        self.behaviour.agent.move('run', l, self.behaviour.speed+1)
212
213
214class NPCBehaviour(ActorBehaviour):
215    def __init__(self, Parent = None, Layer = None):
216        super(NPCBehaviour, self).__init__(Layer)
217       
218        self.parent = Parent
219        self.state = _AGENT_STATE_NONE
220       
221        # hard code this for now
222        self.distRange = (2, 4)
223       
224    def getTargetLocation(self):
225        """@rtype: fife.Location
226           @return: NPC's position"""
227        x = self.getX()
228        y = self.getY()
229        if self.state == _AGENT_STATE_WANDER:
230            """ Random Target Location """
231            l = [0, 0]
232            for i in range(len(l)):
233                sign = randrange(0, 2)
234                dist = randrange(self.distRange[0], self.distRange[1])
235                if sign == 0:
236                    dist *= -1
237                l[i] = dist
238            x += l[0]
239            y += l[1]
240            # Random walk is
241            # rl = randint(-1, 1);ud = randint(-1, 1);x += rl;y += ud
242        l = fife.Location(self.agent.getLocation())
243        l.setLayerCoordinates(fife.ModelCoordinate(x, y))
244        return l
245
246    def onInstanceActionFinished(self, instance, action):
247        """What the NPC does when it has finished an action.
248           Called by the engine and required for InstanceActionListeners.
249           @type instance: fife.Instance
250           @param instance: self.agent (the NPC listener is listening for this
251                                        instance)
252           @type action: ???
253           @param action: ???
254           @return: None"""
255        if self.state == _AGENT_STATE_WANDER:
256            self.target_loc = self.getTargetLocation()
257        self.idle()
258       
259   
260    def idle(self):
261        """Controls the NPC when it is idling. Different actions
262           based on the NPC's state.
263           @return: None"""
264        if self.state == _AGENT_STATE_NONE:
265            self.state = _AGENT_STATE_IDLE
266            self.agent.act('stand', self.agent.getFacingLocation())
267        elif self.state == _AGENT_STATE_IDLE:
268            self.target_loc = self.getTargetLocation()
269            self.state = _AGENT_STATE_WANDER
270            self.agent.act('stand', self.agent.getFacingLocation())
271        elif self.state == _AGENT_STATE_WANDER:
272            self.parent.wander(self.target_loc)
273            self.state = _AGENT_STATE_NONE
274        elif self.state == _AGENT_STATE_TALK:
275            self.agent.act('stand', self.pc.getLocation())
276
277class NonPlayerCharacter(GameObject, Living, Scriptable, CharStats):
278    """NPC class"""
279    def __init__(self, ID, agent_layer=None, name='NPC', \
280                 text = 'A nonplayer character', inventory = None, **kwargs):
281        # init game object
282        GameObject.__init__(self, ID, name=name, **kwargs)
283        Living.__init__(self, **kwargs)
284        Scriptable.__init__(self, **kwargs)
285        CharStats.__init__(self, **kwargs)
286
287        self.is_NPC = True
288        if inventory == None:
289            self.inventory = Inventory()
290            self.inventory.placeItem(CarryableItem(ID = 632,
291                                                   name = "box",
292                                                   image = "/gui/inv_images/inv_giftbox.png"))
293
294        else:
295            self.inventory = inventory
296        self.layer_id = agent_layer.getId()
297        self.createBehaviour(agent_layer)       
298        self.dialogue = kwargs.get('dialogue')
299
300    def __getstate__(self):
301        odict = self.__dict__.copy()
302        del odict["behaviour"]
303        return odict;
304   
305    def __setstate__(self, state):
306        self.__dict__.update(state)       
307
308    def createBehaviour(self, layer):
309        """Creates the behaviour for this actor.
310           @return None """
311        self.behaviour = NPCBehaviour(self, layer)
312
313    def getLocation(self):
314        """Get the NPC's position as a fife.Location object. Basically a
315           wrapper.
316           @rtype: fife.Location
317           @return: the location of the NPC"""
318        return self.behaviour.agent.getLocation()
319   
320    def wander(self, location):
321        """Nice slow movement for random walking.
322           @type location: fife.Location
323           @param location: Where the NPC will walk to.
324           @return: None"""
325        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
326
327    def run(self, location):
328        """Faster movement than walk.
329           @type location: fife.Location
330           @param location: Where the NPC will run to."""
331        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
332
333    def talk(self, pc):
334        """Makes the NPC ready to talk to the PC
335           @return: None"""
336        self.behaviour.state = _AGENT_STATE_TALK
337        self.behaviour.pc = pc.behaviour.agent
338        self.behaviour.idle()
339       
340    def give (self, item, actor):
341        """Gives the specified item to the different actor. Raises an exception if the item was invalid or not found
342           @type item: Carryable
343           @param item: The item object to give
344           @param actor: Person to give item to"""
345        if item == None: 
346            raise ValueError("I don't have %s" % item.name)
347        self.inventory.takeItem(item)
348        actor.inventory.placeItem(item)           
349   
350    def setup(self):
351        """@return: None"""
352        self.behaviour.attachToLayer(self.ID)
353
354    def start(self):
355        """@return: None"""
356        self.behaviour.idle()
Note: See TracBrowser for help on using the repository browser.