Mercurial > parpg-source
annotate behaviours/base.py @ 89:939984cff702
Added Base component, which has a saveable_fields property. It is supposed to be derived from, thus it is not in the components list.
author | KarstenBock@gmx.net |
---|---|
date | Tue, 27 Sep 2011 14:58:03 +0200 |
parents | a9cc5559ec2a |
children | 57f1cff9a75d |
rev | line source |
---|---|
86
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
1 # This file is part of PARPG. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
2 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
3 # PARPG is free software: you can redistribute it and/or modify |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
4 # it under the terms of the GNU General Public License as published by |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
5 # the Free Software Foundation, either version 3 of the License, or |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
6 # (at your option) any later version. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
7 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
8 # PARPG is distributed in the hope that it will be useful, |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
11 # GNU General Public License for more details. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
12 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
13 # You should have received a copy of the GNU General Public License |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
15 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
16 from fife import fife |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
17 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
18 _AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
19 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
20 class BaseBehaviour (fife.InstanceActionListener): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
21 """Fife agent listener""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
22 def __init__(self): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
23 fife.InstanceActionListener.__init__(self) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
24 self.agent = None |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
25 self.state = None |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
26 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
27 def attachToLayer(self, agent_ID, layer): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
28 """Attaches to a certain layer |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
29 @type agent_ID: String |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
30 @param agent_ID: ID of the layer to attach to. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
31 @type layer: Fife layer |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
32 @param layer: Layer of the agent to attach the behaviour to |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
33 @return: None""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
34 self.agent = layer.getInstance(agent_ID) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
35 self.agent.addActionListener(self) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
36 self.state = _AGENT_STATE_NONE |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
37 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
38 def getX(self): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
39 """Get the NPC's x position on the map. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
40 @rtype: integer" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
41 @return: the x coordinate of the NPC's location""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
42 return self.agent.getLocation().getLayerCoordinates().x |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
43 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
44 def getY(self): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
45 """Get the NPC's y position on the map. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
46 @rtype: integer |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
47 @return: the y coordinate of the NPC's location""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
48 return self.agent.getLocation().getLayerCoordinates().y |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
49 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
50 def onNewMap(self, layer): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
51 """Sets the agent onto the new layer.""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
52 if self.agent is not None: |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
53 self.agent.removeActionListener(self) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
54 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
55 self.agent = layer.getInstance(self.parent.general.identifier) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
56 self.agent.addActionListener(self) |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
57 self.state = _AGENT_STATE_NONE |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
58 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
59 def idle(self): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
60 """@return: None""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
61 self.state = _AGENT_STATE_IDLE |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
62 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
63 def onInstanceActionFinished(self, instance, action): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
64 """@type instance: ??? |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
65 @param instance: ??? |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
66 @type action: ??? |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
67 @param action: ??? |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
68 @return: None""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
69 # First we reset the next behavior |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
70 act = self.nextAction |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
71 self.nextAction = None |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
72 self.idle() |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
73 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
74 if act: |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
75 act.execute() |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
76 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
77 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
78 def getLocation(self): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
79 """Get the agents position as a fife.Location object. |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
80 @rtype: fife.Location |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
81 @return: the location of the agent""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
82 return self.agent.getLocation() |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
83 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
84 |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
85 def talk(self, pc): |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
86 """Makes the agent ready to talk to the PC |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
87 @return: None""" |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
88 self.state = _AGENT_STATE_TALK |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
89 self.pc = pc.behaviour.agent |
a9cc5559ec2a
Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents:
61
diff
changeset
|
90 self.idle() |
52
3dfd26b1c7ef
Modifications to make the player agent move around by clicking with the mouse.
KarstenBock@gmx.net
parents:
50
diff
changeset
|
91 |