source: nagslang/game_object.py@ 143:deac6a4008e7

Last change on this file since 143:deac6a4008e7 was 143:deac6a4008e7, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Hook up protagnist animations

File size: 10.2 KB
RevLine 
[63]1import math
[62]2
[81]3import pygame
4import pymunk
[93]5import pymunk.pygame_util
[81]6
[107]7from nagslang.constants import (
[133]8 SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW)
[104]9from nagslang.options import options
[81]10
[82]11
[106]12class PuzzleGlue(object):
13 """Glue that holds bits of a puzzle together.
14 """
15 def __init__(self):
16 self._components = {}
17
18 def add_component(self, name, puzzler):
19 self._components[name] = puzzler
20 puzzler.set_glue(self)
21
22 def get_state_of(self, name):
23 return self._components[name].get_state()
24
25
[81]26class Puzzler(object):
[106]27 """Behaviour specific to a puzzle component.
28 """
29 def set_glue(self, glue):
30 self.glue = glue
31
[123]32 def set_game_object(self, game_object):
33 self.game_object = game_object
34
[106]35 def get_state(self):
[81]36 raise NotImplementedError()
37
38
39class FloorSwitchPuzzler(Puzzler):
[93]40 def get_state(self):
[123]41 space = self.game_object.get_space()
42 for shape in space.shape_query(self.game_object.get_shape()):
[81]43 if shape.collision_type in SWITCH_PUSHERS:
44 return True
45 return False
46
[59]47
[106]48class StateProxyPuzzler(Puzzler):
49 def __init__(self, state_source):
50 self._state_source = state_source
51
52 def get_state(self):
53 return self.glue.get_state_of(self._state_source)
54
55
[140]56class StateLogicalAndPuzzler(Puzzler):
57 def __init__(self, *state_sources):
58 self._state_sources = state_sources
59
60 def get_state(self):
61 for state_source in self._state_sources:
62 if not self.glue.get_state_of(state_source):
63 return False
64 return True
65
66
[59]67class Physicser(object):
[93]68 def __init__(self, space):
[123]69 self._space = space
70
71 def get_space(self):
72 return self._space
73
74 def set_game_object(self, game_object):
75 self.game_object = game_object
76
77 def get_shape(self):
78 raise NotImplementedError()
[93]79
80 def add_to_space(self):
[59]81 raise NotImplementedError()
82
[93]83 def remove_from_space(self):
[59]84 raise NotImplementedError()
85
[93]86 def get_render_position(self, surface):
[63]87 raise NotImplementedError()
88
[93]89 def get_angle(self):
90 raise NotImplementedError()
91
92 def apply_impulse(self, j, r=(0, 0)):
[59]93 raise NotImplementedError()
94
95
96class SingleShapePhysicser(Physicser):
[93]97 def __init__(self, space, shape):
98 super(SingleShapePhysicser, self).__init__(space)
[59]99 self._shape = shape
100
[123]101 def get_shape(self):
102 return self._shape
103
[93]104 def add_to_space(self):
[123]105 self.get_space().add(self._shape)
[93]106 if not self._shape.body.is_static:
[123]107 self.get_space().add(self._shape.body)
[59]108
[93]109 def remove_from_space(self):
[123]110 self.get_space().remove(self._shape)
[93]111 if not self._shape.body.is_static:
[123]112 self.get_space().remove(self._shape.body)
[59]113
[93]114 def get_render_position(self, surface):
[59]115 pos = self._shape.body.position
116 return pymunk.pygame_util.to_pygame(pos, surface)
[63]117
[93]118 def get_angle(self):
[63]119 return self._shape.body.angle
[59]120
[93]121 def apply_impulse(self, j, r=(0, 0)):
122 return self._shape.body.apply_impulse(j, r)
123
[59]124
125class Renderer(object):
[123]126 def set_game_object(self, game_object):
127 self.game_object = game_object
[104]128
[123]129 def _render_shape(self, surface):
130 shape = self.game_object.get_shape()
[104]131 # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
[123]132 color = getattr(shape, 'color', pygame.color.THECOLORS['lightblue'])
[104]133 # We only explicitly draw Circle and Poly shapes. Everything else we
134 # forward to pymunk.
[123]135 if isinstance(shape, pymunk.Circle):
136 centre = pymunk.pygame_util.to_pygame(shape.body.position, surface)
137 radius = int(shape.radius)
[104]138 pygame.draw.circle(surface, color, centre, radius, 2)
[123]139 elif isinstance(shape, pymunk.Poly):
[104]140 # polygon bounding box
141 points = [pymunk.pygame_util.to_pygame(p, surface)
[123]142 for p in shape.get_vertices()]
[104]143 pygame.draw.lines(surface, color, True, points, 2)
144 else:
[123]145 pymunk.pygame_util.draw(surface, shape)
[104]146
[123]147 def render(self, surface):
148 pos = self.game_object.get_render_position(surface)
149 angle = self.game_object.get_render_angle()
[104]150 if options.debug:
151 self._render_shape(surface, pos, angle)
[59]152
[143]153 def animate(self):
154 # Used by time animatations to advance the clock
155 pass
156
[59]157
[63]158def image_pos(image, pos):
159 return (pos[0] - image.get_width() / 2,
160 pos[1] - image.get_height() / 2)
161
162
[59]163class ImageRenderer(Renderer):
[123]164 def __init__(self, image):
[59]165 self._image = image
166
[123]167 def render(self, surface):
168 pos = self.game_object.get_render_position(surface)
[63]169 surface.blit(self._image, image_pos(self._image, pos))
[123]170 super(ImageRenderer, self).render(surface)
[63]171
172
173class FacingImageRenderer(Renderer):
[123]174 def __init__(self, left_image, right_image):
[63]175 self._images = {
176 'left': left_image,
177 'right': right_image,
178 }
179
180 def get_image(self, angle):
181 if abs(angle) < math.pi / 2:
182 return self._images['right']
183 return self._images['left']
184
[123]185 def render(self, surface):
186 pos = self.game_object.get_render_position(surface)
187 image = self.get_image(self.game_object.get_render_angle())
[63]188 surface.blit(image, image_pos(image, pos))
[123]189 super(FacingImageRenderer, self).render(surface)
[59]190
191
[143]192class AnimatedFacingImageRenderer(FacingImageRenderer):
193 def __init__(self, left_images, right_images):
194 self._images = {
195 'left': left_images,
196 'right': right_images,
197 }
198 self._frame = 0
199 self._moving = False
200
201 def get_image(self, angle):
202 if abs(angle) < math.pi / 2:
203 face = 'right'
204 else:
205 face = 'left'
206 if self._frame >= len(self._images[face]):
207 self._frame = 0
208 return self._images[face][self._frame]
209
210 def render(self, surface):
211 pos = self.game_object.get_render_position(surface)
212 image = self.get_image(self.game_object.get_render_angle())
213 surface.blit(image, image_pos(image, pos))
214 super(FacingImageRenderer, self).render(surface)
215
216 def animate(self):
217 if self._moving:
218 self._frame += 1
219 else:
220 self._frame = 0
221
222 def start(self):
223 self._moving = True
224
225 def stop(self):
226 self._moving = False
227
228
229class TimedAnimatedRenderer(ImageRenderer):
230
231 def __init__(self, images):
232 self._images = images
233 self._frame = 0
234 self._image = None
235
236 def _get_image(self):
237 if self._frame > len(self._imaages):
238 self._frame = 0
239 return self._images[self._frame]
240
241 def render(self, surface):
242 self._image = self._get_image()
243 super(TimedAnimatedRenderer, self).render(surface)
244
245 def animate(self):
246 self._frame += 1
247
248
[133]249class ShapeRenderer(Renderer):
250 def render(self, surface):
251 self._render_shape(surface)
252 super(ShapeRenderer, self).render(surface)
253
254
255class ShapeStateRenderer(ShapeRenderer):
[126]256 """Renders the shape in a different colour depending on the state.
257
258 Requires the game object it's attached to to have a puzzler.
259 """
[123]260 def render(self, surface):
[126]261 if self.game_object.puzzler.get_state():
262 color = pygame.color.THECOLORS['green']
263 else:
264 color = pygame.color.THECOLORS['red']
265
266 self.game_object.get_shape().color = color
267 super(ShapeStateRenderer, self).render(surface)
[59]268
269
[133]270def damping_velocity_func(body, gravity, damping, dt):
271 """Apply custom damping to this body's velocity.
272 """
273 damping = getattr(body, 'damping', damping)
274 return pymunk.Body.update_velocity(body, gravity, damping, dt)
275
276
277def make_body(mass, moment, position, damping=None):
278 body = pymunk.Body(mass, moment)
279 body.position = position
280 if damping is not None:
281 body.damping = damping
282 body.velocity_func = damping_velocity_func
283 return body
284
285
[59]286class GameObject(object):
287 """A representation of a thing in the game world.
288
289 This has a rendery thing, physicsy things and maybe some other things.
290 """
291
[93]292 def __init__(self, physicser, renderer, puzzler=None):
293 self.physicser = physicser
[123]294 physicser.set_game_object(self)
[93]295 self.physicser.add_to_space()
[59]296 self.renderer = renderer
[123]297 renderer.set_game_object(self)
[81]298 self.puzzler = puzzler
[123]299 if puzzler is not None:
300 puzzler.set_game_object(self)
[107]301 self.zorder = ZORDER_LOW
[59]302
[123]303 def get_space(self):
304 return self.physicser.get_space()
305
306 def get_shape(self):
307 return self.physicser.get_shape()
308
[93]309 def get_render_position(self, surface):
310 return self.physicser.get_render_position(surface)
311
312 def get_render_angle(self):
313 return self.physicser.get_angle()
[59]314
315 def render(self, surface):
[123]316 return self.renderer.render(surface)
[81]317
[143]318 def animate(self):
319 self.renderer.animate()
320
[81]321
322class FloorSwitch(GameObject):
[93]323 def __init__(self, space, position):
[81]324 body = pymunk.Body()
325 body.position = position
326 self.shape = pymunk.Circle(body, 30)
327 self.shape.collision_type = COLLISION_TYPE_SWITCH
328 self.shape.sensor = True
329 super(FloorSwitch, self).__init__(
[93]330 SingleShapePhysicser(space, self.shape),
[126]331 ShapeStateRenderer(),
[123]332 FloorSwitchPuzzler(),
[81]333 )
334
[106]335
336class FloorLight(GameObject):
337 def __init__(self, space, position, state_source):
338 body = pymunk.Body()
339 body.position = position
340 self.shape = pymunk.Circle(body, 10)
341 self.shape.collision_type = COLLISION_TYPE_SWITCH
342 self.shape.sensor = True
343 super(FloorLight, self).__init__(
344 SingleShapePhysicser(space, self.shape),
[126]345 ShapeStateRenderer(),
[106]346 StateProxyPuzzler(state_source),
347 )
[133]348
349
350class Box(GameObject):
351 def __init__(self, space, position):
352 body = make_body(10, 10000, position, damping=0.5)
353 self.shape = pymunk.Poly(
354 body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
355 self.shape.collision_type = COLLISION_TYPE_BOX
356 super(Box, self).__init__(
357 SingleShapePhysicser(space, self.shape),
358 ShapeRenderer(),
359 )
Note: See TracBrowser for help on using the repository browser.