source: trunk/PARPG/scripts/agents/npc.py @ 181

Revision 153, 5.1 KB checked in by meggie_parpg, 11 years ago (diff)

Added beginnings of talking functionality to the npc and hero classes. Made doors act the same way that other objects act (when it comes to item actions). Most of the door stuff will probably have to be changed when gameData/gameState stuff gets straightened out. Also, the context menu should probably close when an action is selected, but that seems to by a nasty pychan thing.

  • 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
18import fife
19from settings import Setting
20from random import randrange
21
22TDS = Setting()
23_STATE_NONE, _STATE_IDLE, _STATE_WANDER, _STATE_TALK = xrange(4)
24
25class NPC(fife.InstanceActionListener):
26    """This is the class we use for all NPCs"""
27    def __init__(self, text, agent_name, layer):
28        """Init function.
29           @type text: string
30           @param text: The text to draw when character is right clicked.
31           @type agent_name: string
32           @param agent_name: the ID of the NPC
33           @type layer: ???
34           @param layer: ???
35           @return: None"""
36        fife.InstanceActionListener.__init__(self)
37        self.text = text
38        self.id = agent_name
39        # a fife.Instance
40        self.agent = layer.getInstance(agent_name)
41        self.agent.addActionListener(self)
42        self.state = _STATE_NONE
43        self.speed = float(TDS.readSetting("PCSpeed"))-1
44        # hard code this for now
45        self.distRange = (2, 4)
46        self.targetLoc = self.getTargetLocation() 
47        self.text = text
48        # good to have the pc in case we ever need to follow it
49        self.pc = layer.getInstance('PC')
50
51    def getX(self):
52        """Get the NPC's x position on the map.
53           @rtype: integer"
54           @return: the x coordinate of the NPC's location"""
55        return self.agent.getLocation().getLayerCoordinates().x
56
57    def getY(self):
58        """Get the NPC's y position on the map.
59           @rtype: integer
60           @return: the y coordinate of the NPC's location"""
61        return self.agent.getLocation().getLayerCoordinates().y
62
63    def getLocation(self):
64        """ Get the NPC's position as a fife.Location object. Basically a
65            wrapper.
66            @rtype: fife.Location
67            @return: the location of the NPC"""
68        return self.agent.getLocation()
69
70    def getTargetLocation(self):
71        """@rtype: fife.Location
72           @return: NPC's position"""
73        x = self.getX()
74        y = self.getY()
75        if self.state == _STATE_WANDER:
76            """ Random Target Location """
77            l = [0, 0]
78            for i in range(len(l)):
79                sign = randrange(0, 2)
80                dist = randrange(self.distRange[0], self.distRange[1])
81                if sign == 0:
82                    dist *= -1
83                l[i] = dist
84            x += l[0]
85            y += l[1]
86            # Random walk is
87            # rl = randint(-1, 1);ud = randint(-1, 1);x += rl;y += ud
88        l = fife.Location(self.agent.getLocation())
89        l.setLayerCoordinates(fife.ModelCoordinate(*tuple([x, y])))
90        return l
91
92    def onInstanceActionFinished(self, instance, action):
93        """What the NPC does when it has finished an action.
94           Called by the engine and required for InstanceActionListeners.
95           @type instance: fife.Instance
96           @param instance: self.agent (the NPC listener is listening for this
97            instance)
98           @type action: ???
99           @param action: ???
100           @return: None"""
101        if self.state == _STATE_WANDER:
102            self.targetLoc = self.getTargetLocation()
103        self.idle()
104
105    def start(self):
106        """@return: None"""
107        self.idle()
108
109    def idle(self):
110        """Controls the NPC when it is idling. Different actions
111           based on the NPC's state.
112           @return: None"""
113        if self.state == _STATE_NONE:
114            self.state = _STATE_IDLE
115            self.agent.act('stand', self.agent.getFacingLocation())
116        elif self.state == _STATE_IDLE:
117            self.targetLoc = self.getTargetLocation()
118            self.state = _STATE_WANDER
119            self.agent.act('stand', self.agent.getFacingLocation())
120        elif self.state == _STATE_WANDER:
121            self.wander(self.targetLoc)
122            self.state = _STATE_NONE
123        elif self.state == _STATE_TALK:
124            self.agent.act('stand', self.pc.getLocation())
125
126    def wander(self, location):
127        """Nice slow movement for random walking.
128           @type location: fife.Location
129           @param location: Where the NPC will walk to.
130           @return: None"""
131        self.agent.move('walk', location, self.speed-1)
132
133    def run(self, location):
134        """Faster movement than walk.
135           @type location: fife.Location
136           @param location: Where the NPC will run to."""
137        self.agent.move('run', location, self.speed+1)
138
139    def talk(self):
140        """ Makes the NPC ready to talk to the PC
141            @return: None"""
142        self.state = _STATE_TALK
143        self.idle()
Note: See TracBrowser for help on using the repository browser.