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

Revision 563, 4.4 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"""Composite game object classes are kept here"""
19
20from base import GameObject, Container, Lockable, \
21                Scriptable, Trappable, Destructable, Carryable
22
23class ImmovableContainer(GameObject, Container, Lockable, Scriptable, 
24                         Trappable, Destructable):
25    """Composite class that can be used for crates, chests, etc."""
26    def __init__ (self, **kwargs):
27        GameObject   .__init__(self, **kwargs)
28        Container    .__init__(self, **kwargs)
29        Lockable     .__init__(self, **kwargs)
30        Scriptable   .__init__(self, **kwargs)
31        Trappable    .__init__(self, **kwargs)
32        Destructable .__init__(self, **kwargs)
33        self.blocking = True
34
35class CarryableContainer(Container, Carryable):
36    """Composite class that will be used for backpack, pouches, etc."""
37    def __init__ (self, **kwargs):
38        Container.__init__(self, **kwargs)
39        Carryable.__init__(self, **kwargs)
40        self.own_bulk = 0
41        self.own_weight = 0
42
43    def getWeight(self):
44        """Resulting weight of a container"""
45        return sum((item.weight for item in self.items.values()), 
46                   self.own_weight)
47
48    def setWeight(self, weight):
49        """Set container's own weight.
50        For compatibility with inherited methods"""
51        self.own_weight = weight
52
53    weight = property(getWeight, setWeight, "Total weight of container")
54
55    def getBulk(self):
56        """Resulting bulk of container"""
57        return self.getContentsBulk()+self.own_bulk
58
59    def setBulk(self, bulk):
60        """Set container's own bulk. For compatibility with inherited methods"""
61        self.own_bulk = bulk
62
63    bulk = property(getBulk, setBulk, "Total bulk of container")
64
65    def __repr__(self):
66        return "[%s" % self.name + str(reduce((lambda a, b: a + ', ' + \
67                                    str(self.items[b])), self.items, "")) + " ]"
68   
69class SingleItemContainer (CarryableContainer) :
70    """Container that can only store a single item.
71       This class can represent single-item inventory slots"""
72    def __init__ (self, **kwargs):
73        CarryableContainer.__init__(self, **kwargs)
74
75    def placeItem(self,item, index=None):
76        if len(self.items) > 0 :
77            raise self.SlotBusy ('%s is already busy' % self)
78        CarryableContainer.placeItem(self, item)
79
80class CarryableItem (GameObject, Carryable):
81    """Composite class that will be used for all carryable items"""
82    def __init__(self, **kwargs):
83        GameObject.__init__(self, **kwargs)
84        Carryable.__init__(self, **kwargs)
85
86    def prepareStateForSaving(self, state):
87        """Prepares state for saving
88        @type state: dictionary
89        @param state: State of the object 
90        """
91        del state["in_container"]
92
93    def getStateForSaving(self):
94        """Returns state for saving
95        @type state: dictionary
96        @param state: State of the object 
97        """
98        ret_dict = self.__dict__.copy()
99        self.prepareStateForSaving(ret_dict)
100        return ret_dict
101       
102class Door(GameObject, Lockable, Scriptable, Trappable):
103    """Composite class that can be used to create doors on a map."""
104    def __init__ (self, target_map_name = 'my-map',
105                  target_x = 0.0, target_y = 0.0, **kwargs):
106        GameObject.__init__(self, **kwargs)
107        Lockable.__init__(self, **kwargs)
108        Scriptable.__init__(self, **kwargs)
109        Trappable.__init__(self, **kwargs)
110        self.is_door = True
111        self.target_map_name = target_map_name
112        self.target_pos = (target_x, target_y)
113        self.blocking = True
114
115    def getStateForSaving(self):
116        """Returns state for saving
117        """
118        ret_dict = super(Door, self).getStateForSaving()
119        return ret_dict
Note: See TracBrowser for help on using the repository browser.