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

Revision 477, 11.4 KB checked in by maximinus_parpg, 10 years ago (diff)

More clean up of the code.
Finshed first pass.
So should be at least stands compliant.

  • 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()
29_AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6)
30
31class ActorBehaviour (fife.InstanceActionListener):
32    """Fife agent listener"""
33    def __init__(self, layer):
34        fife.InstanceActionListener.__init__(self)
35        self.layer = layer
36   
37    def attachToLayer(self, agent_ID):
38        """Attaches to a certain layer
39           @type agent_ID: String
40           @param agent_ID: ID of the layer to attach to.
41           @return: None"""
42        self.agent = self.layer.getInstance(agent_ID)
43        self.agent.addActionListener(self)
44        self.state = _AGENT_STATE_NONE
45        # TODO: rework/improve
46        self.speed = float(TDS.readSetting("PCSpeed"))-1
47       
48    def getX(self):
49        """Get the NPC's x position on the map.
50           @rtype: integer"
51           @return: the x coordinate of the NPC's location"""
52        return self.agent.getLocation().getLayerCoordinates().x
53
54    def getY(self):
55        """Get the NPC's y position on the map.
56           @rtype: integer
57           @return: the y coordinate of the NPC's location"""
58        return self.agent.getLocation().getLayerCoordinates().y
59       
60    def onInstanceActionFinished(self, instance, action):
61        pass
62
63class PCBehaviour (ActorBehaviour):
64    def __init__(self, parent = None, layer = None):
65        super(PCBehaviour, self).__init__(layer)       
66        self.parent = parent
67        self.idle_counter = 1
68        # TODO: rework/improve
69        self.speed = float(TDS.readSetting("PCSpeed"))
70        self.nextAction = None
71        self.agent = None
72       
73    def onInstanceActionFinished(self, instance, action):
74        """@type instance: ???
75           @param instance: ???
76           @type action: ???
77           @param action: ???
78           @return: None"""
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        if self.agent is not None:
95            self.agent.removeActionListener(self)
96           
97        self.agent = layer.getInstance(self.parent.ID)
98        self.agent.addActionListener(self)
99        self.state = _AGENT_STATE_NONE
100        self.idle_counter = 1
101   
102    def idle(self):
103        """@return: None"""
104        self.state = _AGENT_STATE_IDLE
105        self.agent.act('stand', self.agent.getFacingLocation())
106
107class PlayerCharacter (GameObject, Living, CharStats):
108    """PC class"""
109    def __init__ (self, ID, agent_layer = None, inventory = None, **kwargs):
110        GameObject.__init__( self, ID, **kwargs )
111        Living.__init__( self, **kwargs )
112        CharStats.__init__( self, **kwargs )
113
114        self.is_PC = True
115       
116        # PC _has_ an inventory, he _is not_ one
117        if inventory == None:
118            self.inventory = Inventory()
119            self.inventory.placeItem(CarryableItem(ID=456, name="Dagger123"))
120            self.inventory.placeItem(CarryableItem(ID=555, name="Beer"))
121        else:
122            self.inventory = inventory
123        self.peopleIknow = set()
124
125        self.state = _AGENT_STATE_NONE
126        self.layer_id = agent_layer.getId()
127        self.createBehaviour(agent_layer)
128   
129    def meet(self, npc):
130        """Record that the PC has met a certain NPC
131           @type npc: str
132           @param npc: The NPC's name or id"""
133        if npc in self.peopleIknow:
134            # we could raise an error here, but should probably be a warn
135            # raise RuntimeError("I already know %s" % npc)
136            return
137        self.peopleIknow.add(npc)
138
139    def met(self, npc):
140        """Indicate whether the PC has met this npc before
141           @type npc: str
142           @param npc: The NPC's name or id
143           @return: None"""
144        return npc in self.peopleIknow
145
146    def createBehaviour(self, layer):
147        """Creates the behaviour for this actor.
148           @return: None"""
149        self.behaviour = PCBehaviour(self, layer)
150   
151    def setup(self):
152        """@return: None"""
153        self.behaviour.attachToLayer(self.ID)
154
155    def start(self):
156        """@return: None"""
157        self.behaviour.idle()
158   
159    def run(self, location):
160        """Makes the PC run to a certain location
161           @type location: fife.ScreenPoint
162           @param location: Screen position to run to.
163           @return: None"""
164        self.state = _AGENT_STATE_RUN
165        self.behaviour.nextAction = None
166        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
167
168    def walk(self, location):
169        """Makes the PC walk to a certain location.
170           @type location: fife.ScreenPoint
171           @param location: Screen position to walk to.
172           @return: None"""
173        self.state = _AGENT_STATE_RUN
174        self.behaviour.nextAction = None 
175        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
176
177    def teleport(self, location):
178        """Teleports a PC instantly to the given location.
179           @type location: fife.Location
180           @param location: Target coordinates for PC.
181           @return: None"""
182        self.state = _AGENT_STATE_IDLE
183        self.behaviour.nextAction = None 
184        self.behaviour.agent.setLocation(location)
185
186    def approach(self, location, action = None):
187        """Approaches a location and then perform an action (if set).
188           @type loc: fife.Location
189           @param loc: the location to approach
190           @type action: Action
191           @param action: The action to schedule for execution after the approach.
192           @return: None"""
193        self.state = _AGENT_STATE_APPROACH
194        self.behaviour.nextAction = action
195        boxLocation = tuple([int(float(i)) for i in location])
196        l = fife.Location(self.behaviour.agent.getLocation())
197        l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
198        self.behaviour.agent.move('run', l, self.behaviour.speed+1)
199
200
201class NPCBehaviour(ActorBehaviour):
202    def __init__(self, Parent = None, Layer = None):
203        super(NPCBehaviour, self).__init__(Layer)
204       
205        self.parent = Parent
206        self.state = _AGENT_STATE_NONE
207       
208        # hard code this for now
209        self.distRange = (2, 4)
210       
211    def getTargetLocation(self):
212        """@rtype: fife.Location
213           @return: NPC's position"""
214        x = self.getX()
215        y = self.getY()
216        if self.state == _AGENT_STATE_WANDER:
217            """ Random Target Location """
218            l = [0, 0]
219            for i in range(len(l)):
220                sign = randrange(0, 2)
221                dist = randrange(self.distRange[0], self.distRange[1])
222                if sign == 0:
223                    dist *= -1
224                l[i] = dist
225            x += l[0]
226            y += l[1]
227            # Random walk is
228            # rl = randint(-1, 1);ud = randint(-1, 1);x += rl;y += ud
229        l = fife.Location(self.agent.getLocation())
230        l.setLayerCoordinates(fife.ModelCoordinate(x, y))
231        return l
232
233    def onInstanceActionFinished(self, instance, action):
234        """What the NPC does when it has finished an action.
235           Called by the engine and required for InstanceActionListeners.
236           @type instance: fife.Instance
237           @param instance: self.agent (the NPC listener is listening for this
238                                        instance)
239           @type action: ???
240           @param action: ???
241           @return: None"""
242        if self.state == _AGENT_STATE_WANDER:
243            self.target_loc = self.getTargetLocation()
244        self.idle()
245       
246   
247    def idle(self):
248        """Controls the NPC when it is idling. Different actions
249           based on the NPC's state.
250           @return: None"""
251        if self.state == _AGENT_STATE_NONE:
252            self.state = _AGENT_STATE_IDLE
253            self.agent.act('stand', self.agent.getFacingLocation())
254        elif self.state == _AGENT_STATE_IDLE:
255            self.target_loc = self.getTargetLocation()
256            self.state = _AGENT_STATE_WANDER
257            self.agent.act('stand', self.agent.getFacingLocation())
258        elif self.state == _AGENT_STATE_WANDER:
259            self.parent.wander(self.target_loc)
260            self.state = _AGENT_STATE_NONE
261        elif self.state == _AGENT_STATE_TALK:
262            self.agent.act('stand', self.pc.getLocation())
263
264class NonPlayerCharacter(GameObject, Living, Scriptable, CharStats):
265    """NPC class"""
266    def __init__(self, ID, agent_layer=None, name='NPC', \
267                 text = 'A nonplayer character', inventory = None, **kwargs):
268        # init game object
269        GameObject.__init__(self, ID, name=name, **kwargs)
270        Living.__init__(self, **kwargs)
271        Scriptable.__init__(self, **kwargs)
272        CharStats.__init__(self, **kwargs)
273
274        self.is_NPC = True
275        if inventory == None:
276            self.inventory = Inventory()
277        else:
278            self.inventory = inventory
279        self.layer_id = agent_layer.getId()
280        self.createBehaviour(agent_layer)       
281        self.dialogue = kwargs.get('dialogue')
282
283    def createBehaviour(self, layer):
284        """Creates the behaviour for this actor.
285           @return None """
286        self.behaviour = NPCBehaviour(self, layer)
287
288    def getLocation(self):
289        """Get the NPC's position as a fife.Location object. Basically a
290           wrapper.
291           @rtype: fife.Location
292           @return: the location of the NPC"""
293        return self.behaviour.agent.getLocation()
294   
295    def wander(self, location):
296        """Nice slow movement for random walking.
297           @type location: fife.Location
298           @param location: Where the NPC will walk to.
299           @return: None"""
300        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
301
302    def run(self, location):
303        """Faster movement than walk.
304           @type location: fife.Location
305           @param location: Where the NPC will run to."""
306        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
307
308    def talk(self, pc):
309        """Makes the NPC ready to talk to the PC
310           @return: None"""
311        self.behaviour.state = _AGENT_STATE_TALK
312        self.behaviour.pc = pc.behaviour.agent
313        self.behaviour.idle()
314   
315    def setup(self):
316        """@return: None"""
317        self.behaviour.attachToLayer(self.ID)
318
319    def start(self):
320        """@return: None"""
321        self.behaviour.idle()
Note: See TracBrowser for help on using the repository browser.