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

Revision 611, 5.3 KB checked in by beliar, 10 years ago (diff)

Patch by Beliar.

  • Replaced old attribute system. Instead of is_<attribute> variables there is now a list of attributes that a class has. If the value of the attribute was needed its now saved in another variable. (e.g. is_living became lives)
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env 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, Trapable, Destructable, Carryable, \
22                Usable
23
24class ImmovableContainer(GameObject, Container, Lockable, Scriptable, 
25                         Trapable, Destructable):
26    """Composite class that can be used for crates, chests, etc."""
27    def __init__ (self, **kwargs):
28        GameObject   .__init__(self, **kwargs)
29        Container    .__init__(self, **kwargs)
30        Lockable     .__init__(self, **kwargs)
31        Scriptable   .__init__(self, **kwargs)
32        Trapable    .__init__(self, **kwargs)
33        Destructable .__init__(self, **kwargs)
34        self.blocking = True
35
36class SingleItemContainer (Container) :
37    """Container that can only store a single item.
38       This class can represent single-item inventory slots"""
39    def __init__ (self, **kwargs):
40        Container.__init__(self, **kwargs)
41
42    def placeItem(self,item, index=None):
43        if len(self.items) > 0 :
44            raise self.SlotBusy ('%s is already busy' % self)
45        Container.placeItem(self, item)
46   
47class CarryableItem (GameObject, Carryable, Usable):
48    """Composite class that will be used for all carryable items"""
49    def __init__(self, item_type, **kwargs):
50        GameObject.__init__(self, **kwargs)
51        Carryable.__init__(self, **kwargs)
52        Usable.__init__(self, **kwargs)
53        self.item_type = item_type
54
55    def prepareStateForSaving(self, state):
56        """Prepares state for saving
57        @type state: dictionary
58        @param state: State of the object 
59        """
60        super(CarryableItem, self).prepareStateForSaving(state)
61        if state.has_key("in_container"):
62            del state["in_container"]
63        if state.has_key("on_map"):
64            del state["on_map"]
65        del state["agent"]
66
67    def getStateForSaving(self):
68        """Returns state for saving
69        @type state: dictionary
70        @param state: State of the object 
71        """
72        ret_dict = self.__dict__.copy()
73        self.prepareStateForSaving(ret_dict)
74        return ret_dict
75
76class CarryableContainer(Container, CarryableItem):
77    """Composite class that will be used for backpack, pouches, etc."""
78    def __init__ (self, item_type, **kwargs):
79        Container.__init__(self, **kwargs)
80        CarryableItem.__init__(self, item_type, **kwargs)
81        self.own_bulk = 0
82        self.own_weight = 0
83
84    def getWeight(self):
85        """Resulting weight of a container"""
86        return sum((item.weight for item in self.items.values()), 
87                   self.own_weight)
88
89    def setWeight(self, weight):
90        """Set container's own weight.
91        For compatibility with inherited methods"""
92        self.own_weight = weight
93
94    weight = property(getWeight, setWeight, "Total weight of container")
95
96    def getBulk(self):
97        """Resulting bulk of container"""
98        return self.getContentsBulk()+self.own_bulk
99
100    def setBulk(self, bulk):
101        """Set container's own bulk. For compatibility with inherited methods"""
102        self.own_bulk = bulk
103
104    bulk = property(getBulk, setBulk, "Total bulk of container")
105   
106    def __repr__(self):
107        return "[%s" % self.name + str(reduce((lambda a, b: a + ', ' + \
108                                    str(self.items[b])), self.items, "")) + " ]"
109
110    def getStateForSaving(self):
111        """Returns state for saving
112        @type state: dictionary
113        @param state: State of the object 
114        """
115        state = Container.getStateForSaving(self)
116        state.update(CarryableItem.getStateForSaving(self))
117        return state
118
119class CarryableSingleItemContainer (SingleItemContainer, CarryableContainer) :
120    """Container that can only store a single item.
121       This class can represent single-item inventory slots"""
122    def __init__ (self, item_type, **kwargs):
123        SingleItemContainer.__init__(self, **kwargs)
124        CarryableContainer.__init__(self, item_type, **kwargs)
125       
126class Door(GameObject, Lockable, Scriptable, Trapable):
127    """Composite class that can be used to create doors on a map."""
128    def __init__ (self, target_map_name = 'my-map',
129                  target_x = 0.0, target_y = 0.0, **kwargs):
130        GameObject.__init__(self, **kwargs)
131        Lockable.__init__(self, **kwargs)
132        Scriptable.__init__(self, **kwargs)
133        Trapable.__init__(self, **kwargs)
134        self.attributes.append("door")
135        self.target_map_name = target_map_name
136        self.target_pos = (target_x, target_y)
137        self.blocking = True
138
139    def getStateForSaving(self):
140        """Returns state for saving
141        """
142        ret_dict = super(Door, self).getStateForSaving()
143        return ret_dict
Note: See TracBrowser for help on using the repository browser.