Mercurial > parpg-core
comparison src/parpg/components/container.py @ 44:9f631144124f
Added functions for container components
author | KarstenBock@gmx.net |
---|---|
date | Thu, 01 Sep 2011 23:26:50 +0200 |
parents | 1cc037593517 |
children | 3bacef35b252 |
comparison
equal
deleted
inserted
replaced
43:1cc037593517 | 44:9f631144124f |
---|---|
18 Component that allows an entity to contain one or more child entities. | 18 Component that allows an entity to contain one or more child entities. |
19 """ | 19 """ |
20 | 20 |
21 def __init__(self): | 21 def __init__(self): |
22 Component.__init__(self, children=list, max_bulk=int) | 22 Component.__init__(self, children=list, max_bulk=int) |
23 | |
24 | |
25 class BulkLimitError(Exception): | |
26 """Error that gets raised when the item would exceed the | |
27 bulk limit of the container.""" | |
28 | |
29 def __init__(self, bulk, max_bulk): | |
30 self.bulk = bulk | |
31 self.max_bulk = max_bulk | |
32 | |
33 def __str__(self): | |
34 return "Item would exceed the bulk limit of the container." | |
35 | |
36 def get_free_slot(container): | |
37 """Returns the first slot of the container that is not occupied.""" | |
38 index = 0 | |
39 for child in container.children: | |
40 if not child: | |
41 return index | |
42 index += 1 | |
43 container.children.append(None) | |
44 return index | |
45 | |
46 def get_total_bulk(container): | |
47 """Returns the bulk of all items in the container.""" | |
48 total_bulk = 0 | |
49 for child in container.children: | |
50 if child: | |
51 total_bulk += child.bulk | |
52 return total_bulk | |
53 | |
54 def get_total_weight(container): | |
55 """Returns the weight of all items in the container.""" | |
56 total_weight = 0 | |
57 for child in container.children: | |
58 if child: | |
59 total_weight += child.weight | |
60 return total_weight | |
61 | |
62 def get_item(container, slot): | |
63 """Returns the item that is in the slot.""" | |
64 if len(container.children) >= (slot + 1): | |
65 return container.children[slot] | |
66 return None | |
67 | |
68 def remove_item(container, slot): | |
69 """Removes the item at the given slot.""" | |
70 item = get_item(container, slot) | |
71 if item: | |
72 container.children[slot] = None | |
73 item.container = None | |
74 item.slot = -1 | |
75 | |
76 def take_item(container, slot): | |
77 """Moves the item at the given slot out of the container and returns it.""" | |
78 item = get_item(container, slot) | |
79 if item: | |
80 remove_item(container, slot) | |
81 return item | |
82 | |
83 def put_item(container, item, slot=-1): | |
84 """Puts the item at the given slot in the container. | |
85 Returns the item previously at the slot.""" | |
86 if slot == -1: | |
87 slot = get_free_slot(container) | |
88 total_bulk = get_total_bulk(container) | |
89 total_bulk += item.bulk | |
90 old_item = get_item(container, slot) | |
91 if old_item: | |
92 total_bulk -= old_item.bulk | |
93 if total_bulk > container.max_bulk: | |
94 raise BulkLimitError(total_bulk, container.max_bulk) | |
95 remove_item(container, slot) | |
96 container.children[slot] = item | |
97 if item.container: | |
98 remove_item(item.container, item.slot) | |
99 item.container = container | |
100 item.slot = slot | |
101 return old_item |