Mercurial > parpg-core
annotate src/parpg/components/container.py @ 108:2f928c913c78
Fixed item_type not being set when creating items.
author | KarstenBock@gmx.net |
---|---|
date | Fri, 23 Sep 2011 13:30:41 +0200 |
parents | d456334d09c0 |
children | 3564a46544bc |
rev | line source |
---|---|
34
5ac50245e42c
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 |
5ac50245e42c
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 |
5ac50245e42c
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 |
5ac50245e42c
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. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
5 # |
5ac50245e42c
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, |
5ac50245e42c
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 |
5ac50245e42c
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 |
5ac50245e42c
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. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
10 # |
5ac50245e42c
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 |
5ac50245e42c
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/>. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
13 |
92
0f659c7675f6
Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents:
89
diff
changeset
|
14 from parpg.bGrease.component import Component |
34
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
15 |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
16 class Container(Component): |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
17 """ |
5ac50245e42c
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. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
19 """ |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
20 |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
21 def __init__(self): |
43 | 22 Component.__init__(self, children=list, max_bulk=int) |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
23 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
24 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
25 class BulkLimitError(Exception): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
26 """Error that gets raised when the item would exceed the |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
27 bulk limit of the container.""" |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
28 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
29 def __init__(self, bulk, max_bulk): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
30 self.bulk = bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
31 self.max_bulk = max_bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
32 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
33 def __str__(self): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
34 return "Item would exceed the bulk limit of the container." |
46
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
35 |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
36 class NoFreeSlotError(Exception): |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
37 """Error that gets raised when the container has no free slots.""" |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
38 |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
39 def __str__(self): |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
40 return "Container can't hold any more items." |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
41 |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
42 def get_free_slot(container): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
43 """Returns the first slot of the container that is not occupied.""" |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
44 index = 0 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
45 for child in container.children: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
46 if not child: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
47 return index |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
48 index += 1 |
46
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
49 raise NoFreeSlotError |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
50 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
51 def get_total_bulk(container): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
52 """Returns the bulk of all items in the container.""" |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
53 total_bulk = 0 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
54 for child in container.children: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
55 if child: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
56 total_bulk += child.bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
57 return total_bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
58 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
59 def get_total_weight(container): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
60 """Returns the weight of all items in the container.""" |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
61 total_weight = 0 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
62 for child in container.children: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
63 if child: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
64 total_weight += child.weight |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
65 return total_weight |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
66 |
89
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
67 def get_item(container, slot_or_type): |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
68 """Returns the item that is in the slot, or has the given type.""" |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
69 if type(slot_or_type) == int: |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
70 if len(container.children) >= (slot_or_type + 1): |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
71 return container.children[slot_or_type] |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
72 else: |
107 | 73 for child in container.children: |
74 if child and child.item_type == slot_or_type: | |
89
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
75 return child |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
76 |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
77 return None |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
78 |
89
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
79 def remove_item(container, slot_or_type): |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
80 """Removes the item at the given slot, or with the given type.""" |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
81 if type(slot_or_type) == int: |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
82 item = get_item(container, slot_or_type) |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
83 if item: |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
84 container.children[slot_or_type] = None |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
85 item.container = None |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
86 item.slot = -1 |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
87 else: |
107 | 88 for child in container.children: |
89 if child and child.item_type == slot_or_type: | |
89
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
90 container.children[child.slot] = None |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
91 child.container = None |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
92 child.slot = -1 |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
93 |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
94 def take_item(container, slot_or_type): |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
95 """Moves the item at the given slot, or with the given type, |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
96 out of the container and returns it.""" |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
97 item = get_item(container, slot_or_type) |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
98 if item: |
89
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
99 remove_item(container, slot_or_type) |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
100 return item |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
101 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
102 def put_item(container, item, slot=-1): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
103 """Puts the item at the given slot in the container. |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
104 Returns the item previously at the slot.""" |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
105 if slot == -1: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
106 slot = get_free_slot(container) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
107 total_bulk = get_total_bulk(container) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
108 total_bulk += item.bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
109 old_item = get_item(container, slot) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
110 if old_item: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
111 total_bulk -= old_item.bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
112 if total_bulk > container.max_bulk: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
113 raise BulkLimitError(total_bulk, container.max_bulk) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
114 remove_item(container, slot) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
115 container.children[slot] = item |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
116 if item.container: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
117 remove_item(item.container, item.slot) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
118 item.container = container |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
119 item.slot = slot |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
120 return old_item |