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

Revision 310, 9.5 KB checked in by eliedebrauwer, 10 years ago (diff)

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