# HG changeset patch # User Neil Muller # Date 1282597555 -7200 # Node ID 6bfebfbce42e94c903a0ba52a33de7ebfd7be4a8 # Parent bb7c8072f8c06f13adbf60e02e724e0dd8fdee80 Partial message support diff -r bb7c8072f8c0 -r 6bfebfbce42e gamelib/gamescreen.py --- a/gamelib/gamescreen.py Mon Aug 23 22:42:30 2010 +0200 +++ b/gamelib/gamescreen.py Mon Aug 23 23:05:55 2010 +0200 @@ -5,7 +5,7 @@ from albow.controls import Button, Label, Widget from albow.layout import Column from albow.palette_view import PaletteView -from albow.dialogs import Dialog +from albow.dialogs import Dialog, wrapped_label from pygame import Rect from pygame.color import Color from pygame.locals import BLEND_ADD @@ -54,7 +54,15 @@ self.state.draw(surface) def mouse_down(self, event): - self.state.interact(event.pos) + result = self.state.interact(event.pos) + if result and result.message: + # Display the message as a modal dialog + msg_label = wrapped_label(result.message, 60) + dialog = Dialog(msg_label) + dialog.click_outside_response = -1 + dialog.present() + # queue a redraw to show updated state + self.invalidate() def mouse_move(self, event): self.state.mouse_move(event.pos) diff -r bb7c8072f8c0 -r 6bfebfbce42e gamelib/scenes/cryo.py --- a/gamelib/scenes/cryo.py Mon Aug 23 22:42:30 2010 +0200 +++ b/gamelib/scenes/cryo.py Mon Aug 23 23:05:55 2010 +0200 @@ -2,7 +2,7 @@ import random -from gamelib.state import Scene, Item, Thing +from gamelib.state import Scene, Item, Thing, Result class Cryo(Scene): @@ -45,9 +45,9 @@ } def interact_without(self): - self.message("The corpse in this cryo unit has a prosthetic leg made out of titanium. You take it.") self.state.add_inventory_item('titanium_leg') self.set_data('contains_titanium_leg', False) + return Result("The corpse in this cryo unit has a prosthetic leg made out of titanium. You take it.") def is_interactive(self): return self.get_data('contains_titanium_leg') @@ -64,14 +64,14 @@ } def interact_with_titanium_leg(self, item): - self.message("You wedge the titanium leg into the chain and twist. With a satisfying *snap*, the chain breaks and the door opens.") self.open_door() + return Result("You wedge the titanium leg into the chain and twist. With a satisfying *snap*, the chain breaks and the door opens.") def interact_without(self): - self.message("It moves slightly and then stops. A chain on the other side is preventing it from opening completely.") + return Result("It moves slightly and then stops. A chain on the other side is preventing it from opening completely.") def interact_default(self, item): - self.message(random.choice([ + return Result(random.choice([ "Sadly, this isn't that sort of game.", "Your valiant efforts are foiled by the Evil Game Designer.", "The door resists. Try something else, perhaps?", diff -r bb7c8072f8c0 -r 6bfebfbce42e gamelib/state.py --- 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: