Mercurial > pyntnclick
comparison gamelib/state.py @ 105:65976205fc2d
Rough Stab at basic animation support
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Tue, 24 Aug 2010 11:57:06 +0200 |
parents | 19d784fd3918 |
children | 5213b45fcc7e |
comparison
equal
deleted
inserted
replaced
104:ae76009a00b5 | 105:65976205fc2d |
---|---|
84 self.current_scene.draw(surface) | 84 self.current_scene.draw(surface) |
85 | 85 |
86 def interact(self, pos): | 86 def interact(self, pos): |
87 return self.current_scene.interact(self.tool, pos) | 87 return self.current_scene.interact(self.tool, pos) |
88 | 88 |
89 def animate(self): | |
90 return self.current_scene.animate() | |
91 | |
89 def mouse_move(self, pos): | 92 def mouse_move(self, pos): |
90 self.current_scene.mouse_move(self.tool, pos) | 93 self.current_scene.mouse_move(self.tool, pos) |
91 | 94 |
92 | 95 |
93 class StatefulGizmo(object): | 96 class StatefulGizmo(object): |
184 if self._current_thing: | 187 if self._current_thing: |
185 # Also update descriptions if needed | 188 # Also update descriptions if needed |
186 self._current_description = self._make_description( | 189 self._current_description = self._make_description( |
187 self._current_thing.get_description()) | 190 self._current_thing.get_description()) |
188 return result | 191 return result |
192 | |
193 def animate(self): | |
194 """Animate all the things in the scene. | |
195 | |
196 Return true if any of them need to queue a redraw""" | |
197 result = False | |
198 for thing in self.things.itervalues(): | |
199 if thing.animate(): | |
200 result = True | |
201 return result | |
189 | 202 |
190 def mouse_move(self, item, pos): | 203 def mouse_move(self, item, pos): |
191 """Call to check whether the cursor has entered / exited a thing. | 204 """Call to check whether the cursor has entered / exited a thing. |
192 | 205 |
193 Item may be an item in the list of items or None for the hand. | 206 Item may be an item in the list of items or None for the hand. |
220 | 233 |
221 def draw(self, surface): | 234 def draw(self, surface): |
222 if self.image is not None: | 235 if self.image is not None: |
223 surface.blit(self.image, self.rect, None) | 236 surface.blit(self.image, self.rect, None) |
224 | 237 |
238 def animate(self): | |
239 return False | |
240 | |
225 | 241 |
226 class InteractNoImage(Interact): | 242 class InteractNoImage(Interact): |
227 | 243 |
228 def __init__(self, x, y, w, h): | 244 def __init__(self, x, y, w, h): |
229 super(InteractNoImage, self).__init__(None, None, Rect(x, y, w, h)) | 245 super(InteractNoImage, self).__init__(None, None, Rect(x, y, w, h)) |
246 | |
230 | 247 |
231 class InteractRectUnion(Interact): | 248 class InteractRectUnion(Interact): |
232 | 249 |
233 def __init__(self, rect_list): | 250 def __init__(self, rect_list): |
234 # pygame.rect.Rect.unionall should do this, but is broken | 251 # pygame.rect.Rect.unionall should do this, but is broken |
250 | 267 |
251 def set_thing(self, thing): | 268 def set_thing(self, thing): |
252 self.image = get_image(thing.folder, self._image_name) | 269 self.image = get_image(thing.folder, self._image_name) |
253 self.rect = Rect(self._pos, self.image.get_size()) | 270 self.rect = Rect(self._pos, self.image.get_size()) |
254 self.interact_rect = self.rect | 271 self.interact_rect = self.rect |
272 | |
273 | |
274 class InteractAnimated(Interact): | |
275 """Interactive with an animation rather than an image""" | |
276 | |
277 # FIXME: Assumes all images are the same size | |
278 # anim_seq - sequence of image names | |
279 # delay - number of frames to wait between changing images | |
280 | |
281 def __init__(self, x, y, anim_seq, delay): | |
282 self._pos = (x, y) | |
283 self._anim_pos = 0 | |
284 self._names = anim_seq | |
285 self._frame_count = 0 | |
286 self._anim_seq = None | |
287 self._delay = delay | |
288 | |
289 def set_thing(self, thing): | |
290 self._anim_seq = [get_image(thing.folder, x) for x in self._names] | |
291 self.image = self._anim_seq[0] | |
292 self.rect = Rect(self._pos, self.image.get_size()) | |
293 self.interact_rect = self.rect | |
294 | |
295 def animate(self): | |
296 if self._anim_seq: | |
297 self._frame_count += 1 | |
298 if self._frame_count > self._delay: | |
299 self._frame_count = 0 | |
300 self._anim_pos += 1 | |
301 if self._anim_pos >= len(self._anim_seq): | |
302 self._anim_pos = 0 | |
303 self.image = self._anim_seq[self._anim_pos] | |
304 # queue redraw | |
305 return True | |
306 return False | |
255 | 307 |
256 | 308 |
257 class Thing(StatefulGizmo): | 309 class Thing(StatefulGizmo): |
258 """Base class for things in a scene that you can interact with.""" | 310 """Base class for things in a scene that you can interact with.""" |
259 | 311 |
340 if handler is not None: | 392 if handler is not None: |
341 return handler(item) | 393 return handler(item) |
342 else: | 394 else: |
343 return self.interact_default(item) | 395 return self.interact_default(item) |
344 | 396 |
397 def animate(self): | |
398 return self.current_interact.animate() | |
399 | |
345 def interact_without(self): | 400 def interact_without(self): |
346 return self.interact_default(None) | 401 return self.interact_default(None) |
347 | 402 |
348 def interact_default(self, item): | 403 def interact_default(self, item): |
349 return Result("It doesn't work.") | 404 return Result("It doesn't work.") |