# HG changeset patch # User KarstenBock@gmx.net # Date 1316610312 -7200 # Node ID bf63da4b27f53a483e9f7304e756d3ac0db17ebf # Parent 2727d6b7897808cbc633fc23dda873f0240303a5 Added item_type attribute to the containable component. diff -r 2727d6b78978 -r bf63da4b27f5 components/containable.py --- a/components/containable.py Sun Sep 18 16:26:12 2011 +0200 +++ b/components/containable.py Wed Sep 21 15:05:12 2011 +0200 @@ -18,4 +18,4 @@ def __init__(self): """Constructor""" - Component.__init__(self, bulk=int, weight=int, image=str, container=object, slot=int) + Component.__init__(self, bulk=int, weight=int, item_type=str, image=str, container=object, slot=int) diff -r 2727d6b78978 -r bf63da4b27f5 components/container.py --- a/components/container.py Sun Sep 18 16:26:12 2011 +0200 +++ b/components/container.py Wed Sep 21 15:05:12 2011 +0200 @@ -64,25 +64,39 @@ total_weight += child.weight return total_weight -def get_item(container, slot): - """Returns the item that is in the slot.""" - if len(container.children) >= (slot + 1): - return container.children[slot] +def get_item(container, slot_or_type): + """Returns the item that is in the slot, or has the given type.""" + if type(slot_or_type) == int: + if len(container.children) >= (slot_or_type + 1): + return container.children[slot_or_type] + else: + for item in container.children: + if child and if child.type == slot_or_type: + return child + return None -def remove_item(container, slot): - """Removes the item at the given slot.""" - item = get_item(container, slot) +def remove_item(container, slot_or_type): + """Removes the item at the given slot, or with the given type.""" + if type(slot_or_type) == int: + item = get_item(container, slot_or_type) + if item: + container.children[slot_or_type] = None + item.container = None + item.slot = -1 + else: + for item in container.children: + if child and if child.type == slot_or_type: + container.children[child.slot] = None + child.container = None + child.slot = -1 + +def take_item(container, slot_or_type): + """Moves the item at the given slot, or with the given type, + out of the container and returns it.""" + item = get_item(container, slot_or_type) if item: - container.children[slot] = None - item.container = None - item.slot = -1 - -def take_item(container, slot): - """Moves the item at the given slot out of the container and returns it.""" - item = get_item(container, slot) - if item: - remove_item(container, slot) + remove_item(container, slot_or_type) return item def put_item(container, item, slot=-1):