source: nagslang/game_object.py@ 144:829c8b6e142d

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

Hook up protagnist animations

File size: 10.2 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)
9from nagslang.options import options
10
11
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
26class Puzzler(object):
27 """Behaviour specific to a puzzle component.
28 """
29 def set_glue(self, glue):
30 self.glue = glue
31
32 def set_game_object(self, game_object):
33 self.game_object = game_object
34
35 def get_state(self):
36 raise NotImplementedError()
37
38
39class FloorSwitchPuzzler(Puzzler):
40 def get_state(self):
41 space = self.game_object.get_space()
42 for shape in space.shape_query(self.game_object.get_shape()):
43 if shape.collision_type in SWITCH_PUSHERS:
44 return True
45 return False
46
47
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
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
67class Physicser(object):
68 def __init__(self, space):
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()
79
80 def add_to_space(self):
81 raise NotImplementedError()
82
83 def remove_from_space(self):
84 raise NotImplementedError()
85
86 def get_render_position(self, surface):
87 raise NotImplementedError()
88
89 def get_angle(self):
90 raise NotImplementedError()
91
92 def apply_impulse(self, j, r=(0, 0)):
93 raise NotImplementedError()
94
95
96class SingleShapePhysicser(Physicser):
97 def __init__(self, space, shape):
98 super(SingleShapePhysicser, self).__init__(space)
99 self._shape = shape
100
101 def get_shape(self):
102 return self._shape
103
104 def add_to_space(self):
105 self.get_space().add(self._shape)
106 if not self._shape.body.is_static:
107 self.get_space().add(self._shape.body)
108
109 def remove_from_space(self):
110 self.get_space().remove(self._shape)
111 if not self._shape.body.is_static:
112 self.get_space().remove(self._shape.body)
113
114 def get_render_position(self, surface):
115 pos = self._shape.body.position
116 return pymunk.pygame_util.to_pygame(pos, surface)
117
118 def get_angle(self):
119 return self._shape.body.angle
120
121 def apply_impulse(self, j, r=(0, 0)):
122 return self._shape.body.apply_impulse(j, r)
123
124
125class Renderer(object):
126 def set_game_object(self, game_object):
127 self.game_object = game_object
128
129 def _render_shape(self, surface):
130 shape = self.game_object.get_shape()
131 # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
132 color = getattr(shape, 'color', pygame.color.THECOLORS['lightblue'])
133 # We only explicitly draw Circle and Poly shapes. Everything else we
134 # forward to pymunk.
135 if isinstance(shape, pymunk.Circle):
136 centre = pymunk.pygame_util.to_pygame(shape.body.position, surface)
137 radius = int(shape.radius)
138 pygame.draw.circle(surface, color, centre, radius, 2)
139 elif isinstance(shape, pymunk.Poly):
140 # polygon bounding box
141 points = [pymunk.pygame_util.to_pygame(p, surface)
142 for p in shape.get_vertices()]
143 pygame.draw.lines(surface, color, True, points, 2)
144 else:
145 pymunk.pygame_util.draw(surface, shape)
146
147 def render(self, surface):
148 pos = self.game_object.get_render_position(surface)
149 angle = self.game_object.get_render_angle()
150 if options.debug:
151 self._render_shape(surface, pos, angle)
152
153 def animate(self):
154 # Used by time animatations to advance the clock
155 pass
156
157
158def image_pos(image, pos):
159 return (pos[0] - image.get_width() / 2,
160 pos[1] - image.get_height() / 2)
161
162
163class ImageRenderer(Renderer):
164 def __init__(self, image):
165 self._image = image
166
167 def render(self, surface):
168 pos = self.game_object.get_render_position(surface)
169 surface.blit(self._image, image_pos(self._image, pos))
170 super(ImageRenderer, self).render(surface)
171
172
173class FacingImageRenderer(Renderer):
174 def __init__(self, left_image, right_image):
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
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())
188 surface.blit(image, image_pos(image, pos))
189 super(FacingImageRenderer, self).render(surface)
190
191
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
249class ShapeRenderer(Renderer):
250 def render(self, surface):
251 self._render_shape(surface)
252 super(ShapeRenderer, self).render(surface)
253
254
255class ShapeStateRenderer(ShapeRenderer):
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 """
260 def render(self, surface):
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)
268
269
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
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
292 def __init__(self, physicser, renderer, puzzler=None):
293 self.physicser = physicser
294 physicser.set_game_object(self)
295 self.physicser.add_to_space()
296 self.renderer = renderer
297 renderer.set_game_object(self)
298 self.puzzler = puzzler
299 if puzzler is not None:
300 puzzler.set_game_object(self)
301 self.zorder = ZORDER_LOW
302
303 def get_space(self):
304 return self.physicser.get_space()
305
306 def get_shape(self):
307 return self.physicser.get_shape()
308
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()
314
315 def render(self, surface):
316 return self.renderer.render(surface)
317
318 def animate(self):
319 self.renderer.animate()
320
321
322class FloorSwitch(GameObject):
323 def __init__(self, space, position):
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__(
330 SingleShapePhysicser(space, self.shape),
331 ShapeStateRenderer(),
332 FloorSwitchPuzzler(),
333 )
334
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),
345 ShapeStateRenderer(),
346 StateProxyPuzzler(state_source),
347 )
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.