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

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

Document collision handler a little better.

File size: 13.5 KB
Line 
1import math
2
3import pygame
4import pymunk
5import pymunk.pygame_util
6
7from nagslang.constants import (
8 SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW,
9 ZORDER_FLOOR, COLLISION_TYPE_DOOR, COLLISION_TYPE_PLAYER)
10from nagslang.options import options
11from nagslang.resources import resources
12from nagslang.events import DoorEvent
13from nagslang.widgets.text import TextWidget
14
15
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):
23 if not isinstance(puzzler, Puzzler):
24 puzzler = puzzler.puzzler
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
32class Puzzler(object):
33 """Behaviour specific to a puzzle component.
34 """
35 def set_glue(self, glue):
36 self.glue = glue
37
38 def set_game_object(self, game_object):
39 self.game_object = game_object
40
41 def get_state(self):
42 raise NotImplementedError()
43
44
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
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
65 def get_state(self):
66 space = self.game_object.get_space()
67 for shape in space.shape_query(self.game_object.get_shape()):
68 if shape.collision_type in self._collision_types:
69 return True
70 return False
71
72
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
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
92class Physicser(object):
93 def __init__(self, space):
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()
104
105 def add_to_space(self):
106 raise NotImplementedError()
107
108 def remove_from_space(self):
109 raise NotImplementedError()
110
111 def get_render_position(self, surface):
112 raise NotImplementedError()
113
114 def get_angle(self):
115 raise NotImplementedError()
116
117 def apply_impulse(self, j, r=(0, 0)):
118 raise NotImplementedError()
119
120
121class SingleShapePhysicser(Physicser):
122 def __init__(self, space, shape):
123 super(SingleShapePhysicser, self).__init__(space)
124 self._shape = shape
125 shape.physicser = self
126
127 def get_shape(self):
128 return self._shape
129
130 def add_to_space(self):
131 self.get_space().add(self._shape)
132 if not self._shape.body.is_static:
133 self.get_space().add(self._shape.body)
134
135 def remove_from_space(self):
136 self.get_space().remove(self._shape)
137 if not self._shape.body.is_static:
138 self.get_space().remove(self._shape.body)
139
140 def get_render_position(self, surface):
141 pos = self._shape.body.position
142 return pymunk.pygame_util.to_pygame(pos, surface)
143
144 def get_angle(self):
145 return self._shape.body.angle
146
147 def apply_impulse(self, j, r=(0, 0)):
148 return self._shape.body.apply_impulse(j, r)
149
150
151class Renderer(object):
152 def set_game_object(self, game_object):
153 self.game_object = game_object
154
155 def _render_shape(self, surface):
156 shape = self.game_object.get_shape()
157 # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
158 color = getattr(shape, 'color', pygame.color.THECOLORS['lightblue'])
159 # We only explicitly draw Circle and Poly shapes. Everything else we
160 # forward to pymunk.
161 if isinstance(shape, pymunk.Circle):
162 centre = pymunk.pygame_util.to_pygame(shape.body.position, surface)
163 radius = int(shape.radius)
164 pygame.draw.circle(surface, color, centre, radius, 2)
165 elif isinstance(shape, pymunk.Poly):
166 # polygon bounding box
167 points = [pymunk.pygame_util.to_pygame(p, surface)
168 for p in shape.get_vertices()]
169 pygame.draw.lines(surface, color, True, points, 2)
170 else:
171 pymunk.pygame_util.draw(surface, shape)
172
173 def render(self, surface):
174 if options.debug:
175 self._render_shape(surface)
176
177 def animate(self):
178 # Used by time animatations to advance the clock
179 pass
180
181
182def image_pos(image, pos):
183 return (pos[0] - image.get_width() / 2,
184 pos[1] - image.get_height() / 2)
185
186
187class ImageRenderer(Renderer):
188 def __init__(self, image):
189 self._image = image
190
191 def get_image(self):
192 return self._image
193
194 def rotate_image(self, image):
195 angle = self.game_object.get_render_angle() * 180 / math.pi
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)
201 surface.blit(image, image_pos(image, pos))
202
203 def render(self, surface):
204 self.render_image(surface, self.get_image())
205 super(ImageRenderer, self).render(surface)
206
207
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
216class FacingImageRenderer(ImageRenderer):
217 def __init__(self, left_image, right_image):
218 self._images = {
219 'left': left_image,
220 'right': right_image,
221 }
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'
229
230 def rotate_image(self, image):
231 # Facing images don't get rotated.
232 return image
233
234 def get_facing_image(self):
235 return self._images[self._face]
236
237 def get_image(self):
238 angle = self.game_object.get_render_angle()
239 self._update_facing(angle)
240 return self.get_facing_image()
241
242
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
251 self._face = 'left'
252
253 def get_facing_image(self):
254 if self._frame >= len(self._images[self._face]):
255 self._frame = 0
256 return self._images[self._face][self._frame]
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
278 def get_image(self):
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
287class ShapeRenderer(Renderer):
288 def render(self, surface):
289 self._render_shape(surface)
290 super(ShapeRenderer, self).render(surface)
291
292
293class ShapeStateRenderer(ShapeRenderer):
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 """
298 def render(self, surface):
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)
306
307
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)
317 body.position = tuple(position)
318 if damping is not None:
319 body.damping = damping
320 body.velocity_func = damping_velocity_func
321 return body
322
323
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
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
350 zorder = ZORDER_LOW
351
352 def __init__(self, physicser, renderer, puzzler=None, overlay=None):
353 self.physicser = physicser
354 physicser.set_game_object(self)
355 self.physicser.add_to_space()
356 self.renderer = renderer
357 renderer.set_game_object(self)
358 self.puzzler = puzzler
359 if puzzler is not None:
360 puzzler.set_game_object(self)
361 self.overlay = overlay
362 if overlay is not None:
363 self.overlay.set_game_object(self)
364
365 def get_space(self):
366 return self.physicser.get_space()
367
368 def get_shape(self):
369 return self.physicser.get_shape()
370
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()
376
377 def render(self, surface):
378 return self.renderer.render(surface)
379
380 def animate(self):
381 self.renderer.animate()
382
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 """
389 return True
390
391
392class FloorSwitch(GameObject):
393 zorder = ZORDER_FLOOR
394
395 def __init__(self, space, position):
396 body = make_body(None, None, position)
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__(
401 SingleShapePhysicser(space, self.shape),
402 ImageStateRenderer({
403 True: resources.get_image('objects', 'sensor_on.png'),
404 False: resources.get_image('objects', 'sensor_off.png'),
405 }),
406 CollidePuzzler(*SWITCH_PUSHERS),
407 )
408
409
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
425class FloorLight(GameObject):
426 zorder = ZORDER_FLOOR
427
428 def __init__(self, space, position, state_source):
429 body = make_body(None, None, position)
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),
435 ImageStateRenderer({
436 True: resources.get_image('objects', 'light_on.png'),
437 False: resources.get_image('objects', 'light_off.png'),
438 }),
439 StateProxyPuzzler(state_source),
440 )
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),
451 ImageRenderer(resources.get_image('objects', 'crate.png')),
452 )
453
454
455class Door(GameObject):
456 zorder = ZORDER_FLOOR
457
458 def __init__(self, space, position, destination, dest_pos, key_state=None):
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)
466 if key_state is None:
467 puzzler = YesPuzzler()
468 else:
469 puzzler = StateProxyPuzzler(key_state)
470 super(Door, self).__init__(
471 SingleShapePhysicser(space, self.shape),
472 ImageRenderer(resources.get_image('objects', 'door.png')),
473 puzzler,
474 )
475
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.