changeset 511:93ddcac0b772

Merged in engine refactorings.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 04 Sep 2010 09:53:00 +0200
parents d274cc414178 (current diff) c1f4f9149349 (diff)
children b10dae40dc32
files gamelib/state.py
diffstat 5 files changed, 71 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/scenes/bridge.py	Fri Sep 03 17:04:38 2010 +0200
+++ b/gamelib/scenes/bridge.py	Sat Sep 04 09:53:00 2010 +0200
@@ -217,10 +217,10 @@
     INVENTORY_IMAGE = 'superconductor_fixed.png'
     CURSOR = CursorSprite('superconductor_fixed.png')
 
-    def interact_with_duct_tape(self, item, state):
+    def interact_with_duct_tape(self, item):
         taped_superconductor = TapedSuperconductor('taped_superconductor')
-        state.add_item(taped_superconductor)
-        state.replace_inventory_item(self.name, taped_superconductor.name)
+        self.state.add_item(taped_superconductor)
+        self.state.replace_inventory_item(self.name, taped_superconductor.name)
         return Result("You rip off a piece of duct tape and stick it on the superconductor. "
                       "It almost sticks to itself, but you successfully avoid disaster.")
 
--- a/gamelib/scenes/crew_quarters.py	Fri Sep 03 17:04:38 2010 +0200
+++ b/gamelib/scenes/crew_quarters.py	Sat Sep 04 09:53:00 2010 +0200
@@ -133,10 +133,10 @@
     CURSOR = CursorSprite('fishbowl.png')
     NAME = "fishbowl"
 
-    def interact_with_duct_tape(self, item, state):
+    def interact_with_duct_tape(self, item):
         helmet = FishbowlHelmet('helmet')
-        state.add_item(helmet)
-        state.replace_inventory_item(self.name, helmet.name)
+        self.state.add_item(helmet)
+        self.state.replace_inventory_item(self.name, helmet.name)
         return Result("You duct tape the edges of the helmet. The seal is"
                 " crude, but it will serve as a workable helmet if needed.")
 
--- a/gamelib/scenes/mess.py	Fri Sep 03 17:04:38 2010 +0200
+++ b/gamelib/scenes/mess.py	Sat Sep 04 09:53:00 2010 +0200
@@ -50,22 +50,22 @@
 class BaseCan(CloneableItem):
     """Base class for the cans"""
 
-    def interact_with_full_can(self, item, state):
+    def interact_with_full_can(self, item):
         return Result("You bang the cans together. It sounds like two cans being banged together.", soundfile="can_hit.ogg")
 
-    def interact_with_dented_can(self, item, state):
-        return self.interact_with_full_can(item, state)
+    def interact_with_dented_can(self, item):
+        return self.interact_with_full_can(item)
 
-    def interact_with_empty_can(self, item, state):
-        return self.interact_with_full_can(item, state)
+    def interact_with_empty_can(self, item):
+        return self.interact_with_full_can(item)
 
-    def interact_with_machete(self, item, state):
+    def interact_with_machete(self, item):
         return Result("You'd mangle it beyond usefulness.")
 
-    def interact_with_canopener(self, item, state):
+    def interact_with_canopener(self, item):
         empty = EmptyCan('empty_can')
-        state.add_item(empty)
-        state.replace_inventory_item(self.name, empty.name)
+        self.state.add_item(empty)
+        self.state.replace_inventory_item(self.name, empty.name)
         return Result("You open both ends of the can, discarding the hideous contents.")
 
 
@@ -75,10 +75,10 @@
     INVENTORY_IMAGE = "empty_can.png"
     CURSOR = CursorSprite('empty_can_cursor.png')
 
-    def interact_with_titanium_leg(self, item, state):
+    def interact_with_titanium_leg(self, item):
         return Result("Flattening the can doesn't look like a useful thing to do.")
 
-    def interact_with_canopener(self, item, state):
+    def interact_with_canopener(self, item):
         return Result("There's nothing left to open on this can")
 
 
@@ -88,10 +88,10 @@
     INVENTORY_IMAGE = "full_can.png"
     CURSOR = CursorSprite('full_can_cursor.png')
 
-    def interact_with_titanium_leg(self, item, state):
+    def interact_with_titanium_leg(self, item):
         dented = DentedCan("dented_can")
-        state.add_item(dented)
-        state.replace_inventory_item(self.name, dented.name)
+        self.state.add_item(dented)
+        self.state.replace_inventory_item(self.name, dented.name)
         return Result("You club the can with the femur. The can gets dented, but doesn't open.", soundfile="can_hit.ogg")
 
 
@@ -101,7 +101,7 @@
     INVENTORY_IMAGE = "dented_can.png"
     CURSOR = CursorSprite('dented_can_cursor.png')
 
-    def interact_with_titanium_leg(self, item, state):
+    def interact_with_titanium_leg(self, item):
         return Result("You club the can with the femur. The dents shift around, but it still doesn't open.", soundfile="can_hit.ogg")
 
 
