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
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 if not isinstance(puzzler, Puzzler):
20 puzzler = puzzler.puzzler
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
28class Puzzler(object):
29 """Behaviour specific to a puzzle component.
30 """
31 def set_glue(self, glue):
32 self.glue = glue
33
34 def set_game_object(self, game_object):
35 self.game_object = game_object
36
37 def get_state(self):
38 raise NotImplementedError()
39
40
41class FloorSwitchPuzzler(Puzzler):
42 def get_state(self):
43 space = self.game_object.get_space()
44 for shape in space.shape_query(self.game_object.get_shape()):
45 if shape.collision_type in SWITCH_PUSHERS:
46 return True
47 return False
48
49
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
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
69class Physicser(object):
70 def __init__(self, space):
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()
81
82 def add_to_space(self):
83 raise NotImplementedError()
84
85 def remove_from_space(self):
86 raise NotImplementedError()
87
88 def get_render_position(self, surface):
89 raise NotImplementedError()
90
91 def get_angle(self):
92 raise NotImplementedError()
93
94 def apply_impulse(self, j, r=(0, 0)):
95 raise NotImplementedError()
96
97
98class SingleShapePhysicser(Physicser):
99 def __init__(self, space, shape):
100 super(SingleShapePhysicser, self).__init__(space)
101 self._shape = shape
102
103 def get_shape(self):
104 return self._shape
105
106 def add_to_space(self):
107 self.get_space().add(self._shape)
108 if not self._shape.body.is_static:
109 self.get_space().add(self._shape.body)
110
111 def remove_from_space(self):
112 self.get_space().remove(self._shape)
113 if not self._shape.body.is_static:
114 self.get_space().remove(self._shape.body)
115
116 def get_render_position(self, surface):
117 pos = self._shape.body.position
118 return pymunk.pygame_util.to_pygame(pos, surface)
119
120 def get_angle(self):
121 return self._shape.body.angle
122
123 def apply_impulse(self, j, r=(0, 0)):
124 return self._shape.body.apply_impulse(j, r)
125
126
127class Renderer(object):
128 def set_game_object(self, game_object):
129 self.game_object = game_object
130
131 def _render_shape(self, surface):
132 shape = self.game_object.get_shape()
133 # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
134 color = getattr(shape, 'color', pygame.color.THECOLORS['lightblue'])
135 # We only explicitly draw Circle and Poly shapes. Everything else we
136 # forward to pymunk.
137 if isinstance(shape, pymunk.Circle):
138 centre = pymunk.pygame_util.to_pygame(shape.body.position, surface)
139 radius = int(shape.radius)
140 pygame.draw.circle(surface, color, centre, radius, 2)
141 elif isinstance(shape, pymunk.Poly):
142 # polygon bounding box
143 points = [pymunk.pygame_util.to_pygame(p, surface)
144 for p in shape.get_vertices()]
145 pygame.draw.lines(surface, color, True, points, 2)
146 else:
147 pymunk.pygame_util.draw(surface, shape)
148
149 def render(self, surface):
150 pos = self.game_object.get_render_position(surface)
151 angle = self.game_object.get_render_angle()
152 if options.debug:
153 self._render_shape(surface, pos, angle)
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 render(self, surface):
170 pos = self.game_object.get_render_position(surface)
171 surface.blit(self._image, image_pos(self._image, pos))
172 super(ImageRenderer, self).render(surface)
173
174
175class FacingImageRenderer(Renderer):
176 def __init__(self, left_image, right_image):
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
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())
190 surface.blit(image, image_pos(image, pos))
191 super(FacingImageRenderer, self).render(surface)
192
193
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
251class ShapeRenderer(Renderer):
252 def render(self, surface):
253 self._render_shape(surface)
254 super(ShapeRenderer, self).render(surface)
255
256
257class ShapeStateRenderer(ShapeRenderer):
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 """
262 def render(self, surface):
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)
270
271
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)
281 body.position = tuple(position)
282 if damping is not None:
283 body.damping = damping
284 body.velocity_func = damping_velocity_func
285 return body
286
287
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
294 def __init__(self, physicser, renderer, puzzler=None):
295 self.physicser = physicser
296 physicser.set_game_object(self)
297 self.physicser.add_to_space()
298 self.renderer = renderer
299 renderer.set_game_object(self)
300 self.puzzler = puzzler
301 if puzzler is not None:
302 puzzler.set_game_object(self)
303 self.zorder = ZORDER_LOW
304
305 def get_space(self):
306 return self.physicser.get_space()
307
308 def get_shape(self):
309 return self.physicser.get_shape()
310
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()
316
317 def render(self, surface):
318 return self.renderer.render(surface)
319
320 def animate(self):
321 self.renderer.animate()
322
323
324class FloorSwitch(GameObject):
325 def __init__(self, space, position):
326 body = make_body(None, None, position)
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__(
331 SingleShapePhysicser(space, self.shape),
332 ShapeStateRenderer(),
333 FloorSwitchPuzzler(),
334 )
335
336
337class FloorLight(GameObject):
338 def __init__(self, space, position, state_source):
339 body = make_body(None, None, 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.