changeset 63:3087be3463e0

Some framework support for better message handling
author Neil Muller <neil@dip.sun.ac.za>
date Mon, 23 Aug 2010 18:32:15 +0200
parents e32e2100ee52
children c21e4521512e
files gamelib/gamescreen.py gamelib/scenes/cryo.py gamelib/state.py
diffstat 3 files changed, 49 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/gamescreen.py	Mon Aug 23 18:13:57 2010 +0200
+++ b/gamelib/gamescreen.py	Mon Aug 23 18:32:15 2010 +0200
@@ -51,10 +51,25 @@
     def __init__(self, state):
         Widget.__init__(self, Rect(0, 0, 800, 600 - BUTTON_SIZE))
         self.state = state
+        # current mouse-over thing description
+        self.description = None
 
     def draw(self, surface):
         self.state.draw(surface)
+        if self.description:
+            print self.description
+        msg = self.state.get_message()
+        if msg:
+            # FIXME: add some timer to invalidate msgs
+            print msg
+            self.state.clear_message()
 
+    def mouse_move(self, event):
+        old_desc = self.description
+        self.description = self.state.get_description(event.pos)
+        if self.description != old_desc:
+            # queue a redraw
+            self.invalidate()
 
 class GameScreen(Screen):
     def __init__(self, shell):
@@ -94,6 +109,7 @@
         self.state.add_inventory_item('triangle')
         self.state.add_inventory_item('titanium_leg')
 
+
     # Albow uses magic method names (command + '_cmd'). Yay.
     # Albow's search order means they need to be defined here, not in
     # PopMenu, which is annoying.
@@ -111,3 +127,4 @@
         self.handbutton.toggle_selected()
         self.inventory.unselect()
 
+
--- a/gamelib/scenes/cryo.py	Mon Aug 23 18:13:57 2010 +0200
+++ b/gamelib/scenes/cryo.py	Mon Aug 23 18:32:15 2010 +0200
@@ -18,8 +18,8 @@
         super(Cryo, self).__init__(state)
         self.add_item(Triangle("triangle"))
         self.add_item(TitaniumLeg("titanium_leg"))
-        self.add_thing(CryoUnitAlpha("cryo.unit.1", (20, 20, 400, 500)))
-        self.add_thing(CryoRoomDoor("cryo.door", (30, 30, 400, 300)))
+        self.add_thing(CryoUnitAlpha("cryo.unit.1", (20, 20, 200, 200)))
+        self.add_thing(CryoRoomDoor("cryo.door", (200, 200, 400, 300)))
 
 
 class Triangle(Item):
@@ -69,5 +69,10 @@
         self.set_data('open', True)
         self.state.scenes['bridge'].set_data('accessible', True)
 
+    def get_description(self):
+        if self.get_data('open'):
+            return 'An open doorway leads to the rest of the ship'
+        return 'A rusty door. It is currently closed'
+
 
 SCENES = [Cryo]
--- a/gamelib/state.py	Mon Aug 23 18:13:57 2010 +0200
+++ b/gamelib/state.py	Mon Aug 23 18:32:15 2010 +0200
@@ -2,6 +2,7 @@
 
 from albow.resource import get_image, get_sound
 from pygame.locals import BLEND_ADD
+from pygame.rect import Rect
 
 
 def initial_state():
@@ -33,6 +34,8 @@
         self.items = {}
         # list of item objects in inventory
         self.inventory = []
+        # Result of the most recent action
+        self.msg = None
         # current scene
         self.current_scene = None
 
@@ -59,8 +62,18 @@
     def draw(self, surface):
         self.current_scene.draw(surface)
 
+    def get_message(self):
+        return self.msg
+
+    def clear_message(self):
+        self.msg = None
+
+    def get_description(self, pos):
+        """Get the description associated with current mouse position"""
+        return self.current_scene.get_description(pos)
+
     def message(self, msg):
-        print msg
+        self.msg = msg
 
 
 class StatefulGizmo(object):
@@ -123,6 +136,14 @@
         self.draw_background(surface)
         self.draw_things(surface)
 
+    def get_description(self, pos):
+        desc = None
+        for thing in self.things.itervalues():
+            # Last thing in the list that matches wins
+            if Rect(thing.rect).collidepoint(pos):
+                desc = thing.get_description()
+        return desc
+
 
 class Thing(StatefulGizmo):
     """Base class for things in a scene that you can interact with."""
@@ -152,6 +173,9 @@
     def message(self, msg):
         self.state.message(msg)
 
+    def get_description(self):
+        return None
+
     def is_interactive(self):
         return True