--- a/gamelib/state.py	Fri Sep 03 17:04:38 2010 +0200
+++ b/gamelib/state.py	Sat Sep 04 09:53:00 2010 +0200
@@ -114,6 +114,7 @@
 
     def add_item(self, item):
         self.items[item.name] = item
+        item.set_state(self)
 
     def load_scenes(self, modname):
         mod = __import__("gamelib.scenes.%s" % (modname,), fromlist=[modname])
@@ -378,7 +379,36 @@
         return self._background.get_size()
 
 
-class Thing(StatefulGizmo):
+class InteractiveMixin(object):
+    def is_interactive(self):
+        return True
+
+    def interact(self, tool):
+        if not self.is_interactive():
+            return None
+        if tool is None:
+            return self.interact_without()
+        handler = getattr(self, 'interact_with_' + tool.tool_name, None)
+        inverse_handler = self.get_inverse_interact(tool)
+        if handler is not None:
+            return handler(tool)
+        elif inverse_handler is not None:
+            return inverse_handler(self)
+        else:
+            return self.interact_default(tool)
+
+    def get_inverse_interact(self, tool):
+        return None
+
+    def interact_without(self):
+        return self.interact_default(None)
+
+    def interact_default(self, item=None):
+        return None
+
+
+
+class Thing(StatefulGizmo, InteractiveMixin):
     """Base class for things in a scene that you can interact with."""
 
     # name of thing
@@ -452,9 +482,6 @@
     def get_description(self):
         return None
 
-    def is_interactive(self):
-        return True
-
     def enter(self, item):
         """Called when the cursor enters the Thing."""
         pass
@@ -463,27 +490,9 @@
         """Called when the cursr leaves the Thing."""
         pass
 
-    def interact(self, item):
-        if not self.is_interactive():
-            return
-        if item is None:
-            return self.interact_without()
-        else:
-            handler = getattr(self, 'interact_with_' + item.tool_name, None)
-            if handler is not None:
-                return handler(item)
-            else:
-                return self.interact_default(item)
-
     def animate(self):
         return self.current_interact.animate()
 
-    def interact_without(self):
-        return self.interact_default(None)
-
-    def interact_default(self, item):
-        return None
-
     def draw(self, surface):
         old_rect = self.current_interact.rect
         if old_rect:
@@ -500,42 +509,40 @@
                             rect.inflate(1, 1), 1)
 
 
-class Item(object):
+class Item(InteractiveMixin):
     """Base class for inventory items."""
 
     # image for inventory
     INVENTORY_IMAGE = None
 
+    # name of item
+    NAME = None
+
     # name for interactions (i.e. def interact_with_<TOOL_NAME>)
     TOOL_NAME = None
 
     # set to instance of CursorSprite
     CURSOR = None
 
-    def __init__(self, name):
-        self.name = name
+    def __init__(self, name=None):
+        self.state = None
+        self.name = self.NAME
+        if name is not None:
+            self.name = name
         self.tool_name = name
         if self.TOOL_NAME is not None:
             self.tool_name = self.TOOL_NAME
         self.inventory_image = get_image('items', self.INVENTORY_IMAGE)
 
+    def set_state(self, state):
+        assert self.state is None
+        self.state = state
+
     def get_inventory_image(self):
         return self.inventory_image
 
-    def interact(self, tool, state):
-        if tool is None:
-            return self.interact_without(state)
-        handler = getattr(self, 'interact_with_' + tool.name, None)
-        inverse_handler = getattr(tool, 'interact_with_' + self.tool_name, None)
-        if handler is not None:
-            return handler(tool, state)
-        elif inverse_handler is not None:
-            return inverse_handler(self, state)
-        else:
-            return self.interact_default(tool, state)
-
-    def interact_default(self, tool, state):
-        return Result("That doesn't do anything useful")
+    def get_inverse_interact(self, tool):
+        return getattr(tool, 'interact_with_' + self.tool_name, None)
 
 
 class CloneableItem(Item):
@@ -546,9 +553,7 @@
         cls._counter += 1
         return cls._counter - 1
 
-    def __init__(self, name):
+    def __init__(self, name=None):
+        super(CloneableItem, self).__init__(name)
         my_count = self._get_new_id()
-        super(CloneableItem, self).__init__("%s.%s" % (name, my_count))
-        self.tool_name = name
-        if self.TOOL_NAME is not None:
-            self.tool_name = self.TOOL_NAME
+        self.name = "%s.%s" % (self.name, my_count)
--- a/gamelib/tests/game_logic_utils.py	Fri Sep 03 17:04:38 2010 +0200
+++ b/gamelib/tests/game_logic_utils.py	Sat Sep 04 09:53:00 2010 +0200
@@ -81,7 +81,7 @@
         self.assert_inventory_item(target_item)
         item_obj = self.state.items[item]
         target_obj = self.state.items[target_item]
-        result = target_obj.interact(item_obj, self.state)
+        result = target_obj.interact(item_obj)
         return self.handle_result(result)
 
     def close_detail(self):