source: nagslang/game_object.py@ 427:3ee839f227ad

Last change on this file since 427:3ee839f227ad was 427:3ee839f227ad, checked in by davidsharpe@…, 8 years ago

Special relativistic gravities.

File size: 22.0 KB
Line 
1import pymunk
2import pymunk.pygame_util
3
4import math
5
6from nagslang import environment
7from nagslang import puzzle
8from nagslang import render
9from nagslang.mutators import FLIP_H, ImageOverlay
10from nagslang.constants import (
11 COLLISION_TYPE_DOOR, COLLISION_TYPE_FURNITURE, COLLISION_TYPE_PROJECTILE,
12 COLLISION_TYPE_SWITCH, COLLISION_TYPE_WEREWOLF_ATTACK,
13 SWITCH_PUSHERS, ZORDER_FLOOR, ZORDER_LOW, ZORDER_HIGH)
14from nagslang.resources import resources
15from nagslang.events import DoorEvent
16
17
18class Result(object):
19 '''
20 Return from an update() function, to add new objects to the world, and/or
21 remove old objects.
22 '''
23 def __init__(self, add=(), remove=()):
24 self.add = add
25 self.remove = remove
26
27 def merge(self, result):
28 if result is not None:
29 self.add += result.add
30 self.remove += result.remove
31 return self
32
33
34def get_editable_game_objects():
35 classes = []
36 for cls_name, cls in globals().iteritems():
37 if isinstance(cls, type) and hasattr(cls, 'requires'):
38 classes.append((cls_name, cls))
39 return classes
40
41
42class Physicser(object):
43 def __init__(self, space):
44 self._space = space
45
46 def get_space(self):
47 return self._space
48
49 def set_space(self, new_space):
50 self._space = new_space
51
52 def set_game_object(self, game_object):
53 self.game_object = game_object
54
55 def get_shape(self):
56 raise NotImplementedError()
57
58 def add_to_space(self):
59 shape = self.get_shape()
60 self.get_space().add(shape)
61 if not shape.body.is_static:
62 self.get_space().add(shape.body)
63
64 def remove_from_space(self):
65 shape = self.get_shape()
66 self.get_space().remove(shape)
67 if not shape.body.is_static:
68 self.get_space().remove(shape.body)
69
70 def get_render_position(self, surface):
71 pos = self.get_shape().body.position
72 return pymunk.pygame_util.to_pygame(pos, surface)
73
74 def get_angle(self):
75 return self.get_shape().body.angle
76
77 def get_velocity(self):
78 return self.get_shape().body.velocity
79
80 def _get_position(self):
81 return self.get_shape().body.position
82
83 def _set_position(self, position):
84 self.get_shape().body.position = position
85
86 position = property(_get_position, _set_position)
87
88 def apply_impulse(self, j, r=(0, 0)):
89 return self.get_shape().body.apply_impulse(j, r)
90
91
92class SingleShapePhysicser(Physicser):
93 def __init__(self, space, shape):
94 super(SingleShapePhysicser, self).__init__(space)
95 self._shape = shape
96 shape.physicser = self
97
98 def get_shape(self):
99 return self._shape
100
101
102class MultiShapePhysicser(Physicser):
103 def __init__(self, space, shape, *extra_shapes):
104 super(MultiShapePhysicser, self).__init__(space)
105 self._shape = shape
106 self._extra_shapes = extra_shapes
107 shape.physicser = self
108
109 def get_shape(self):
110 return self._shape
111
112 def add_to_space(self):
113 shape = self.get_shape()
114 self.get_space().add(shape)
115 if not shape.body.is_static:
116 self.get_space().add(shape.body)
117 for s in self._extra_shapes:
118 self.get_space().add(s)
119
120 def remove_from_space(self):
121 shape = self.get_shape()
122 self.get_space().remove(shape)
123 if not shape.body.is_static:
124 self.get_space().remove(shape.body)
125 for s in self._extra_shapes:
126 self.get_space().remove(s)
127
128
129def damping_velocity_func(body, gravity, damping, dt):
130 """Apply custom damping to this body's velocity.
131 """
132 damping = getattr(body, 'damping', damping)
133 return pymunk.Body.update_velocity(body, gravity, damping, dt)
134
135
136def make_body(mass, moment, position, damping=None):
137 body = pymunk.Body(mass, moment)
138 body.position = tuple(position)
139 if damping is not None:
140 body.damping = damping
141 body.velocity_func = damping_velocity_func
142 return body
143
144
145class GameObject(object):
146 """A representation of a thing in the game world.
147
148 This has a rendery thing, physicsy things and maybe some other things.
149 """
150
151 zorder = ZORDER_LOW
152 is_moving = False # `True` if a movement animation should play.
153
154 def __init__(self, physicser, renderer, puzzler=None, overlay=None,
155 interactible=None):
156 self.lifetime = 0
157 self.physicser = physicser
158 physicser.set_game_object(self)
159 self.physicser.add_to_space()
160 self.renderer = renderer
161 renderer.set_game_object(self)
162 self.puzzler = puzzler
163 if puzzler is not None:
164 puzzler.set_game_object(self)
165 self.overlay = overlay
166 if overlay is not None:
167 self.overlay.set_game_object(self)
168 self.interactible = interactible
169 if interactible is not None:
170 self.interactible.set_game_object(self)
171 self._timers = {}
172 self._active_timers = {}
173
174 def add_timer(self, name, secs):
175 self._timers[name] = secs
176
177 def start_timer(self, name, secs=None):
178 if secs is None:
179 secs = self._timers[name]
180 self._active_timers[name] = secs
181
182 def check_timer(self, name):
183 return name in self._active_timers
184
185 def set_stored_state_dict(self, stored_state):
186 """Override this to set up whatever state storage you want.
187
188 The `stored_state` dict passed in contains whatever saved state we
189 might have for this object. If the return value of this method
190 evaluates to `True`, the contents of the `stored_state` dict will be
191 saved, otherwise it will be discarded.
192 """
193 pass
194
195 def get_space(self):
196 return self.physicser.get_space()
197
198 def get_shape(self):
199 return self.physicser.get_shape()
200
201 def get_render_position(self, surface):
202 return self.physicser.get_render_position(surface)
203
204 def get_render_angle(self):
205 return self.physicser.get_angle()
206
207 def get_facing_direction(self):
208 """Used by rendererd that care what direction an object is facing.
209 """
210 return None
211
212 def render(self, surface):
213 return self.renderer.render(surface)
214
215 def update(self, dt):
216 self.lifetime += dt
217 for timer in self._active_timers.keys():
218 self._active_timers[timer] -= dt
219 if self._active_timers[timer] <= 0:
220 self._active_timers.pop(timer)
221 self.renderer.update(dt)
222
223 def hit(self, weapon):
224 '''Was hit with a weapon (such as a bullet)'''
225 pass
226
227 def collide_with_protagonist(self, protagonist):
228 """Called as a `pre_solve` collision callback with the protagonist.
229
230 You can return `False` to ignore the collision, anything else
231 (including `None`) to process the collision as normal.
232 """
233 return True
234
235 def collide_with_furniture(self, furniture):
236 return True
237
238 def collide_with_claw_attack(self, claw_attack):
239 return True
240
241 def environmental_movement(self, vec):
242 self.physicser.apply_impulse(vec)
243
244 @classmethod
245 def requires(cls):
246 """Hints for the level editor"""
247 return [("name", "string")]
248
249
250class FloorSwitch(GameObject):
251 zorder = ZORDER_FLOOR
252
253 def __init__(self, space, position):
254 body = make_body(None, None, position)
255 self.shape = pymunk.Circle(body, 30)
256 self.shape.collision_type = COLLISION_TYPE_SWITCH
257 self.shape.sensor = True
258 super(FloorSwitch, self).__init__(
259 SingleShapePhysicser(space, self.shape),
260 render.ImageStateRenderer({
261 True: resources.get_image('objects', 'sensor_on.png'),
262 False: resources.get_image('objects', 'sensor_off.png'),
263 }),
264 puzzle.CollidePuzzler(*SWITCH_PUSHERS),
265 )
266
267 @classmethod
268 def requires(cls):
269 return [("name", "string"), ("position", "coordinates")]
270
271
272class Note(GameObject):
273 zorder = ZORDER_FLOOR
274
275 def __init__(self, space, position, message):
276 body = make_body(None, None, position)
277 self.shape = pymunk.Circle(body, 30)
278 self.shape.sensor = True
279 super(Note, self).__init__(
280 SingleShapePhysicser(space, self.shape),
281 render.ImageRenderer(resources.get_image('objects', 'note.png')),
282 puzzle.CollidePuzzler(),
283 render.TextOverlay(message),
284 )
285
286 @classmethod
287 def requires(cls):
288 return [("name", "string"), ("position", "coordinates"),
289 ("message", "text")]
290
291
292class FloorLight(GameObject):
293 zorder = ZORDER_FLOOR
294
295 def __init__(self, space, position, state_source):
296 body = make_body(None, None, position)
297 self.shape = pymunk.Circle(body, 10)
298 self.shape.collision_type = COLLISION_TYPE_SWITCH
299 self.shape.sensor = True
300 super(FloorLight, self).__init__(
301 SingleShapePhysicser(space, self.shape),
302 render.ImageStateRenderer({
303 True: resources.get_image('objects', 'light_on.png'),
304 False: resources.get_image('objects', 'light_off.png'),
305 }),
306 puzzle.StateProxyPuzzler(state_source),
307 )
308
309 @classmethod
310 def requires(cls):
311 return [("name", "string"), ("position", "coordinates"),
312 ("state_source", "puzzler")]
313
314
315class Box(GameObject):
316 def __init__(self, space, position):
317 body = make_body(10, 10000, position, damping=0.5)
318 self.shape = pymunk.Poly(
319 body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
320 self.shape.friction = 0.5
321 self.shape.collision_type = COLLISION_TYPE_FURNITURE
322 super(Box, self).__init__(
323 SingleShapePhysicser(space, self.shape),
324 render.ImageRenderer(resources.get_image('objects', 'crate.png')),
325 )
326
327 @classmethod
328 def requires(cls):
329 return [("name", "string"), ("position", "coordinates"),
330 ("state_source", "puzzler")]
331
332
333class BaseDoor(GameObject):
334 zorder = ZORDER_FLOOR
335 is_open = True
336
337 def __init__(self, space, position, destination, dest_pos, angle,
338 renderer, condition):
339 body = make_body(pymunk.inf, pymunk.inf, position, damping=0.5)
340 self.shape = pymunk.Circle(body, 30)
341 self.shape.collision_type = COLLISION_TYPE_DOOR
342 self.shape.body.angle = float(angle) / 180 * math.pi
343 self.shape.sensor = True
344 self.destination = destination
345 self.dest_pos = tuple(dest_pos)
346 super(BaseDoor, self).__init__(
347 SingleShapePhysicser(space, self.shape),
348 renderer,
349 puzzle.ParentAttrPuzzler('is_open'),
350 interactible=environment.Interactible(
351 environment.Action(self._post_door_event, condition)),
352 )
353
354 def _post_door_event(self, protagonist):
355 DoorEvent.post(self.destination, self.dest_pos)
356
357
358class Door(BaseDoor):
359 def __init__(self, space, position, destination, dest_pos, angle):
360 super(Door, self).__init__(
361 space, position, destination, dest_pos, angle,
362 render.ImageRenderer(resources.get_image('objects', 'door.png')),
363 environment.YesCondition(),
364 )
365
366 @classmethod
367 def requires(cls):
368 return [("name", "string"), ("position", "coordinates"),
369 ("destination", "level name"), ("dest_pos", "coordinate"),
370 ("angle", "degrees")]
371
372
373class PuzzleDoor(BaseDoor):
374 def __init__(self, space, position, destination, dest_pos, angle,
375 key_state):
376 self._key_state = key_state
377 overlay = ImageOverlay(resources.get_image('objects', 'lock.png'))
378 super(PuzzleDoor, self).__init__(
379 space, position, destination, dest_pos, angle,
380 render.ImageStateRenderer({
381 True: resources.get_image('objects', 'door.png'),
382 False: resources.get_image(
383 'objects', 'door.png', transforms=(overlay,)),
384 }),
385 environment.FunctionCondition(lambda p: self.is_open),
386 )
387
388 @property
389 def is_open(self):
390 return self._stored_state['is_open']
391
392 def set_stored_state_dict(self, stored_state):
393 self._stored_state = stored_state
394 self._stored_state.setdefault('is_open', False)
395 return True
396
397 def update(self, dt):
398 if not self.is_open:
399 self._stored_state['is_open'] = self.puzzler.glue.get_state_of(
400 self._key_state)
401 super(PuzzleDoor, self).update(dt)
402
403 @classmethod
404 def requires(cls):
405 return [("name", "string"), ("position", "coordinates"),
406 ("destination", "level name"), ("dest_pos", "coordinate"),
407 ("angle", "degrees"),
408 ("key_state", "puzzler")]
409
410
411class KeyedDoor(BaseDoor):
412 def __init__(self, space, position, destination, dest_pos, angle,
413 key_item=None):
414 self._key_item = key_item
415 overlay = ImageOverlay(
416 resources.get_image('objects', '%s.png' % (key_item,)))
417 super(KeyedDoor, self).__init__(
418 space, position, destination, dest_pos, angle,
419 render.ImageRenderer(resources.get_image(
420 'objects', 'door.png', transforms=(overlay,))),
421 environment.ItemRequiredCondition(key_item),
422 )
423
424 @classmethod
425 def requires(cls):
426 return [("name", "string"), ("position", "coordinates"),
427 ("destination", "level name"), ("dest_pos", "coordinate"),
428 ("angle", "degrees"), ("key_item", "item name")]
429
430
431class Bulkhead(GameObject):
432 zorder = ZORDER_FLOOR
433
434 def __init__(self, space, end1, end2, key_state=None):
435 body = make_body(None, None, (0, 0))
436 self.shape = pymunk.Segment(body, tuple(end1), tuple(end2), 3)
437 self.shape.collision_type = COLLISION_TYPE_DOOR
438 if key_state is None:
439 puzzler = puzzle.YesPuzzler()
440 else:
441 puzzler = puzzle.StateProxyPuzzler(key_state)
442 super(Bulkhead, self).__init__(
443 SingleShapePhysicser(space, self.shape),
444 render.ShapeStateRenderer(),
445 puzzler,
446 )
447
448 def collide_with_protagonist(self, protagonist):
449 if self.puzzler.get_state():
450 # Reject the collision, we can walk through.
451 return False
452 return True
453
454 collide_with_furniture = collide_with_protagonist
455
456 @classmethod
457 def requires(cls):
458 return [("name", "string"), ("end1", "coordinates"),
459 ("end2", "coordinates"), ("key_state", "puzzler")]
460
461
462class ToggleSwitch(GameObject):
463 zorder = ZORDER_LOW
464
465 def __init__(self, space, position):
466 body = make_body(None, None, position)
467 self.shape = pymunk.Circle(body, 20)
468 self.shape.sensor = True
469 super(ToggleSwitch, self).__init__(
470 SingleShapePhysicser(space, self.shape),
471 render.ImageStateRenderer({
472 True: resources.get_image('objects', 'lever.png'),
473 False: resources.get_image(
474 'objects', 'lever.png', transforms=(FLIP_H,)),
475 }),
476 puzzle.ParentAttrPuzzler('toggle_on'),
477 interactible=environment.Interactible(
478 environment.Action(self._toggle)),
479 )
480
481 @property
482 def toggle_on(self):
483 return self._stored_state['toggle_on']
484
485 def _toggle(self, protagonist):
486 self._stored_state['toggle_on'] = not self.toggle_on
487
488 def set_stored_state_dict(self, stored_state):
489 self._stored_state = stored_state
490 # We start in the "off" position.
491 self._stored_state.setdefault('toggle_on', False)
492 return True
493
494 @classmethod
495 def requires(cls):
496 return [("name", "string"), ("position", "coordinates")]
497
498
499class Bullet(GameObject):
500 def __init__(self, space, position, impulse, damage, bullet_type,
501 source_collision_type):
502 body = make_body(1, pymunk.inf, position)
503 body.angle = impulse.angle
504 self.last_position = position
505 self.shape = pymunk.Circle(body, 2)
506 self.shape.sensor = True
507 self.shape.collision_type = COLLISION_TYPE_PROJECTILE
508 self.damage = damage
509 self.type = bullet_type
510 self.source_collision_type = source_collision_type
511 super(Bullet, self).__init__(
512 SingleShapePhysicser(space, self.shape),
513 render.ImageRenderer(resources.get_image(
514 'objects', '%s.png' % self.type)),
515 )
516 self.physicser.apply_impulse(impulse)
517
518 def update(self, dt):
519 super(Bullet, self).update(dt)
520 position = (self.physicser.position.x, self.physicser.position.y)
521 r = self.get_space().segment_query(self.last_position, position)
522 self.last_position = position
523 for collision in r:
524 shape = collision.shape
525 if (shape.collision_type == self.source_collision_type
526 or shape == self.physicser.get_shape()
527 or shape.sensor):
528 continue
529 if hasattr(shape, 'physicser'):
530 shape.physicser.game_object.hit(self)
531 self.physicser.remove_from_space()
532 return Result(remove=[self])
533
534
535class ClawAttack(GameObject):
536 def __init__(self, space, pos, vector, damage):
537 body = make_body(1, pymunk.inf,
538 (pos[0] + (vector.length * math.cos(vector.angle)),
539 pos[1] + (vector.length * math.sin(vector.angle))))
540 body.angle = vector.angle
541 self.shape = pymunk.Circle(body, 30)
542 self.shape.sensor = True
543 self.shape.collision_type = COLLISION_TYPE_WEREWOLF_ATTACK
544 self.damage = damage
545 super(ClawAttack, self).__init__(
546 SingleShapePhysicser(space, self.shape),
547 render.ImageRenderer(resources.get_image(
548 'objects', 'werewolf_SW_claw_attack.png',
549 transforms=(FLIP_H,))),
550 )
551
552 def update(self, dt):
553 super(ClawAttack, self).update(dt)
554 if self.lifetime > 0.1:
555 self.physicser.remove_from_space()
556 return Result(remove=[self])
557
558
559class HostileTerrain(GameObject):
560 zorder = ZORDER_FLOOR
561 damage = None
562 tiles = []
563 tile_alpha = 255
564 tile_frame_ticks = 3
565 # How often to hit the player
566 rate = 5
567
568 def __init__(self, space, position, outline):
569 body = make_body(10, pymunk.inf, position)
570 # Adjust shape relative to position
571 shape_outline = [(p[0] - position[0], p[1] - position[1]) for
572 p in outline]
573 self.shape = pymunk.Poly(body, shape_outline)
574 self._ticks = 0
575 self.shape.collision_type = COLLISION_TYPE_SWITCH
576 self.shape.sensor = True
577 if len(self.tiles) > 1:
578 tile_images = [resources.get_image('tiles', x)
579 for x in self.tiles]
580 renderer = render.TimedTiledRenderer(outline, tile_images,
581 self.tile_frame_ticks,
582 self.tile_alpha)
583 else:
584 tile_image = resources.get_image('tiles', self.tiles[0])
585 renderer = render.TiledRenderer(outline, tile_image,
586 self.tile_alpha)
587 super(HostileTerrain, self).__init__(
588 SingleShapePhysicser(space, self.shape),
589 renderer)
590
591 def collide_with_protagonist(self, protagonist):
592 # We're called every frame we're colliding, so
593 # There are timing issues with stepping on and
594 # off terrian, but as long as the rate is reasonably
595 # low, they shouldn't impact gameplay
596 if self._ticks == 0:
597 self.apply_effect(protagonist)
598 self._ticks += 1
599 if self._ticks > self.rate:
600 self._ticks = 0
601
602 def apply_effect(self, protagonist):
603 protagonist.lose_health(self.damage)
604
605 @classmethod
606 def requires(cls):
607 return [("name", "string"), ("position", "coordinates"),
608 ("outline", "polygon (convex)")]
609
610
611class AcidFloor(HostileTerrain):
612 damage = 1
613 tiles = ['acid.png', 'acid2.png', 'acid3.png']
614 tile_alpha = 200
615 tile_frame_ticks = 10
616
617
618class ForceWolfFloor(HostileTerrain):
619 tiles = ['moonlight.png']
620 rate = 0
621 tile_alpha = 150
622 zorder = ZORDER_HIGH
623
624 def apply_effect(self, protagonist):
625 protagonist.force_wolf_form()
626
627
628class GravityWell(GameObject):
629 zorder = ZORDER_FLOOR
630 # How often to hit the player
631 rate = 5
632
633 def __init__(self, space, position, radius, force):
634 body = make_body(None, None, position)
635 # Adjust shape relative to position
636 self._radius = radius
637 self.shape = pymunk.Circle(body, radius)
638 self.centre = pymunk.Circle(body, 10)
639 self.centre.friction = pymunk.inf
640 self._ticks = 0
641 self.force = force
642 self.shape.collision_type = COLLISION_TYPE_SWITCH
643 self.shape.sensor = True
644 super(GravityWell, self).__init__(
645 MultiShapePhysicser(space, self.shape, self.centre),
646 render.ImageRenderer(resources.get_image(
647 'objects', 'gravity_well.png')),
648 )
649
650 def collide_with_protagonist(self, protagonist):
651 # We're called every frame we're colliding, so
652 # There are timing issues with stepping on and
653 # off terrian, but as long as the rate is reasonably
654 # low, they shouldn't impact gameplay
655 self.apply_effect(protagonist)
656
657 def collide_with_furniture(self, furniture):
658 # We're called every frame we're colliding, so
659 # There are timing issues with stepping on and
660 # off terrian, but as long as the rate is reasonably
661 # low, they shouldn't impact gameplay
662 self.apply_effect(furniture)
663
664 def apply_effect(self, object_to_move):
665 movement = self.physicser.position - object_to_move.physicser.position
666 force = self.force * (1 - (self._radius / movement.length))
667 movement.length = force
668 object_to_move.environmental_movement(-movement)
669
670 @classmethod
671 def requires(cls):
672 return [("name", "string"), ("position", "coordinates"),
673 ("outline", "polygon (convex)")]
Note: See TracBrowser for help on using the repository browser.