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