changeset 62:bf63da4b27f5

Added item_type attribute to the containable component.
author KarstenBock@gmx.net
date Wed, 21 Sep 2011 15:05:12 +0200
parents 2727d6b78978
children e5acfeca9bc9
files components/containable.py components/container.py
diffstat 2 files changed, 31 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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):