Mercurial > boomslang
comparison gamelib/state.py @ 120:48d24a48d0ce
Enter and leave hooks
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Tue, 24 Aug 2010 17:24:54 +0200 |
parents | d5f7cccfdb6c |
children | 97322b78d1c1 |
comparison
equal
deleted
inserted
replaced
119:d5f7cccfdb6c | 120:48d24a48d0ce |
---|---|
28 #state.load_scenes("mess") | 28 #state.load_scenes("mess") |
29 #state.load_scenes("engine") | 29 #state.load_scenes("engine") |
30 #state.load_scenes("machine") | 30 #state.load_scenes("machine") |
31 #state.load_scenes("map") | 31 #state.load_scenes("map") |
32 state.set_current_scene("cryo") | 32 state.set_current_scene("cryo") |
33 state.set_do_enter_leave() | |
33 return state | 34 return state |
34 | 35 |
35 | 36 |
36 class State(object): | 37 class State(object): |
37 """Complete game state. | 38 """Complete game state. |
55 self.tool = None | 56 self.tool = None |
56 # current scene | 57 # current scene |
57 self.current_scene = None | 58 self.current_scene = None |
58 # current detail view | 59 # current detail view |
59 self.current_detail = None | 60 self.current_detail = None |
61 # scene we came from, for enter and leave processing | |
62 self.previous_scene = None | |
63 # scene transion helpers | |
64 self.do_check = None | |
65 self.old_pos = None | |
60 | 66 |
61 def add_scene(self, scene): | 67 def add_scene(self, scene): |
62 self.scenes[scene.name] = scene | 68 self.scenes[scene.name] = scene |
63 | 69 |
64 def add_detail_view(self, detail_view): | 70 def add_detail_view(self, detail_view): |
74 if hasattr(mod, 'DETAIL_VIEWS'): | 80 if hasattr(mod, 'DETAIL_VIEWS'): |
75 for scene_cls in mod.DETAIL_VIEWS: | 81 for scene_cls in mod.DETAIL_VIEWS: |
76 self.add_detail_view(scene_cls(self)) | 82 self.add_detail_view(scene_cls(self)) |
77 | 83 |
78 def set_current_scene(self, name): | 84 def set_current_scene(self, name): |
85 old_scene = self.current_scene | |
79 self.current_scene = self.scenes[name] | 86 self.current_scene = self.scenes[name] |
87 if old_scene and old_scene != self.current_scene: | |
88 self.previous_scene = old_scene | |
89 self.set_do_enter_leave() | |
80 | 90 |
81 def set_current_detail(self, name): | 91 def set_current_detail(self, name): |
82 if name is None: | 92 if name is None: |
83 self.current_detail = None | 93 self.current_detail = None |
84 else: | 94 else: |
96 | 106 |
97 def set_tool(self, item): | 107 def set_tool(self, item): |
98 self.tool = item | 108 self.tool = item |
99 | 109 |
100 def draw(self, surface): | 110 def draw(self, surface): |
101 self.current_scene.draw(surface) | 111 if self.do_check and self.previous_scene and self.do_check == constants.LEAVE: |
112 # We still need to handle leave events, so still display the scene | |
113 self.previous_scene.draw(surface) | |
114 else: | |
115 self.current_scene.draw(surface) | |
102 | 116 |
103 def draw_detail(self, surface): | 117 def draw_detail(self, surface): |
104 self.current_detail.draw(surface) | 118 self.current_detail.draw(surface) |
105 | 119 |
106 def interact(self, pos): | 120 def interact(self, pos): |
108 | 122 |
109 def interact_detail(self, pos): | 123 def interact_detail(self, pos): |
110 return self.current_detail.interact(self.tool, pos) | 124 return self.current_detail.interact(self.tool, pos) |
111 | 125 |
112 def animate(self): | 126 def animate(self): |
113 return self.current_scene.animate() | 127 if not self.do_check: |
128 return self.current_scene.animate() | |
129 | |
130 def check_enter_leave(self): | |
131 if not self.do_check: | |
132 return None | |
133 if self.do_check == constants.LEAVE: | |
134 self.do_check = constants.ENTER | |
135 if self.previous_scene: | |
136 return self.previous_scene.leave() | |
137 return None | |
138 elif self.do_check == constants.ENTER: | |
139 self.do_check = None | |
140 # Fix descriptions, etc. | |
141 if self.old_pos: | |
142 self.current_scene.mouse_move(self.tool, self.old_pos) | |
143 return self.current_scene.enter() | |
144 raise RuntimeError('invalid do_check value %s' % self.do_check) | |
114 | 145 |
115 def mouse_move(self, pos): | 146 def mouse_move(self, pos): |
116 self.current_scene.mouse_move(self.tool, pos) | 147 self.current_scene.mouse_move(self.tool, pos) |
148 # So we can do sensible things on enter and leave | |
149 self.old_pos = pos | |
150 | |
151 def set_do_enter_leave(self): | |
152 """Flag that we need to run the enter loop""" | |
153 self.do_check = constants.LEAVE | |
117 | 154 |
118 def mouse_move_detail(self, pos): | 155 def mouse_move_detail(self, pos): |
119 self.current_detail.mouse_move(self.tool, pos) | 156 self.current_detail.mouse_move(self.tool, pos) |
120 | 157 |
121 | 158 |
223 result = False | 260 result = False |
224 for thing in self.things.itervalues(): | 261 for thing in self.things.itervalues(): |
225 if thing.animate(): | 262 if thing.animate(): |
226 result = True | 263 result = True |
227 return result | 264 return result |
265 | |
266 def enter(self): | |
267 return None | |
268 | |
269 def leave(self): | |
270 self._current_description = None | |
271 return None | |
228 | 272 |
229 def mouse_move(self, item, pos): | 273 def mouse_move(self, item, pos): |
230 """Call to check whether the cursor has entered / exited a thing. | 274 """Call to check whether the cursor has entered / exited a thing. |
231 | 275 |
232 Item may be an item in the list of items or None for the hand. | 276 Item may be an item in the list of items or None for the hand. |