diff gamelib/state.py @ 78:6bfebfbce42e

Partial message support
author Neil Muller <neil@dip.sun.ac.za>
date Mon, 23 Aug 2010 23:05:55 +0200
parents bb7c8072f8c0
children d7c0a702a0b4
line wrap: on
line diff
--- a/gamelib/state.py	Mon Aug 23 22:42:30 2010 +0200
+++ b/gamelib/state.py	Mon Aug 23 23:05:55 2010 +0200
@@ -9,6 +9,12 @@
 
 import constants
 
+class Result(object):
+    """Result of interacting with a thing"""
+
+    def __init__(self, message=None):
+        self.message = message
+
 
 def initial_state():
     """Load the initial state."""
@@ -71,7 +77,7 @@
         self.current_scene.draw(surface)
 
     def interact(self, pos):
-        self.current_scene.interact(self.tool, pos)
+        return self.current_scene.interact(self.tool, pos)
 
     def mouse_move(self, pos):
         self.current_scene.mouse_move(self.tool, pos)
@@ -166,11 +172,18 @@
         """Interact with a particular position.
 
         Item may be an item in the list of items or None for the hand.
+
+        Returns a Result object to provide feedback to the player.
         """
         for thing in self.things.itervalues():
             if thing.rect.collidepoint(pos):
-                thing.interact(item)
-                break
+                result = thing.interact(item)
+                if result:
+                    if self._current_thing:
+                        # Also update descriptions if needed
+                        self._current_description = self._make_description(
+                                self._current_thing.get_description())
+                    return result
 
     def mouse_move(self, item, pos):
         """Call to check whether the cursor has entered / exited a thing.
@@ -227,9 +240,6 @@
         self.scene = scene
         self.state = scene.state
 
-    def message(self, msg):
-        self.state.message(msg)
-
     def get_description(self):
         return None
 
@@ -248,19 +258,19 @@
         if not self.is_interactive():
             return
         if item is None:
-            self.interact_without()
+            return self.interact_without()
         else:
             handler = getattr(self, 'interact_with_' + item.name, None)
             if handler is not None:
-                handler(item)
+                return handler(item)
             else:
-                self.interact_default(item)
+                return self.interact_default(item)
 
     def interact_without(self):
-        self.interact_default(None)
+        return self.interact_default(None)
 
     def interact_default(self, item):
-        self.message("It doesn't work.")
+        return Result("It doesn't work.")
 
     def draw(self, surface):
         if self._interact_hilight_color is not None: