# HG changeset patch # User KarstenBock@gmx.net # Date 1316610312 -7200 # Node ID b5619032c521c9482eb588369c76c0d8a2ef2ff1 # Parent d89e88a90c9e8c8fa3bcec0cd68dbc77598b312b Added item_type attribute to the containable component. diff -r d89e88a90c9e -r b5619032c521 src/parpg/components/containable.py --- a/src/parpg/components/containable.py Sun Sep 18 16:26:12 2011 +0200 +++ b/src/parpg/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 d89e88a90c9e -r b5619032c521 src/parpg/components/container.py --- a/src/parpg/components/container.py Sun Sep 18 16:26:12 2011 +0200 +++ b/src/parpg/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):