Mercurial > pyntnclick
comparison gamelib/state.py @ 488:d8087848722d engine_refactor
Factor Thing interactive stuff out into a mixing. (Items to follow.)
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sun, 29 Aug 2010 13:15:14 +0200 |
parents | efb34a6cd2a1 |
children | 463a8d60c73e |
comparison
equal
deleted
inserted
replaced
487:efb34a6cd2a1 | 488:d8087848722d |
---|---|
372 | 372 |
373 def get_detail_size(self): | 373 def get_detail_size(self): |
374 return self._background.get_size() | 374 return self._background.get_size() |
375 | 375 |
376 | 376 |
377 class Thing(StatefulGizmo): | 377 class InteractiveMixin(object): |
378 def is_interactive(self): | |
379 return True | |
380 | |
381 def interact(self, item): | |
382 if not self.is_interactive(): | |
383 return | |
384 if item is None: | |
385 return self.interact_without() | |
386 else: | |
387 handler = getattr(self, 'interact_with_' + item.tool_name, None) | |
388 if handler is not None: | |
389 return handler(item) | |
390 else: | |
391 return self.interact_default(item) | |
392 | |
393 def interact_without(self): | |
394 return self.interact_default(None) | |
395 | |
396 def interact_default(self, item=None): | |
397 return None | |
398 | |
399 | |
400 | |
401 class Thing(StatefulGizmo, InteractiveMixin): | |
378 """Base class for things in a scene that you can interact with.""" | 402 """Base class for things in a scene that you can interact with.""" |
379 | 403 |
380 # name of thing | 404 # name of thing |
381 NAME = None | 405 NAME = None |
382 | 406 |
446 return False | 470 return False |
447 | 471 |
448 def get_description(self): | 472 def get_description(self): |
449 return None | 473 return None |
450 | 474 |
451 def is_interactive(self): | |
452 return True | |
453 | |
454 def enter(self, item): | 475 def enter(self, item): |
455 """Called when the cursor enters the Thing.""" | 476 """Called when the cursor enters the Thing.""" |
456 pass | 477 pass |
457 | 478 |
458 def leave(self): | 479 def leave(self): |
459 """Called when the cursr leaves the Thing.""" | 480 """Called when the cursr leaves the Thing.""" |
460 pass | 481 pass |
461 | 482 |
462 def interact(self, item): | |
463 if not self.is_interactive(): | |
464 return | |
465 if item is None: | |
466 return self.interact_without() | |
467 else: | |
468 handler = getattr(self, 'interact_with_' + item.tool_name, None) | |
469 if handler is not None: | |
470 return handler(item) | |
471 else: | |
472 return self.interact_default(item) | |
473 | |
474 def animate(self): | 483 def animate(self): |
475 return self.current_interact.animate() | 484 return self.current_interact.animate() |
476 | |
477 def interact_without(self): | |
478 return self.interact_default(None) | |
479 | |
480 def interact_default(self, item): | |
481 return None | |
482 | 485 |
483 def draw(self, surface): | 486 def draw(self, surface): |
484 old_rect = self.current_interact.rect | 487 old_rect = self.current_interact.rect |
485 if old_rect: | 488 if old_rect: |
486 self.current_interact.rect = old_rect.move(self.scene.OFFSET) | 489 self.current_interact.rect = old_rect.move(self.scene.OFFSET) |