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

Revision 287, 8.8 KB checked in by orlandov, 10 years ago (diff)

Ticket #74: Patch by or1andov, Integrate dialogue GUI and engine into game.

It should now be possible to engage in a conversation with NPC characters by
selecting talk from their context menu. fixes[s:trac, t:74]

Known Issues:


  • the GUI is terrible. instead of a dialog window, we could use a better looking background image and fixed position widgets
  • requires PyYAML to be installed sudo apt-get install python-yaml easy_install PyYAML
  • we have to create a simple state model for the PC can remember tasks
  • need to make NPC's be able to remember things - need a model in NPCs for this
  • no way currently to give or take items from npc's; again, more model work. this could be done by adding another callback similar to start_quest. may be necessary for npc's to have an inventory for this
  • The dialogue engine is not hooked up to specific NPC's... it currently always loads dialogue/sample.yaml - need to have the dialogue looked up from some XML attribute
  • dialogue is not broken off if the PC moves away from the NPC
  • npc image is hardcoded, it should use the image specified in the yaml file
  • 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        """@type location: ???
125           @param location: ???
126           @return: None"""
127        self.state = _AGENT_STATE_RUN
128        self.behaviour.agent.move('run', location, self.behaviour.speed)
129       
130    def approach(self, location, action = None):
131        """Approaches an npc and then ???.
132           @type loc: fife.Location
133           @param loc: the location to approach
134           @type action: Action
135           @param action: The action to schedule for execution after the approach.
136           @return: None"""
137        self.state = _AGENT_STATE_APPROACH
138        self.behaviour.nextAction = action
139        boxLocation = tuple([int(float(i)) for i in location])
140        l = fife.Location(self.behaviour.agent.getLocation())
141        l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
142        self.behaviour.agent.move('run', l, self.behaviour.speed)
143
144class NPCBehaviour(ActorBehaviour):
145    def __init__(self, Parent = None, Layer = None):
146        super(NPCBehaviour, self).__init__(Layer)
147       
148        self.parent = Parent
149        self.state = _AGENT_STATE_NONE
150       
151        # hard code this for now
152        self.distRange = (2, 4)
153       
154    def getTargetLocation(self):
155        """@rtype: fife.Location
156           @return: NPC's position"""
157        x = self.getX()
158        y = self.getY()
159        if self.state == _AGENT_STATE_WANDER:
160            """ Random Target Location """
161            l = [0, 0]
162            for i in range(len(l)):
163                sign = randrange(0, 2)
164                dist = randrange(self.distRange[0], self.distRange[1])
165                if sign == 0:
166                    dist *= -1
167                l[i] = dist
168            x += l[0]
169            y += l[1]
170            # Random walk is
171            # rl = randint(-1, 1);ud = randint(-1, 1);x += rl;y += ud
172        l = fife.Location(self.agent.getLocation())
173        l.setLayerCoordinates(fife.ModelCoordinate(*tuple([x, y])))
174        return l
175
176    def onInstanceActionFinished(self, instance, action):
177        """What the NPC does when it has finished an action.
178           Called by the engine and required for InstanceActionListeners.
179           @type instance: fife.Instance
180           @param instance: self.agent (the NPC listener is listening for this
181            instance)
182           @type action: ???
183           @param action: ???
184           @return: None"""
185        if self.state == _AGENT_STATE_WANDER:
186            self.targetLoc = self.getTargetLocation()
187        self.idle()
188       
189   
190    def idle(self):
191        """Controls the NPC when it is idling. Different actions
192           based on the NPC's state.
193           @return: None"""
194        if self.state == _AGENT_STATE_NONE:
195            self.state = _AGENT_STATE_IDLE
196            self.agent.act('stand', self.agent.getFacingLocation())
197        elif self.state == _AGENT_STATE_IDLE:
198            self.targetLoc = self.getTargetLocation()
199            self.state = _AGENT_STATE_WANDER
200            self.agent.act('stand', self.agent.getFacingLocation())
201        elif self.state == _AGENT_STATE_WANDER:
202            self.parent.wander(self.targetLoc)
203            self.state = _AGENT_STATE_NONE
204        elif self.state == _AGENT_STATE_TALK:
205            self.agent.act('stand', self.pc.getLocation())
206
207class NonPlayerCharacter(GameObject, Living, Scriptable, CharStats):
208    """
209    NPC class
210    """
211    def __init__(self, ID, agent_layer = None, name = 'NPC', \
212                 text = 'A nonplayer character', **kwargs):
213        # init game object
214        GameObject.__init__( self, ID, **kwargs )
215        Living.__init__( self, **kwargs )
216        Scriptable.__init__( self, **kwargs )
217        CharStats.__init__( self, **kwargs )
218
219        self.is_NPC = True
220        self.inventory = None
221       
222        self.behaviour = NPCBehaviour(self, agent_layer)
223
224    def getLocation(self):
225        """ Get the NPC's position as a fife.Location object. Basically a
226            wrapper.
227            @rtype: fife.Location
228            @return: the location of the NPC"""
229        return self.behaviour.agent.getLocation()
230   
231    def wander(self, location):
232        """Nice slow movement for random walking.
233           @type location: fife.Location
234           @param location: Where the NPC will walk to.
235           @return: None"""
236        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
237
238    def run(self, location):
239        """Faster movement than walk.
240           @type location: fife.Location
241           @param location: Where the NPC will run to."""
242        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
243
244    def talk(self, pc):
245        """ Makes the NPC ready to talk to the PC
246            @return: None"""
247        self.behaviour.state = _AGENT_STATE_TALK
248        self.behaviour.pc = pc.behaviour.agent
249        self.behaviour.idle()
250   
251    def setup(self):
252        """@return: None"""
253        self.behaviour.attachToLayer(self.ID)
254
255    def start(self):
256        """@return: None"""
257        self.behaviour.idle()
Note: See TracBrowser for help on using the repository browser.