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