comparison components/container.py @ 62:bf63da4b27f5

Added item_type attribute to the containable component.
author KarstenBock@gmx.net
date Wed, 21 Sep 2011 15:05:12 +0200
parents ff3e395abf91
children e856b604b650
comparison
equal deleted inserted replaced
61:2727d6b78978 62:bf63da4b27f5
62 for child in container.children: 62 for child in container.children:
63 if child: 63 if child:
64 total_weight += child.weight 64 total_weight += child.weight
65 return total_weight 65 return total_weight
66 66
67 def get_item(container, slot): 67 def get_item(container, slot_or_type):
68 """Returns the item that is in the slot.""" 68 """Returns the item that is in the slot, or has the given type."""
69 if len(container.children) >= (slot + 1): 69 if type(slot_or_type) == int:
70 return container.children[slot] 70 if len(container.children) >= (slot_or_type + 1):
71 return container.children[slot_or_type]
72 else:
73 for item in container.children:
74 if child and if child.type == slot_or_type:
75 return child
76
71 return None 77 return None
72 78
73 def remove_item(container, slot): 79 def remove_item(container, slot_or_type):
74 """Removes the item at the given slot.""" 80 """Removes the item at the given slot, or with the given type."""
75 item = get_item(container, slot) 81 if type(slot_or_type) == int:
82 item = get_item(container, slot_or_type)
83 if item:
84 container.children[slot_or_type] = None
85 item.container = None
86 item.slot = -1
87 else:
88 for item in container.children:
89 if child and if child.type == slot_or_type:
90 container.children[child.slot] = None
91 child.container = None
92 child.slot = -1
93
94 def take_item(container, slot_or_type):
95 """Moves the item at the given slot, or with the given type,
96 out of the container and returns it."""
97 item = get_item(container, slot_or_type)
76 if item: 98 if item:
77 container.children[slot] = None 99 remove_item(container, slot_or_type)
78 item.container = None
79 item.slot = -1
80
81 def take_item(container, slot):
82 """Moves the item at the given slot out of the container and returns it."""
83 item = get_item(container, slot)
84 if item:
85 remove_item(container, slot)
86 return item 100 return item
87 101
88 def put_item(container, item, slot=-1): 102 def put_item(container, item, slot=-1):
89 """Puts the item at the given slot in the container. 103 """Puts the item at the given slot in the container.
90 Returns the item previously at the slot.""" 104 Returns the item previously at the slot."""