source: trunk/game/scripts/objects/containers.py @ 563

Revision 563, 4.3 KB checked in by beliar, 10 years ago (diff)

Ticket #200: Patch by Beliar.

  • Merging map_loading_change branch back into trunk

fixes[s:trac, t:200]

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