Mercurial > parpg-core
comparison src/parpg/behaviours/npc.py @ 55:4311a0a5378c
Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
author | KarstenBock@gmx.net |
---|---|
date | Mon, 05 Sep 2011 13:02:05 +0200 |
parents | |
children | 097cceb06b92 |
comparison
equal
deleted
inserted
replaced
54:5e27c24acaef | 55:4311a0a5378c |
---|---|
1 # This file is part of PARPG. | |
2 | |
3 # PARPG is free software: you can redistribute it and/or modify | |
4 # it under the terms of the GNU General Public License as published by | |
5 # the Free Software Foundation, either version 3 of the License, or | |
6 # (at your option) any later version. | |
7 | |
8 # PARPG is distributed in the hope that it will be useful, | |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 # GNU General Public License for more details. | |
12 | |
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/>. | |
15 | |
16 from random import randrange | |
17 | |
18 from base import * | |
19 | |
20 class NPCBehaviour(BaseBehaviour): | |
21 """This is a basic NPC behaviour""" | |
22 def __init__(self, Parent=None, Layer=None): | |
23 super(NPCBehaviour, self).__init__(Layer) | |
24 | |
25 self.parent = Parent | |
26 self.state = _AGENT_STATE_NONE | |
27 self.pc = None | |
28 self.target_loc = None | |
29 self.nextAction = None | |
30 | |
31 # hard code these for now | |
32 self.distRange = (2, 4) | |
33 # these are parameters to lower the rate of wandering | |
34 # wander rate is the number of "IDLEs" before a wander step | |
35 # this could be set for individual NPCs at load time | |
36 # or thrown out altogether. | |
37 self.wanderCounter = 0 | |
38 self.wanderRate = 9 | |
39 | |
40 def getTargetLocation(self): | |
41 """@rtype: fife.Location | |
42 @return: NPC's position""" | |
43 x = self.getX() | |
44 y = self.getY() | |
45 if self.state == _AGENT_STATE_WANDER: | |
46 """ Random Target Location """ | |
47 l = [0, 0] | |
48 for i in range(len(l)): | |
49 sign = randrange(0, 2) | |
50 dist = randrange(self.distRange[0], self.distRange[1]) | |
51 if sign == 0: | |
52 dist *= -1 | |
53 l[i] = dist | |
54 x += l[0] | |
55 y += l[1] | |
56 # Random walk is | |
57 # rl = randint(-1, 1);ud = randint(-1, 1);x += rl;y += ud | |
58 l = fife.Location(self.agent.getLocation()) | |
59 l.setLayerCoordinates(fife.ModelCoordinate(x, y)) | |
60 return l | |
61 | |
62 def onInstanceActionFinished(self, instance, action): | |
63 """What the NPC does when it has finished an action. | |
64 Called by the engine and required for InstanceActionListeners. | |
65 @type instance: fife.Instance | |
66 @param instance: self.agent (the NPC listener is listening for this | |
67 instance) | |
68 @type action: ??? | |
69 @param action: ??? | |
70 @return: None""" | |
71 if self.state == _AGENT_STATE_WANDER: | |
72 self.target_loc = self.getTargetLocation() | |
73 self.idle() | |
74 | |
75 | |
76 def idle(self): | |
77 """Controls the NPC when it is idling. Different actions | |
78 based on the NPC's state. | |
79 @return: None""" | |
80 if self.state == _AGENT_STATE_NONE: | |
81 self.state = _AGENT_STATE_IDLE | |
82 self.agent.act('stand', self.agent.getFacingLocation()) | |
83 elif self.state == _AGENT_STATE_IDLE: | |
84 if self.wanderCounter > self.wanderRate: | |
85 self.wanderCounter = 0 | |
86 self.state = _AGENT_STATE_WANDER | |
87 else: | |
88 self.wanderCounter += 1 | |
89 self.state = _AGENT_STATE_NONE | |
90 | |
91 self.target_loc = self.getTargetLocation() | |
92 self.agent.act('stand', self.agent.getFacingLocation()) | |
93 elif self.state == _AGENT_STATE_WANDER: | |
94 self.parent.wander(self.target_loc) | |
95 self.state = _AGENT_STATE_NONE | |
96 elif self.state == _AGENT_STATE_TALK: | |
97 self.agent.act('stand', self.pc.getLocation()) |