source: nagslang/game_object.py@ 155:b455873020be

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

Crates look like crates.

File size: 10.3 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 }
182
183 def get_image(self, angle):
184 if abs(angle) < math.pi / 2:
185 return self._images['right']
186 return self._images['left']
187
[123]188 def render(self, surface):
189 pos = self.game_object.get_render_position(surface)
190 image = self.get_image(self.game_object.get_render_angle())
[63]191 surface.blit(image, image_pos(image, pos))
[123]192 super(FacingImageRenderer, self).render(surface)
[59]193
194
[143]195class AnimatedFacingImageRenderer(FacingImageRenderer):
196 def __init__(self, left_images, right_images):
197 self._images = {
198 'left': left_images,
199 'right': right_images,
200 }
201 self._frame = 0
202 self._moving = False
203
204 def get_image(self, angle):
205 if abs(angle) < math.pi / 2:
206 face = 'right'
207 else:
208 face = 'left'
209 if self._frame >= len(self._images[face]):
210 self._frame = 0
211 return self._images[face][self._frame]
212
213 def render(self, surface):
214 pos = self.game_object.get_render_position(surface)
215 image = self.get_image(self.game_object.get_render_angle())
216 surface.blit(image, image_pos(image, pos))
217 super(FacingImageRenderer, self).render(surface)
218
219 def animate(self):
220 if self._moving:
221 self._frame += 1
222 else:
223 self._frame = 0
224
225 def start(self):
226 self._moving = True
227
228 def stop(self):
229 self._moving = False
230
231
232class TimedAnimatedRenderer(ImageRenderer):
233
234 def __init__(self, images):
235 self._images = images
236 self._frame = 0
237 self._image = None
238
239 def _get_image(self):
240 if self._frame > len(self._imaages):
241 self._frame = 0
242 return self._images[self._frame]
243
244 def render(self, surface):
245 self._image = self._get_image()
246 super(TimedAnimatedRenderer, self).render(surface)
247
248 def animate(self):
249 self._frame += 1
250
251
[133]252class ShapeRenderer(Renderer):
253 def render(self, surface):
254 self._render_shape(surface)
255 super(ShapeRenderer, self).render(surface)
256
257
258class ShapeStateRenderer(ShapeRenderer):
[126]259 """Renders the shape in a different colour depending on the state.
260
261 Requires the game object it's attached to to have a puzzler.
262 """
[123]263 def render(self, surface):
[126]264 if self.game_object.puzzler.get_state():
265 color = pygame.color.THECOLORS['green']
266 else:
267 color = pygame.color.THECOLORS['red']
268
269 self.game_object.get_shape().color = color
270 super(ShapeStateRenderer, self).render(surface)
[59]271
272
[133]273def damping_velocity_func(body, gravity, damping, dt):
274 """Apply custom damping to this body's velocity.
275 """
276 damping = getattr(body, 'damping', damping)
277 return pymunk.Body.update_velocity(body, gravity, damping, dt)
278
279
280def make_body(mass, moment, position, damping=None):
281 body = pymunk.Body(mass, moment)
[145]282 body.position = tuple(position)
[133]283 if damping is not None:
284 body.damping = damping
285 body.velocity_func = damping_velocity_func
286 return body
287
288
[59]289class GameObject(object):
290 """A representation of a thing in the game world.
291
292 This has a rendery thing, physicsy things and maybe some other things.
293 """
294
[93]295 def __init__(self, physicser, renderer, puzzler=None):
296 self.physicser = physicser
[123]297 physicser.set_game_object(self)
[93]298 self.physicser.add_to_space()
[59]299 self.renderer = renderer
[123]300 renderer.set_game_object(self)
[81]301 self.puzzler = puzzler
[123]302 if puzzler is not None:
303 puzzler.set_game_object(self)
[107]304 self.zorder = ZORDER_LOW
[59]305
[123]306 def get_space(self):
307 return self.physicser.get_space()
308
309 def get_shape(self):
310 return self.physicser.get_shape()
311
[93]312 def get_render_position(self, surface):
313 return self.physicser.get_render_position(surface)
314
315 def get_render_angle(self):
316 return self.physicser.get_angle()
[59]317
318 def render(self, surface):
[123]319 return self.renderer.render(surface)
[81]320
[143]321 def animate(self):
322 self.renderer.animate()
323
[81]324
325class FloorSwitch(GameObject):
[93]326 def __init__(self, space, position):
[145]327 body = make_body(None, None, position)
[81]328 self.shape = pymunk.Circle(body, 30)
329 self.shape.collision_type = COLLISION_TYPE_SWITCH
330 self.shape.sensor = True
331 super(FloorSwitch, self).__init__(
[93]332 SingleShapePhysicser(space, self.shape),
[126]333 ShapeStateRenderer(),
[123]334 FloorSwitchPuzzler(),
[81]335 )
336
[106]337
338class FloorLight(GameObject):
339 def __init__(self, space, position, state_source):
[145]340 body = make_body(None, None, position)
[106]341 self.shape = pymunk.Circle(body, 10)
342 self.shape.collision_type = COLLISION_TYPE_SWITCH
343 self.shape.sensor = True
344 super(FloorLight, self).__init__(
345 SingleShapePhysicser(space, self.shape),
[126]346 ShapeStateRenderer(),
[106]347 StateProxyPuzzler(state_source),
348 )
[133]349
350
351class Box(GameObject):
352 def __init__(self, space, position):
353 body = make_body(10, 10000, position, damping=0.5)
354 self.shape = pymunk.Poly(
355 body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
356 self.shape.collision_type = COLLISION_TYPE_BOX
357 super(Box, self).__init__(
358 SingleShapePhysicser(space, self.shape),
[155]359 ImageRenderer(resources.get_image('objects', 'crate.png')),
[133]360 )
Note: See TracBrowser for help on using the repository browser.