comparison src/parpg/behaviours/npc.py @ 74:47e1345fbac2

Fixed NPC behaviour. NPCs are wandering around again.
author KarstenBock@gmx.net
date Wed, 07 Sep 2011 14:48:08 +0200
parents 58661f5b2f6b
children c3350fc9cd45
comparison
equal deleted inserted replaced
73:58661f5b2f6b 74:47e1345fbac2
12 12
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. 14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
15 15
16 from random import randrange 16 from random import randrange
17
18 from fife import fife
17 19
18 import base 20 import base
19 from base import BaseBehaviour 21 from base import BaseBehaviour
20 22
21 class NPCBehaviour(BaseBehaviour): 23 class NPCBehaviour(BaseBehaviour):
41 def getTargetLocation(self): 43 def getTargetLocation(self):
42 """@rtype: fife.Location 44 """@rtype: fife.Location
43 @return: NPC's position""" 45 @return: NPC's position"""
44 x = self.getX() 46 x = self.getX()
45 y = self.getY() 47 y = self.getY()
46 if self.state == _AGENT_STATE_WANDER: 48 if self.state == base._AGENT_STATE_WANDER:
47 """ Random Target Location """ 49 """ Random Target Location """
48 l = [0, 0] 50 l = [0, 0]
49 for i in range(len(l)): 51 for i in range(len(l)):
50 sign = randrange(0, 2) 52 sign = randrange(0, 2)
51 dist = randrange(self.distRange[0], self.distRange[1]) 53 dist = randrange(self.distRange[0], self.distRange[1])
67 @param instance: self.agent (the NPC listener is listening for this 69 @param instance: self.agent (the NPC listener is listening for this
68 instance) 70 instance)
69 @type action: ??? 71 @type action: ???
70 @param action: ??? 72 @param action: ???
71 @return: None""" 73 @return: None"""
72 if self.state == _AGENT_STATE_WANDER: 74 if self.state == base._AGENT_STATE_WANDER:
73 self.target_loc = self.getTargetLocation() 75 self.target_loc = self.getTargetLocation()
74 self.idle() 76 self.idle()
75 77
76 78
77 def idle(self): 79 def idle(self):
78 """Controls the NPC when it is idling. Different actions 80 """Controls the NPC when it is idling. Different actions
79 based on the NPC's state. 81 based on the NPC's state.
80 @return: None""" 82 @return: None"""
81 if self.state == _AGENT_STATE_NONE: 83 if self.state == base._AGENT_STATE_NONE:
82 self.state = _AGENT_STATE_IDLE 84 self.state = base._AGENT_STATE_IDLE
83 self.agent.act('stand', self.agent.getFacingLocation()) 85 self.agent.act('stand', self.agent.getFacingLocation())
84 elif self.state == _AGENT_STATE_IDLE: 86 elif self.state == base._AGENT_STATE_IDLE:
85 if self.wanderCounter > self.wanderRate: 87 if self.wanderCounter > self.wanderRate:
86 self.wanderCounter = 0 88 self.wanderCounter = 0
87 self.state = _AGENT_STATE_WANDER 89 self.state = base._AGENT_STATE_WANDER
88 else: 90 else:
89 self.wanderCounter += 1 91 self.wanderCounter += 1
90 self.state = _AGENT_STATE_NONE 92 self.state = base._AGENT_STATE_NONE
91 93
92 self.target_loc = self.getTargetLocation() 94 self.target_loc = self.getTargetLocation()
93 self.agent.act('stand', self.agent.getFacingLocation()) 95 self.agent.act('stand', self.agent.getFacingLocation())
94 elif self.state == _AGENT_STATE_WANDER: 96 elif self.state == base._AGENT_STATE_WANDER:
95 self.parent.wander(self.target_loc) 97 self.wander(self.target_loc)
96 self.state = _AGENT_STATE_NONE 98 self.state = base._AGENT_STATE_NONE
97 elif self.state == _AGENT_STATE_TALK: 99 elif self.state == base._AGENT_STATE_TALK:
98 self.agent.act('stand', self.pc.getLocation()) 100 self.agent.act('stand', self.pc.getLocation())
101
102 def wander(self, location):
103 """Nice slow movement for random walking.
104 @type location: fife.Location
105 @param location: Where the NPC will walk to.
106 @return: None"""
107 self.agent.move('walk', location, self.speed - 1)