annotate src/parpg/objects/composed.py @ 31:ff198b1168af

TimeDelta parameter is now correct
author KarstenBock@gmx.net
date Thu, 14 Jul 2011 10:44:51 +0200
parents 1fd2201f5c36
children
rev   line source
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
1 # This file is part of PARPG.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
2
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
3 # PARPG is free software: you can redistribute it and/or modify
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
6 # (at your option) any later version.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
7
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
8 # PARPG is distributed in the hope that it will be useful,
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
11 # GNU General Public License for more details.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
12
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
15
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
16 """Composite game object classes are kept here"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
17
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
18 from base import GameObject, Container, Lockable, \
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
19 Scriptable, Trapable, Destructable, Carryable, \
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
20 Usable
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
21
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
22 class ImmovableContainer(GameObject, Container, Lockable, Scriptable,
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
23 Trapable, Destructable):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
24 """Composite class that can be used for crates, chests, etc."""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
25 def __init__ (self, **kwargs):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
26 GameObject .__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
27 Container .__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
28 Lockable .__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
29 Scriptable .__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
30 Trapable .__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
31 Destructable .__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
32 self.blocking = True
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
33
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
34 class SingleItemContainer (Container) :
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
35 """Container that can only store a single item.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
36 This class can represent single-item inventory slots"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
37 def __init__ (self, **kwargs):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
38 Container.__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
39
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
40 def placeItem(self,item, index=None):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
41 if len(self.items) > 0 :
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
42 raise self.SlotBusy ('%s is already busy' % self)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
43 Container.placeItem(self, item)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
44
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
45 class CarryableItem (GameObject, Carryable, Usable):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
46 """Composite class that will be used for all carryable items"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
47 def __init__(self, item_type, **kwargs):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
48 GameObject.__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
49 Carryable.__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
50 Usable.__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
51 self.item_type = item_type
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
52
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
53 def prepareStateForSaving(self, state):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
54 """Prepares state for saving
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
55 @type state: dictionary
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
56 @param state: State of the object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
57 """
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
58 super(CarryableItem, self).prepareStateForSaving(state)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
59 if state.has_key("in_container"):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
60 del state["in_container"]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
61 if state.has_key("on_map"):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
62 del state["on_map"]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
63 if state.has_key("agent"):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
64 del state["agent"]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
65
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
66 def getStateForSaving(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
67 """Returns state for saving
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
68 @type state: dictionary
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
69 @param state: State of the object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
70 """
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
71 ret_dict = self.__dict__.copy()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
72 self.prepareStateForSaving(ret_dict)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
73 return ret_dict
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
74
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
75 class CarryableContainer(Container, CarryableItem):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
76 """Composite class that will be used for backpack, pouches, etc."""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
77 def __init__ (self, item_type, **kwargs):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
78 Container.__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
79 CarryableItem.__init__(self, item_type, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
80 self.own_bulk = 0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
81 self.own_weight = 0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
82
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
83 def getWeight(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
84 """Resulting weight of a container"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
85 return sum((item.weight for item in self.items.values()),
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
86 self.own_weight)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
87
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
88 def setWeight(self, weight):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
89 """Set container's own weight.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
90 For compatibility with inherited methods"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
91 self.own_weight = weight
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
92
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
93 weight = property(getWeight, setWeight, "Total weight of container")
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
94
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
95 def getBulk(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
96 """Resulting bulk of container"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
97 return self.getContentsBulk()+self.own_bulk
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
98
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
99 def setBulk(self, bulk):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
100 """Set container's own bulk. For compatibility with inherited methods"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
101 self.own_bulk = bulk
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
102
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
103 bulk = property(getBulk, setBulk, "Total bulk of container")
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
104
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
105 def __repr__(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
106 return "[%s" % self.name + str(reduce((lambda a, b: a + ', ' + \
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
107 str(self.items[b])), self.items, "")) + " ]"
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
108
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
109 def getStateForSaving(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
110 """Returns state for saving
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
111 @type state: dictionary
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
112 @param state: State of the object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
113 """
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
114 state = Container.getStateForSaving(self)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
115 if not state.has_key("attributes"):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
116 state["attributes"] = []
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
117 state["attributes"].append("Container")
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
118 state.update(CarryableItem.getStateForSaving(self))
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
119 return state
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
120
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
121 class CarryableSingleItemContainer (SingleItemContainer, CarryableContainer) :
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
122 """Container that can only store a single item.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
123 This class can represent single-item inventory slots"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
124 def __init__ (self, item_type, **kwargs):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
125 SingleItemContainer.__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
126 CarryableContainer.__init__(self, item_type, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
127
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
128 class Door(GameObject, Lockable, Scriptable, Trapable):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
129 """Composite class that can be used to create doors on a map."""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
130 def __init__ (self, target_map_name = 'my-map',
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
131 target_x = 0.0, target_y = 0.0, **kwargs):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
132 GameObject.__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
133 Lockable.__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
134 Scriptable.__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
135 Trapable.__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
136 self.attributes.append("door")
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
137 self.target_map_name = target_map_name
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
138 self.target_pos = (target_x, target_y)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
139 self.blocking = True
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
140
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
141 def getStateForSaving(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
142 """Returns state for saving
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
143 """
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
144 ret_dict = super(Door, self).getStateForSaving()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
145 return ret_dict