comparison gamelib/state.py @ 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 75bf3d3689e9
children cab924519037
comparison
equal deleted inserted replaced
62:e32e2100ee52 63:3087be3463e0
1 """Utilities and base classes for dealing with scenes.""" 1 """Utilities and base classes for dealing with scenes."""
2 2
3 from albow.resource import get_image, get_sound 3 from albow.resource import get_image, get_sound
4 from pygame.locals import BLEND_ADD 4 from pygame.locals import BLEND_ADD
5 from pygame.rect import Rect
5 6
6 7
7 def initial_state(): 8 def initial_state():
8 """Load the initial state.""" 9 """Load the initial state."""
9 state = State() 10 state = State()
31 self.scenes = {} 32 self.scenes = {}
32 # map of item name -> Item object 33 # map of item name -> Item object
33 self.items = {} 34 self.items = {}
34 # list of item objects in inventory 35 # list of item objects in inventory
35 self.inventory = [] 36 self.inventory = []
37 # Result of the most recent action
38 self.msg = None
36 # current scene 39 # current scene
37 self.current_scene = None 40 self.current_scene = None
38 41
39 def add_scene(self, scene): 42 def add_scene(self, scene):
40 self.scenes[scene.name] = scene 43 self.scenes[scene.name] = scene
57 self.inventory.remove(self.items[name]) 60 self.inventory.remove(self.items[name])
58 61
59 def draw(self, surface): 62 def draw(self, surface):
60 self.current_scene.draw(surface) 63 self.current_scene.draw(surface)
61 64
65 def get_message(self):
66 return self.msg
67
68 def clear_message(self):
69 self.msg = None
70
71 def get_description(self, pos):
72 """Get the description associated with current mouse position"""
73 return self.current_scene.get_description(pos)
74
62 def message(self, msg): 75 def message(self, msg):
63 print msg 76 self.msg = msg
64 77
65 78
66 class StatefulGizmo(object): 79 class StatefulGizmo(object):
67 80
68 # initial data (optional, defaults to none) 81 # initial data (optional, defaults to none)
120 thing.draw(surface) 133 thing.draw(surface)
121 134
122 def draw(self, surface): 135 def draw(self, surface):
123 self.draw_background(surface) 136 self.draw_background(surface)
124 self.draw_things(surface) 137 self.draw_things(surface)
138
139 def get_description(self, pos):
140 desc = None
141 for thing in self.things.itervalues():
142 # Last thing in the list that matches wins
143 if Rect(thing.rect).collidepoint(pos):
144 desc = thing.get_description()
145 return desc
125 146
126 147
127 class Thing(StatefulGizmo): 148 class Thing(StatefulGizmo):
128 """Base class for things in a scene that you can interact with.""" 149 """Base class for things in a scene that you can interact with."""
129 150
149 self.scene = scene 170 self.scene = scene
150 self.state = scene.state 171 self.state = scene.state
151 172
152 def message(self, msg): 173 def message(self, msg):
153 self.state.message(msg) 174 self.state.message(msg)
175
176 def get_description(self):
177 return None
154 178
155 def is_interactive(self): 179 def is_interactive(self):
156 return True 180 return True
157 181
158 def interact(self, item): 182 def interact(self, item):