comparison 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
comparison
equal deleted inserted replaced
77:bb7c8072f8c0 78:6bfebfbce42e
6 from pygame.locals import BLEND_ADD 6 from pygame.locals import BLEND_ADD
7 from pygame.rect import Rect 7 from pygame.rect import Rect
8 from pygame.color import Color 8 from pygame.color import Color
9 9
10 import constants 10 import constants
11
12 class Result(object):
13 """Result of interacting with a thing"""
14
15 def __init__(self, message=None):
16 self.message = message
11 17
12 18
13 def initial_state(): 19 def initial_state():
14 """Load the initial state.""" 20 """Load the initial state."""
15 state = State() 21 state = State()
69 75
70 def draw(self, surface): 76 def draw(self, surface):
71 self.current_scene.draw(surface) 77 self.current_scene.draw(surface)
72 78
73 def interact(self, pos): 79 def interact(self, pos):
74 self.current_scene.interact(self.tool, pos) 80 return self.current_scene.interact(self.tool, pos)
75 81
76 def mouse_move(self, pos): 82 def mouse_move(self, pos):
77 self.current_scene.mouse_move(self.tool, pos) 83 self.current_scene.mouse_move(self.tool, pos)
78 84
79 85
164 170
165 def interact(self, item, pos): 171 def interact(self, item, pos):
166 """Interact with a particular position. 172 """Interact with a particular position.
167 173
168 Item may be an item in the list of items or None for the hand. 174 Item may be an item in the list of items or None for the hand.
175
176 Returns a Result object to provide feedback to the player.
169 """ 177 """
170 for thing in self.things.itervalues(): 178 for thing in self.things.itervalues():
171 if thing.rect.collidepoint(pos): 179 if thing.rect.collidepoint(pos):
172 thing.interact(item) 180 result = thing.interact(item)
173 break 181 if result:
182 if self._current_thing:
183 # Also update descriptions if needed
184 self._current_description = self._make_description(
185 self._current_thing.get_description())
186 return result
174 187
175 def mouse_move(self, item, pos): 188 def mouse_move(self, item, pos):
176 """Call to check whether the cursor has entered / exited a thing. 189 """Call to check whether the cursor has entered / exited a thing.
177 190
178 Item may be an item in the list of items or None for the hand. 191 Item may be an item in the list of items or None for the hand.
225 def set_scene(self, scene): 238 def set_scene(self, scene):
226 assert self.scene is None 239 assert self.scene is None
227 self.scene = scene 240 self.scene = scene
228 self.state = scene.state 241 self.state = scene.state
229 242
230 def message(self, msg):
231 self.state.message(msg)
232
233 def get_description(self): 243 def get_description(self):
234 return None 244 return None
235 245
236 def is_interactive(self): 246 def is_interactive(self):
237 return True 247 return True
246 256
247 def interact(self, item): 257 def interact(self, item):
248 if not self.is_interactive(): 258 if not self.is_interactive():
249 return 259 return
250 if item is None: 260 if item is None:
251 self.interact_without() 261 return self.interact_without()
252 else: 262 else:
253 handler = getattr(self, 'interact_with_' + item.name, None) 263 handler = getattr(self, 'interact_with_' + item.name, None)
254 if handler is not None: 264 if handler is not None:
255 handler(item) 265 return handler(item)
256 else: 266 else:
257 self.interact_default(item) 267 return self.interact_default(item)
258 268
259 def interact_without(self): 269 def interact_without(self):
260 self.interact_default(None) 270 return self.interact_default(None)
261 271
262 def interact_default(self, item): 272 def interact_default(self, item):
263 self.message("It doesn't work.") 273 return Result("It doesn't work.")
264 274
265 def draw(self, surface): 275 def draw(self, surface):
266 if self._interact_hilight_color is not None: 276 if self._interact_hilight_color is not None:
267 frame_rect(surface, self._interact_hilight_color, 277 frame_rect(surface, self._interact_hilight_color,
268 self.interact_rect.inflate(1, 1), 1) 278 self.interact_rect.inflate(1, 1), 1)