Mercurial > parpg-core
annotate src/parpg/objects/containers.py @ 33:32d8b4dd9d99
Added basic components
author | KarstenBock@gmx.net |
---|---|
date | Mon, 25 Jul 2011 13:39:01 +0200 |
parents | 1fd2201f5c36 |
children |
rev | line source |
---|---|
0
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
1 # This file is part of PARPG. |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
2 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
3 # PARPG is free software: you can redistribute it and/or modify |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
4 # it under the terms of the GNU General Public License as published by |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
5 # the Free Software Foundation, either version 3 of the License, or |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
6 # (at your option) any later version. |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
7 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
8 # PARPG is distributed in the hope that it will be useful, |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
11 # GNU General Public License for more details. |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
12 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
13 # You should have received a copy of the GNU General Public License |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
15 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
16 """Containes classes defining concrete container game objects like crates, |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
17 barrels, chests, etc.""" |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
18 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
19 __all__ = ["WoodenCrate", "Footlocker"] |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
20 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
21 _AGENT_STATE_NONE, _AGENT_STATE_OPENED, _AGENT_STATE_CLOSED, \ |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
22 _AGENT_STATE_OPENING, _AGENT_STATE_CLOSING = xrange(5) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
23 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
24 from composed import ImmovableContainer |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
25 from fife import fife |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
26 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
27 class WoodenCrate (ImmovableContainer): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
28 def __init__(self, object_id, name = 'Wooden Crate', |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
29 text = 'A battered crate', gfx = 'crate', **kwargs): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
30 ImmovableContainer.__init__(self, ID = object_id, name = name, |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
31 gfx = gfx, text = text, **kwargs) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
32 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
33 class ContainerBehaviour(fife.InstanceActionListener): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
34 def __init__(self, parent = None, agent_layer = None): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
35 fife.InstanceActionListener.__init__(self) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
36 self.parent = parent |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
37 self.layer = agent_layer |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
38 self.state = _AGENT_STATE_CLOSED |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
39 self.agent = None |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
40 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
41 def attachToLayer(self, agent_id): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
42 """ Attaches to a certain layer |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
43 @type agent_id: String |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
44 @param agent_id: ID of the layer to attach to. |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
45 @return: None""" |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
46 self.agent = self.layer.getInstance(agent_id) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
47 self.agent.addActionListener(self) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
48 self.state = _AGENT_STATE_CLOSED |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
49 self.agent.act('closed', self.agent.getLocation()) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
50 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
51 def onInstanceActionFinished(self, instance, action): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
52 """What the Actor does when it has finished an action. |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
53 Called by the engine and required for InstanceActionListeners. |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
54 @type instance: fife.Instance |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
55 @param instance: self.agent (the Actor listener is listening for this |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
56 instance) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
57 @type action: ??? |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
58 @param action: ??? |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
59 @return: None""" |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
60 if self.state == _AGENT_STATE_OPENING: |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
61 self.agent.act('opened', self.agent.getFacingLocation(), True) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
62 self.state = _AGENT_STATE_OPENED |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
63 if self.state == _AGENT_STATE_CLOSING: |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
64 self.agent.act('closed', self.agent.getFacingLocation(), True) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
65 self.state = _AGENT_STATE_CLOSED |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
66 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
67 def open (self): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
68 if self.state != _AGENT_STATE_OPENED and self.state != \ |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
69 _AGENT_STATE_OPENING: |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
70 self.agent.act('open', self.agent.getLocation()) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
71 self.state = _AGENT_STATE_OPENING |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
72 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
73 def close(self): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
74 if self.state != _AGENT_STATE_CLOSED and self.state != \ |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
75 _AGENT_STATE_CLOSING: |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
76 self.agent.act('close', self.agent.getLocation()) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
77 self.state = _AGENT_STATE_CLOSING |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
78 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
79 class Footlocker(ImmovableContainer): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
80 def __init__ (self, object_id, agent_layer=None, name = 'Footlocker', |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
81 text = 'A Footlocker', gfx = 'lock_box_metal01', **kwargs): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
82 ImmovableContainer.__init__(self, ID = object_id, name = name, |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
83 gfx = gfx, text = text, **kwargs) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
84 self.behaviour = None |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
85 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
86 self.attributes.append("AnimatedContainer") |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
87 self.createBehaviour(agent_layer) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
88 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
89 def prepareStateForSaving(self, state): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
90 """Prepares state for saving |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
91 @type state: dictionary |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
92 @param state: State of the object |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
93 """ |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
94 ImmovableContainer.prepareStateForSaving(self, state) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
95 del state["behaviour"] |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
96 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
97 def createBehaviour(self, layer): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
98 self.behaviour = ContainerBehaviour(self, layer) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
99 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
100 def setup(self): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
101 """@return: None""" |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
102 self.behaviour.attachToLayer(self.ID) |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
103 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
104 def open (self): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
105 super (Footlocker, self).open() |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
106 self.behaviour.open() |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
107 |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
108 def close(self): |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
109 super (Footlocker, self).close() |
1fd2201f5c36
Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
110 self.behaviour.close() |