source: nagslang/game_object.py@ 192:3dc2b6290e66

Last change on this file since 192:3dc2b6290e66 was 192:3dc2b6290e66, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Document collision handler a little better.

File size: 13.5 KB
RevLine 
[63]1import math
[62]2
[81]3import pygame
4import pymunk
[93]5import pymunk.pygame_util
[81]6
[107]7from nagslang.constants import (
[162]8 SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW,
[176]9 ZORDER_FLOOR, COLLISION_TYPE_DOOR, COLLISION_TYPE_PLAYER)
[104]10from nagslang.options import options
[155]11from nagslang.resources import resources
[180]12from nagslang.events import DoorEvent
[191]13from nagslang.widgets.text import TextWidget
[81]14
[82]15
[106]16class PuzzleGlue(object):
17 """Glue that holds bits of a puzzle together.
18 """
19 def __init__(self):
20 self._components = {}
21
22 def add_component(self, name, puzzler):
[145]23 if not isinstance(puzzler, Puzzler):
24 puzzler = puzzler.puzzler
[106]25 self._components[name] = puzzler
26 puzzler.set_glue(self)
27
28 def get_state_of(self, name):
29 return self._components[name].get_state()
30
31
[81]32class Puzzler(object):
[106]33 """Behaviour specific to a puzzle component.
34 """
35 def set_glue(self, glue):
36 self.glue = glue
37
[123]38 def set_game_object(self, game_object):
39 self.game_object = game_object
40
[106]41 def get_state(self):
[81]42 raise NotImplementedError()
43
44
[186]45class YesPuzzler(Puzzler):
46 """Yes sir, I'm always on.
47 """
48 def get_state(self):
49 return True
50
51
52class NoPuzzler(Puzzler):
53 """No sir, I'm always off.
54 """
55 def get_state(self):
56 return False
57
58
[185]59class CollidePuzzler(Puzzler):
60 def __init__(self, *collision_types):
61 if not collision_types:
62 collision_types = (COLLISION_TYPE_PLAYER,)
63 self._collision_types = collision_types
64
[93]65 def get_state(self):
[123]66 space = self.game_object.get_space()
67 for shape in space.shape_query(self.game_object.get_shape()):
[185]68 if shape.collision_type in self._collision_types:
[81]69 return True
70 return False
71
[59]72
[106]73class StateProxyPuzzler(Puzzler):
74 def __init__(self, state_source):
75 self._state_source = state_source
76
77 def get_state(self):
78 return self.glue.get_state_of(self._state_source)
79
80
[140]81class StateLogicalAndPuzzler(Puzzler):
82 def __init__(self, *state_sources):
83 self._state_sources = state_sources
84
85 def get_state(self):
86 for state_source in self._state_sources:
87 if not self.glue.get_state_of(state_source):
88 return False
89 return True
90
91
[59]92class Physicser(object):
[93]93 def __init__(self, space):
[123]94 self._space = space
95
96 def get_space(self):
97 return self._space
98
99 def set_game_object(self, game_object):
100 self.game_object = game_object
101
102 def get_shape(self):
103 raise NotImplementedError()
[93]104
105 def add_to_space(self):
[59]106 raise NotImplementedError()
107
[93]108 def remove_from_space(self):
[59]109 raise NotImplementedError()
110
[93]111 def get_render_position(self, surface):
[63]112 raise NotImplementedError()
113
[93]114 def get_angle(self):
115 raise NotImplementedError()
116
117 def apply_impulse(self, j, r=(0, 0)):
[59]118 raise NotImplementedError()
119
120
121class SingleShapePhysicser(Physicser):
[93]122 def __init__(self, space, shape):
123 super(SingleShapePhysicser, self).__init__(space)
[59]124 self._shape = shape
[186]125 shape.physicser = self
[59]126
[123]127 def get_shape(self):
128 return self._shape
129
[93]130 def add_to_space(self):
[123]131 self.get_space().add(self._shape)
[93]132 if not self._shape.body.is_static:
[123]133 self.get_space().add(self._shape.body)
[59]134
[93]135 def remove_from_space(self):
[123]136 self.get_space().remove(self._shape)
[93]137 if not self._shape.body.is_static:
[123]138 self.get_space().remove(self._shape.body)
[59]139
[93]140 def get_render_position(self, surface):
[59]141 pos = self._shape.body.position
142 return pymunk.pygame_util.to_pygame(pos, surface)
[63]143
[93]144 def get_angle(self):
[63]145 return self._shape.body.angle
[59]146
[93]147 def apply_impulse(self, j, r=(0, 0)):
148 return self._shape.body.apply_impulse(j, r)
149
[59]150
151class Renderer(object):
[123]152 def set_game_object(self, game_object):
153 self.game_object = game_object
[104]154
[123]155 def _render_shape(self, surface):
156 shape = self.game_object.get_shape()
[104]157 # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
[123]158 color = getattr(shape, 'color', pygame.color.THECOLORS['lightblue'])
[104]159 # We only explicitly draw Circle and Poly shapes. Everything else we
160 # forward to pymunk.
[123]161 if isinstance(shape, pymunk.Circle):
162 centre = pymunk.pygame_util.to_pygame(shape.body.position, surface)
163 radius = int(shape.radius)
[104]164 pygame.draw.circle(surface, color, centre, radius, 2)
[123]165 elif isinstance(shape, pymunk.Poly):
[104]166 # polygon bounding box
167 points = [pymunk.pygame_util.to_pygame(p, surface)
[123]168 for p in shape.get_vertices()]
[104]169 pygame.draw.lines(surface, color, True, points, 2)
170 else:
[123]171 pymunk.pygame_util.draw(surface, shape)
[104]172
[123]173 def render(self, surface):
[104]174 if options.debug:
[155]175 self._render_shape(surface)
[59]176
[143]177 def animate(self):
178 # Used by time animatations to advance the clock
179 pass
180
[59]181
[63]182def image_pos(image, pos):
183 return (pos[0] - image.get_width() / 2,
184 pos[1] - image.get_height() / 2)
185
186
[59]187class ImageRenderer(Renderer):
[123]188 def __init__(self, image):
[59]189 self._image = image
190
[160]191 def get_image(self):
192 return self._image
193
194 def rotate_image(self, image):
[155]195 angle = self.game_object.get_render_angle() * 180 / math.pi
[160]196 return pygame.transform.rotate(image, angle)
197
198 def render_image(self, surface, image):
199 image = self.rotate_image(image)
200 pos = self.game_object.get_render_position(surface)
[155]201 surface.blit(image, image_pos(image, pos))
[160]202
203 def render(self, surface):
204 self.render_image(surface, self.get_image())
[123]205 super(ImageRenderer, self).render(surface)
[63]206
207
[162]208class ImageStateRenderer(ImageRenderer):
209 def __init__(self, state_images):
210 self._state_images = state_images
211
212 def get_image(self):
213 return self._state_images[self.game_object.puzzler.get_state()]
214
215
[160]216class FacingImageRenderer(ImageRenderer):
[123]217 def __init__(self, left_image, right_image):
[63]218 self._images = {
219 'left': left_image,
220 'right': right_image,
221 }
[159]222 self._face = 'left'
223
224 def _update_facing(self, angle):
225 if abs(angle) < math.pi / 2:
226 self._face = 'right'
227 elif abs(angle) > math.pi / 2:
228 self._face = 'left'
[63]229
[160]230 def rotate_image(self, image):
231 # Facing images don't get rotated.
232 return image
233
234 def get_facing_image(self):
[159]235 return self._images[self._face]
[63]236
[160]237 def get_image(self):
238 angle = self.game_object.get_render_angle()
239 self._update_facing(angle)
240 return self.get_facing_image()
[59]241
242
[143]243class AnimatedFacingImageRenderer(FacingImageRenderer):
244 def __init__(self, left_images, right_images):
245 self._images = {
246 'left': left_images,
247 'right': right_images,
248 }
249 self._frame = 0
250 self._moving = False
[159]251 self._face = 'left'
[143]252
[160]253 def get_facing_image(self):
[159]254 if self._frame >= len(self._images[self._face]):
[143]255 self._frame = 0
[159]256 return self._images[self._face][self._frame]
[143]257
258 def animate(self):
259 if self._moving:
260 self._frame += 1
261 else:
262 self._frame = 0
263
264 def start(self):
265 self._moving = True
266
267 def stop(self):
268 self._moving = False
269
270
271class TimedAnimatedRenderer(ImageRenderer):
272
273 def __init__(self, images):
274 self._images = images
275 self._frame = 0
276 self._image = None
277
[160]278 def get_image(self):
[143]279 if self._frame > len(self._imaages):
280 self._frame = 0
281 return self._images[self._frame]
282
283 def animate(self):
284 self._frame += 1
285
286
[133]287class ShapeRenderer(Renderer):
288 def render(self, surface):
289 self._render_shape(surface)
290 super(ShapeRenderer, self).render(surface)
291
292
293class ShapeStateRenderer(ShapeRenderer):
[126]294 """Renders the shape in a different colour depending on the state.
295
296 Requires the game object it's attached to to have a puzzler.
297 """
[123]298 def render(self, surface):
[126]299 if self.game_object.puzzler.get_state():
300 color = pygame.color.THECOLORS['green']
301 else:
302 color = pygame.color.THECOLORS['red']
303
304 self.game_object.get_shape().color = color
305 super(ShapeStateRenderer, self).render(surface)
[59]306
307
[133]308def damping_velocity_func(body, gravity, damping, dt):
309 """Apply custom damping to this body's velocity.
310 """
311 damping = getattr(body, 'damping', damping)
312 return pymunk.Body.update_velocity(body, gravity, damping, dt)
313
314
315def make_body(mass, moment, position, damping=None):
316 body = pymunk.Body(mass, moment)
[145]317 body.position = tuple(position)
[133]318 if damping is not None:
319 body.damping = damping
320 body.velocity_func = damping_velocity_func
321 return body
322
323
[191]324class Overlay(object):
325 def set_game_object(self, game_object):
326 self.game_object = game_object
327
328 def render(self, surface):
329 pass
330
331 def is_visible(self):
332 return self.game_object.puzzler.get_state()
333
334
335class TextOverlay(Overlay):
336 def __init__(self, text):
337 self.text = text
338 self.widget = TextWidget((20, 20), self.text)
339
340 def render(self, surface):
341 self.widget.draw(surface)
342
343
[59]344class GameObject(object):
345 """A representation of a thing in the game world.
346
347 This has a rendery thing, physicsy things and maybe some other things.
348 """
349
[162]350 zorder = ZORDER_LOW
351
[191]352 def __init__(self, physicser, renderer, puzzler=None, overlay=None):
[93]353 self.physicser = physicser
[123]354 physicser.set_game_object(self)
[93]355 self.physicser.add_to_space()
[59]356 self.renderer = renderer
[123]357 renderer.set_game_object(self)
[81]358 self.puzzler = puzzler
[123]359 if puzzler is not None:
360 puzzler.set_game_object(self)
[191]361 self.overlay = overlay
362 if overlay is not None:
363 self.overlay.set_game_object(self)
[59]364
[123]365 def get_space(self):
366 return self.physicser.get_space()
367
368 def get_shape(self):
369 return self.physicser.get_shape()
370
[93]371 def get_render_position(self, surface):
372 return self.physicser.get_render_position(surface)
373
374 def get_render_angle(self):
375 return self.physicser.get_angle()
[59]376
377 def render(self, surface):
[123]378 return self.renderer.render(surface)
[81]379
[143]380 def animate(self):
381 self.renderer.animate()
382
[186]383 def collide_with_protagonist(self):
384 """Called as a `pre_solve` collision callback with the protagonist.
385
386 You can return `False` to ignore the collision, anything else
387 (including `None`) to process the collision as normal.
388 """
[192]389 return True
[186]390
[81]391
392class FloorSwitch(GameObject):
[162]393 zorder = ZORDER_FLOOR
394
[93]395 def __init__(self, space, position):
[145]396 body = make_body(None, None, position)
[81]397 self.shape = pymunk.Circle(body, 30)
398 self.shape.collision_type = COLLISION_TYPE_SWITCH
399 self.shape.sensor = True
400 super(FloorSwitch, self).__init__(
[93]401 SingleShapePhysicser(space, self.shape),
[162]402 ImageStateRenderer({
403 True: resources.get_image('objects', 'sensor_on.png'),
404 False: resources.get_image('objects', 'sensor_off.png'),
405 }),
[185]406 CollidePuzzler(*SWITCH_PUSHERS),
[81]407 )
408
[106]409
[191]410class Note(GameObject):
411 zorder = ZORDER_FLOOR
412
413 def __init__(self, space, position, message):
414 body = make_body(None, None, position)
415 self.shape = pymunk.Circle(body, 30)
416 self.shape.sensor = True
417 super(Note, self).__init__(
418 SingleShapePhysicser(space, self.shape),
419 ImageRenderer(resources.get_image('objects', 'note.png')),
420 CollidePuzzler(),
421 TextOverlay(message),
422 )
423
424
[106]425class FloorLight(GameObject):
[162]426 zorder = ZORDER_FLOOR
427
[106]428 def __init__(self, space, position, state_source):
[145]429 body = make_body(None, None, position)
[106]430 self.shape = pymunk.Circle(body, 10)
431 self.shape.collision_type = COLLISION_TYPE_SWITCH
432 self.shape.sensor = True
433 super(FloorLight, self).__init__(
434 SingleShapePhysicser(space, self.shape),
[162]435 ImageStateRenderer({
436 True: resources.get_image('objects', 'light_on.png'),
437 False: resources.get_image('objects', 'light_off.png'),
438 }),
[106]439 StateProxyPuzzler(state_source),
440 )
[133]441
442
443class Box(GameObject):
444 def __init__(self, space, position):
445 body = make_body(10, 10000, position, damping=0.5)
446 self.shape = pymunk.Poly(
447 body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
448 self.shape.collision_type = COLLISION_TYPE_BOX
449 super(Box, self).__init__(
450 SingleShapePhysicser(space, self.shape),
[155]451 ImageRenderer(resources.get_image('objects', 'crate.png')),
[133]452 )
[176]453
454
455class Door(GameObject):
456 zorder = ZORDER_FLOOR
457
[186]458 def __init__(self, space, position, destination, dest_pos, key_state=None):
[176]459 body = make_body(pymunk.inf, pymunk.inf, position, damping=0.5)
460 self.shape = pymunk.Poly(
461 body, [(-32, -32), (32, -32), (32, 32), (-32, 32)])
462 self.shape.collision_type = COLLISION_TYPE_DOOR
463 self.shape.sensor = True
464 self.destination = destination
465 self.dest_pos = tuple(dest_pos)
[186]466 if key_state is None:
467 puzzler = YesPuzzler()
468 else:
469 puzzler = StateProxyPuzzler(key_state)
[176]470 super(Door, self).__init__(
471 SingleShapePhysicser(space, self.shape),
472 ImageRenderer(resources.get_image('objects', 'door.png')),
[186]473 puzzler,
[176]474 )
475
[188]476 def collide_with_protagonist(self):
477 if self.puzzler.get_state():
478 DoorEvent.post(self.destination, self.dest_pos)
Note: See TracBrowser for help on using the repository browser.