source: nagslang/game_object.py@ 159:f80323140317

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

Stickier facings

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