comparison pyntnclick/state.py @ 553:ebb2efcb4ea7 pyntnclick

Create a re-usable main function.
author Simon Cross <hodgestar+bzr@gmail.com>
date Sat, 11 Feb 2012 13:40:51 +0200
parents 38fb04728ac5
children 99a1420097df
comparison
equal deleted inserted replaced
552:15713dfe555d 553:ebb2efcb4ea7
8 from pygame.rect import Rect 8 from pygame.rect import Rect
9 from pygame.color import Color 9 from pygame.color import Color
10 10
11 from pyntnclick import constants 11 from pyntnclick import constants
12 from pyntnclick.sound import get_sound 12 from pyntnclick.sound import get_sound
13
14 from gamelib.scenes import SCENE_LIST, INITIAL_SCENE
15
16 # override the initial scene to for debugging
17 DEBUG_SCENE = None
18
19 # whether to show debugging rects
20 DEBUG_RECTS = False
21 13
22 14
23 class Result(object): 15 class Result(object):
24 """Result of interacting with a thing""" 16 """Result of interacting with a thing"""
25 17
60 if res: 52 if res:
61 # List may contain None's 53 # List may contain None's
62 res.process(scene_widget) 54 res.process(scene_widget)
63 55
64 56
65 def initial_state(): 57 def initial_state_creator(initial_scene, scene_list, debug_rects=False):
66 """Load the initial state.""" 58 def initial_state():
67 state = GameState() 59 """Load the initial state."""
68 for scene in SCENE_LIST: 60 state = GameState()
69 state.load_scenes(scene) 61 state.set_debug_rects(debug_rects)
70 initial_scene = INITIAL_SCENE if DEBUG_SCENE is None else DEBUG_SCENE 62 for scene in scene_list:
71 state.set_current_scene(initial_scene) 63 state.load_scenes(scene)
72 state.set_do_enter_leave() 64 state.set_current_scene(initial_scene)
73 return state 65 state.set_do_enter_leave()
66 return state
67 return initial_state
74 68
75 69
76 class GameState(object): 70 class GameState(object):
77 """Complete game state. 71 """Complete game state.
78 72
79 Game state consists of: 73 Game state consists of:
80 74
81 * items 75 * items
82 * scenes 76 * scenes
83 """ 77 """
84
85 def __init__(self): 78 def __init__(self):
86 # map of scene name -> Scene object 79 # map of scene name -> Scene object
87 self.scenes = {} 80 self.scenes = {}
88 # map of detail view name -> DetailView object 81 # map of detail view name -> DetailView object
89 self.detail_views = {} 82 self.detail_views = {}
103 self.do_check = None 96 self.do_check = None
104 self.old_pos = None 97 self.old_pos = None
105 # current thing 98 # current thing
106 self.current_thing = None 99 self.current_thing = None
107 self.highlight_override = False 100 self.highlight_override = False
101 # debug rects
102 self.debug_rects = False
103
104 def set_debug_rects(self, value=True):
105 self.debug_rects = value
108 106
109 def add_scene(self, scene): 107 def add_scene(self, scene):
110 self.scenes[scene.name] = scene 108 self.scenes[scene.name] = scene
111 109
112 def add_detail_view(self, detail_view): 110 def add_detail_view(self, detail_view):
477 old_rect = self.current_interact.rect 475 old_rect = self.current_interact.rect
478 if old_rect: 476 if old_rect:
479 self.current_interact.rect = old_rect.move(self.scene.OFFSET) 477 self.current_interact.rect = old_rect.move(self.scene.OFFSET)
480 self.current_interact.draw(surface) 478 self.current_interact.draw(surface)
481 self.current_interact.rect = old_rect 479 self.current_interact.rect = old_rect
482 if DEBUG_RECTS and self._interact_hilight_color: 480 if self.state.debug_rects and self._interact_hilight_color:
483 if hasattr(self.rect, 'collidepoint'): 481 if hasattr(self.rect, 'collidepoint'):
484 frame_rect(surface, self._interact_hilight_color, 482 frame_rect(surface, self._interact_hilight_color,
485 self.rect.inflate(1, 1), 1) 483 self.rect.inflate(1, 1), 1)
486 else: 484 else:
487 for rect in self.rect: 485 for rect in self.rect: