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

Revision 340, 10.1 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #66: Patch by Saritor, This patch implements Kaydeth's camera fix as well as a fix for letting us be able to remove the PC entry on individual map files. However the very first map a player starts the game on will need such an entry. fixes[s:trac, t:66]

  • 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       
71    def onInstanceActionFinished(self, instance, action):
72        """@type instance: ???
73           @param instance: ???
74           @type action: ???
75           @param action: ???
76           @return: None"""
77        if self.nextAction:
78            self.nextAction.execute()
79            self.nextAction = None
80            self.idle()
81        else:
82            self.idle()
83           
84        if(action.getId() != 'stand'):
85            self.idle_counter = 1
86        else:
87            self.idle_counter += 1
88           
89    def onNewMap(self, layer):
90        """Sets the agent onto the new layer.
91        """
92        self.agent.removeActionListener(self)
93        self.agent = layer.getInstance(self.parent.ID)
94        self.agent.addActionListener(self)
95        self.state = _AGENT_STATE_NONE
96        self.idle_counter = 1
97   
98    def idle(self):
99        """@return: None"""
100        self.state = _AGENT_STATE_IDLE
101        self.agent.act('stand', self.agent.getFacingLocation())
102
103class PlayerCharacter (GameObject, Living, CharStats):
104    """
105    PC class
106    """
107    def __init__ (self, ID, agent_layer = None, **kwargs):
108        GameObject.__init__( self, ID, **kwargs )
109        Living.__init__( self, **kwargs )
110        CharStats.__init__( self, **kwargs )
111
112        self.is_PC = True
113       
114        # PC _has_ an inventory, he _is not_ one
115        self.inventory = None
116       
117        self.state = _AGENT_STATE_NONE
118        self.layer_id = agent_layer.getId()
119        self.createBehaviour(agent_layer)
120   
121    def createBehaviour(self, layer):
122        """ Creates the behaviour for this actor.
123            @return None """
124        self.behaviour = PCBehaviour(self, layer)
125   
126    def setup(self):
127        """@return: None"""
128        self.behaviour.attachToLayer(self.ID)
129
130    def start(self):
131        """@return: None"""
132        self.behaviour.idle()
133   
134    def run(self, location):
135        """Makes the PC run to a certain location
136           @type location: fife.ScreenPoint
137           @param location: Screen position to run to.
138           @return: None"""
139        self.state = _AGENT_STATE_RUN
140        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
141
142    def walk(self, location):
143        """Makes the PC walk to a certain location.
144           @type location: fife.ScreenPoint
145           @param location: Screen position to walk to.
146           @return: None"""
147        self.state = _AGENT_STATE_RUN
148        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
149
150    def teleport(self, location):
151        """Teleports a PC instantly to the given location.
152           @type location: fife.Location
153           @param location: Target coordinates for PC.
154           @return: None"""
155        self.state = _AGENT_STATE_IDLE
156        self.behaviour.agent.setLocation(location)
157
158    def approach(self, location, action = None):
159        """Approaches an npc and then ???.
160           @type loc: fife.Location
161           @param loc: the location to approach
162           @type action: Action
163           @param action: The action to schedule for execution after the approach.
164           @return: None"""
165        self.state = _AGENT_STATE_APPROACH
166        self.behaviour.nextAction = action
167        boxLocation = tuple([int(float(i)) for i in location])
168        l = fife.Location(self.behaviour.agent.getLocation())
169        l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
170        self.behaviour.agent.move('run', l, self.behaviour.speed)
171
172class NPCBehaviour(ActorBehaviour):
173    def __init__(self, Parent = None, Layer = None):
174        super(NPCBehaviour, self).__init__(Layer)
175       
176        self.parent = Parent
177        self.state = _AGENT_STATE_NONE
178       
179        # hard code this for now
180        self.distRange = (2, 4)
181       
182    def getTargetLocation(self):
183        """@rtype: fife.Location
184           @return: NPC's position"""
185        x = self.getX()
186        y = self.getY()
187        if self.state == _AGENT_STATE_WANDER:
188            """ Random Target Location """
189            l = [0, 0]
190            for i in range(len(l)):
191                sign = randrange(0, 2)
192                dist = randrange(self.distRange[0], self.distRange[1])
193                if sign == 0:
194                    dist *= -1
195                l[i] = dist
196            x += l[0]
197            y += l[1]
198            # Random walk is
199            # rl = randint(-1, 1);ud = randint(-1, 1);x += rl;y += ud
200        l = fife.Location(self.agent.getLocation())
201        l.setLayerCoordinates(fife.ModelCoordinate(*tuple([x, y])))
202        return l
203
204    def onInstanceActionFinished(self, instance, action):
205        """What the NPC does when it has finished an action.
206           Called by the engine and required for InstanceActionListeners.
207           @type instance: fife.Instance
208           @param instance: self.agent (the NPC listener is listening for this
209            instance)
210           @type action: ???
211           @param action: ???
212           @return: None"""
213        if self.state == _AGENT_STATE_WANDER:
214            self.target_loc = self.getTargetLocation()
215        self.idle()
216       
217   
218    def idle(self):
219        """Controls the NPC when it is idling. Different actions
220           based on the NPC's state.
221           @return: None"""
222        if self.state == _AGENT_STATE_NONE:
223            self.state = _AGENT_STATE_IDLE
224            self.agent.act('stand', self.agent.getFacingLocation())
225        elif self.state == _AGENT_STATE_IDLE:
226            self.target_loc = self.getTargetLocation()
227            self.state = _AGENT_STATE_WANDER
228            self.agent.act('stand', self.agent.getFacingLocation())
229        elif self.state == _AGENT_STATE_WANDER:
230            self.parent.wander(self.target_loc)
231            self.state = _AGENT_STATE_NONE
232        elif self.state == _AGENT_STATE_TALK:
233            self.agent.act('stand', self.pc.getLocation())
234
235class NonPlayerCharacter(GameObject, Living, Scriptable, CharStats):
236    """
237    NPC class
238    """
239    def __init__(self, ID, agent_layer=None, name='NPC', \
240                 text = 'A nonplayer character', **kwargs):
241        # init game object
242        GameObject.__init__(self, ID, name=name, **kwargs)
243        Living.__init__(self, **kwargs)
244        Scriptable.__init__(self, **kwargs)
245        CharStats.__init__(self, **kwargs)
246
247        self.is_NPC = True
248        self.inventory = None
249        self.layer_id = agent_layer.getId()
250        self.createBehaviour(agent_layer)       
251        self.dialogue = kwargs.get('dialogue')
252
253    def createBehaviour(self, layer):
254        """ Creates the behaviour for this actor.
255            @return None """
256        self.behaviour = NPCBehaviour(self, layer)
257
258    def getLocation(self):
259        """ Get the NPC's position as a fife.Location object. Basically a
260            wrapper.
261            @rtype: fife.Location
262            @return: the location of the NPC"""
263        return self.behaviour.agent.getLocation()
264   
265    def wander(self, location):
266        """Nice slow movement for random walking.
267           @type location: fife.Location
268           @param location: Where the NPC will walk to.
269           @return: None"""
270        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
271
272    def run(self, location):
273        """Faster movement than walk.
274           @type location: fife.Location
275           @param location: Where the NPC will run to."""
276        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
277
278    def talk(self, pc):
279        """ Makes the NPC ready to talk to the PC
280            @return: None"""
281        self.behaviour.state = _AGENT_STATE_TALK
282        self.behaviour.pc = pc.behaviour.agent
283        self.behaviour.idle()
284   
285    def setup(self):
286        """@return: None"""
287        self.behaviour.attachToLayer(self.ID)
288
289    def start(self):
290        """@return: None"""
291        self.behaviour.idle()
Note: See TracBrowser for help on using the repository browser.