Mercurial > parpg-source
annotate components/container.py @ 22:d8c17766895b
Added functions for container components
author | KarstenBock@gmx.net |
---|---|
date | Thu, 01 Sep 2011 23:26:50 +0200 |
parents | def430d59281 |
children | eab8af30dfc7 |
rev | line source |
---|---|
12
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
1 # This program is free software: you can redistribute it and/or modify |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
2 # it under the terms of the GNU General Public License as published by |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
3 # the Free Software Foundation, either version 3 of the License, or |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
4 # (at your option) any later version. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
5 # |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
6 # This program is distributed in the hope that it will be useful, |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
9 # GNU General Public License for more details. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
10 # |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
11 # You should have received a copy of the GNU General Public License |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
12 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
13 |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
14 from parpg.grease.component import Component |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
15 |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
16 class Container(Component): |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
17 """ |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
18 Component that allows an entity to contain one or more child entities. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
19 """ |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
20 |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
21 def __init__(self): |
21 | 22 Component.__init__(self, children=list, max_bulk=int) |
22
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
23 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
24 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
25 class BulkLimitError(Exception): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
26 """Error that gets raised when the item would exceed the |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
27 bulk limit of the container.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
28 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
29 def __init__(self, bulk, max_bulk): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
30 self.bulk = bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
31 self.max_bulk = max_bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
32 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
33 def __str__(self): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
34 return "Item would exceed the bulk limit of the container." |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
35 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
36 def get_free_slot(container): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
37 """Returns the first slot of the container that is not occupied.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
38 index = 0 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
39 for child in container.children: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
40 if not child: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
41 return index |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
42 index += 1 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
43 container.children.append(None) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
44 return index |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
45 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
46 def get_total_bulk(container): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
47 """Returns the bulk of all items in the container.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
48 total_bulk = 0 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
49 for child in container.children: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
50 if child: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
51 total_bulk += child.bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
52 return total_bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
53 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
54 def get_total_weight(container): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
55 """Returns the weight of all items in the container.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
56 total_weight = 0 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
57 for child in container.children: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
58 if child: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
59 total_weight += child.weight |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
60 return total_weight |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
61 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
62 def get_item(container, slot): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
63 """Returns the item that is in the slot.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
64 if len(container.children) >= (slot + 1): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
65 return container.children[slot] |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
66 return None |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
67 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
68 def remove_item(container, slot): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
69 """Removes the item at the given slot.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
70 item = get_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
71 if item: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
72 container.children[slot] = None |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
73 item.container = None |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
74 item.slot = -1 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
75 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
76 def take_item(container, slot): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
77 """Moves the item at the given slot out of the container and returns it.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
78 item = get_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
79 if item: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
80 remove_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
81 return item |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
82 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
83 def put_item(container, item, slot=-1): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
84 """Puts the item at the given slot in the container. |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
85 Returns the item previously at the slot.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
86 if slot == -1: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
87 slot = get_free_slot(container) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
88 total_bulk = get_total_bulk(container) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
89 total_bulk += item.bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
90 old_item = get_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
91 if old_item: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
92 total_bulk -= old_item.bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
93 if total_bulk > container.max_bulk: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
94 raise BulkLimitError(total_bulk, container.max_bulk) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
95 remove_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
96 container.children[slot] = item |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
97 if item.container: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
98 remove_item(item.container, item.slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
99 item.container = container |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
100 item.slot = slot |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
101 return old_item |