Mercurial > parpg-core
comparison src/parpg/objects/containers.py @ 0:1fd2201f5c36
Initial commit of parpg-core.
author | M. George Hansen <technopolitica@gmail.com> |
---|---|
date | Sat, 14 May 2011 01:12:35 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1fd2201f5c36 |
---|---|
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 """Containes classes defining concrete container game objects like crates, | |
17 barrels, chests, etc.""" | |
18 | |
19 __all__ = ["WoodenCrate", "Footlocker"] | |
20 | |
21 _AGENT_STATE_NONE, _AGENT_STATE_OPENED, _AGENT_STATE_CLOSED, \ | |
22 _AGENT_STATE_OPENING, _AGENT_STATE_CLOSING = xrange(5) | |
23 | |
24 from composed import ImmovableContainer | |
25 from fife import fife | |
26 | |
27 class WoodenCrate (ImmovableContainer): | |
28 def __init__(self, object_id, name = 'Wooden Crate', | |
29 text = 'A battered crate', gfx = 'crate', **kwargs): | |
30 ImmovableContainer.__init__(self, ID = object_id, name = name, | |
31 gfx = gfx, text = text, **kwargs) | |
32 | |
33 class ContainerBehaviour(fife.InstanceActionListener): | |
34 def __init__(self, parent = None, agent_layer = None): | |
35 fife.InstanceActionListener.__init__(self) | |
36 self.parent = parent | |
37 self.layer = agent_layer | |
38 self.state = _AGENT_STATE_CLOSED | |
39 self.agent = None | |
40 | |
41 def attachToLayer(self, agent_id): | |
42 """ Attaches to a certain layer | |
43 @type agent_id: String | |
44 @param agent_id: ID of the layer to attach to. | |
45 @return: None""" | |
46 self.agent = self.layer.getInstance(agent_id) | |
47 self.agent.addActionListener(self) | |
48 self.state = _AGENT_STATE_CLOSED | |
49 self.agent.act('closed', self.agent.getLocation()) | |
50 | |
51 def onInstanceActionFinished(self, instance, action): | |
52 """What the Actor does when it has finished an action. | |
53 Called by the engine and required for InstanceActionListeners. | |
54 @type instance: fife.Instance | |
55 @param instance: self.agent (the Actor listener is listening for this | |
56 instance) | |
57 @type action: ??? | |
58 @param action: ??? | |
59 @return: None""" | |
60 if self.state == _AGENT_STATE_OPENING: | |
61 self.agent.act('opened', self.agent.getFacingLocation(), True) | |
62 self.state = _AGENT_STATE_OPENED | |
63 if self.state == _AGENT_STATE_CLOSING: | |
64 self.agent.act('closed', self.agent.getFacingLocation(), True) | |
65 self.state = _AGENT_STATE_CLOSED | |
66 | |
67 def open (self): | |
68 if self.state != _AGENT_STATE_OPENED and self.state != \ | |
69 _AGENT_STATE_OPENING: | |
70 self.agent.act('open', self.agent.getLocation()) | |
71 self.state = _AGENT_STATE_OPENING | |
72 | |
73 def close(self): | |
74 if self.state != _AGENT_STATE_CLOSED and self.state != \ | |
75 _AGENT_STATE_CLOSING: | |
76 self.agent.act('close', self.agent.getLocation()) | |
77 self.state = _AGENT_STATE_CLOSING | |
78 | |
79 class Footlocker(ImmovableContainer): | |
80 def __init__ (self, object_id, agent_layer=None, name = 'Footlocker', | |
81 text = 'A Footlocker', gfx = 'lock_box_metal01', **kwargs): | |
82 ImmovableContainer.__init__(self, ID = object_id, name = name, | |
83 gfx = gfx, text = text, **kwargs) | |
84 self.behaviour = None | |
85 | |
86 self.attributes.append("AnimatedContainer") | |
87 self.createBehaviour(agent_layer) | |
88 | |
89 def prepareStateForSaving(self, state): | |
90 """Prepares state for saving | |
91 @type state: dictionary | |
92 @param state: State of the object | |
93 """ | |
94 ImmovableContainer.prepareStateForSaving(self, state) | |
95 del state["behaviour"] | |
96 | |
97 def createBehaviour(self, layer): | |
98 self.behaviour = ContainerBehaviour(self, layer) | |
99 | |
100 def setup(self): | |
101 """@return: None""" | |
102 self.behaviour.attachToLayer(self.ID) | |
103 | |
104 def open (self): | |
105 super (Footlocker, self).open() | |
106 self.behaviour.open() | |
107 | |
108 def close(self): | |
109 super (Footlocker, self).close() | |
110 self.behaviour.close() |