changeset 64:b73050f98411

Added equip function.
author KarstenBock@gmx.net
date Wed, 21 Sep 2011 15:43:12 +0200
parents e5acfeca9bc9
children e856b604b650
files components/equip.py
diffstat 1 files changed, 23 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/components/equip.py	Wed Sep 21 15:25:32 2011 +0200
+++ b/components/equip.py	Wed Sep 21 15:43:12 2011 +0200
@@ -19,4 +19,26 @@
     """
     
     def __init__(self):
-        Component.__init__(self, head=object, body=object, leg=object, feet=object, l_arm=object, r_arm=object)
\ No newline at end of file
+        Component.__init__(self, head=object, body=object, leg=object, feet=object, l_arm=object, r_arm=object)
+
+class SlotInvalidError(Exception):
+    """Error that gets raised when the slot is invalid."""
+    
+    def __init__(self, slot):
+        self.slot = slot
+    
+    def __str__(self):
+        return "\"%s\" is not a valid slot." % self.slot
+    
+def equip(wearer, equipable, slot):
+    """Equip the wearer with the given equipable.
+    @returns The equipable that was at the given slot, or None"""
+    if slot in equipable.possible_slots:
+        try:
+            old_item = getattr(wearer, slot)
+            setattr(wearer, slot, equipable)
+            return old_item
+        except AttributeError:
+            raise SlotInvalidError(slot)
+    return None
+    
\ No newline at end of file