source: nagslang/game_object.py@ 162:507df17cfbaf

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

Pictures for lights and switches.

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