source: nagslang/game_object.py@ 145:0c49627920eb

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

Load game objects from level.

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