source: nagslang/game_object.py@ 158:59f05553ffd4

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

Crates look like crates.

File size: 10.3 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
10from nagslang.resources import resources
11
12
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):
20 if not isinstance(puzzler, Puzzler):
21 puzzler = puzzler.puzzler
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
29class Puzzler(object):
30 """Behaviour specific to a puzzle component.
31 """
32 def set_glue(self, glue):
33 self.glue = glue
34
35 def set_game_object(self, game_object):
36 self.game_object = game_object
37
38 def get_state(self):
39 raise NotImplementedError()
40
41
42class FloorSwitchPuzzler(Puzzler):
43 def get_state(self):
44 space = self.game_object.get_space()
45 for shape in space.shape_query(self.game_object.get_shape()):
46 if shape.collision_type in SWITCH_PUSHERS:
47 return True
48 return False
49
50
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
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
70class Physicser(object):
71 def __init__(self, space):
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()
82
83 def add_to_space(self):
84 raise NotImplementedError()
85
86 def remove_from_space(self):
87 raise NotImplementedError()
88
89 def get_render_position(self, surface):
90 raise NotImplementedError()
91
92 def get_angle(self):
93 raise NotImplementedError()
94
95 def apply_impulse(self, j, r=(0, 0)):
96 raise NotImplementedError()
97
98
99class SingleShapePhysicser(Physicser):
100 def __init__(self, space, shape):
101 super(SingleShapePhysicser, self).__init__(space)
102 self._shape = shape
103
104 def get_shape(self):
105 return self._shape
106
107 def add_to_space(self):
108 self.get_space().add(self._shape)
109 if not self._shape.body.is_static:
110 self.get_space().add(self._shape.body)
111
112 def remove_from_space(self):
113 self.get_space().remove(self._shape)
114 if not self._shape.body.is_static:
115 self.get_space().remove(self._shape.body)
116
117 def get_render_position(self, surface):
118 pos = self._shape.body.position
119 return pymunk.pygame_util.to_pygame(pos, surface)
120
121 def get_angle(self):
122 return self._shape.body.angle
123
124 def apply_impulse(self, j, r=(0, 0)):
125 return self._shape.body.apply_impulse(j, r)
126
127
128class Renderer(object):
129 def set_game_object(self, game_object):
130 self.game_object = game_object
131
132 def _render_shape(self, surface):
133 shape = self.game_object.get_shape()
134 # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
135 color = getattr(shape, 'color', pygame.color.THECOLORS['lightblue'])
136 # We only explicitly draw Circle and Poly shapes. Everything else we
137 # forward to pymunk.
138 if isinstance(shape, pymunk.Circle):
139 centre = pymunk.pygame_util.to_pygame(shape.body.position, surface)
140 radius = int(shape.radius)
141 pygame.draw.circle(surface, color, centre, radius, 2)
142 elif isinstance(shape, pymunk.Poly):
143 # polygon bounding box
144 points = [pymunk.pygame_util.to_pygame(p, surface)
145 for p in shape.get_vertices()]
146 pygame.draw.lines(surface, color, True, points, 2)
147 else:
148 pymunk.pygame_util.draw(surface, shape)
149
150 def render(self, surface):
151 if options.debug:
152 self._render_shape(surface)
153
154 def animate(self):
155 # Used by time animatations to advance the clock
156 pass
157
158
159def image_pos(image, pos):
160 return (pos[0] - image.get_width() / 2,
161 pos[1] - image.get_height() / 2)
162
163
164class ImageRenderer(Renderer):
165 def __init__(self, image):
166 self._image = image
167
168 def render(self, surface):
169 pos = self.game_object.get_render_position(surface)
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))
173 super(ImageRenderer, self).render(surface)
174
175
176class FacingImageRenderer(Renderer):
177 def __init__(self, left_image, right_image):
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
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())
191 surface.blit(image, image_pos(image, pos))
192 super(FacingImageRenderer, self).render(surface)
193
194
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
252class ShapeRenderer(Renderer):
253 def render(self, surface):
254 self._render_shape(surface)
255 super(ShapeRenderer, self).render(surface)
256
257
258class ShapeStateRenderer(ShapeRenderer):
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 """
263 def render(self, surface):
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)
271
272
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)
282 body.position = tuple(position)
283 if damping is not None:
284 body.damping = damping
285 body.velocity_func = damping_velocity_func
286 return body
287
288
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
295 def __init__(self, physicser, renderer, puzzler=None):
296 self.physicser = physicser
297 physicser.set_game_object(self)
298 self.physicser.add_to_space()
299 self.renderer = renderer
300 renderer.set_game_object(self)
301 self.puzzler = puzzler
302 if puzzler is not None:
303 puzzler.set_game_object(self)
304 self.zorder = ZORDER_LOW
305
306 def get_space(self):
307 return self.physicser.get_space()
308
309 def get_shape(self):
310 return self.physicser.get_shape()
311
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()
317
318 def render(self, surface):
319 return self.renderer.render(surface)
320
321 def animate(self):
322 self.renderer.animate()
323
324
325class FloorSwitch(GameObject):
326 def __init__(self, space, position):
327 body = make_body(None, None, position)
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__(
332 SingleShapePhysicser(space, self.shape),
333 ShapeStateRenderer(),
334 FloorSwitchPuzzler(),
335 )
336
337
338class FloorLight(GameObject):
339 def __init__(self, space, position, state_source):
340 body = make_body(None, None, position)
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),
346 ShapeStateRenderer(),
347 StateProxyPuzzler(state_source),
348 )
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),
359 ImageRenderer(resources.get_image('objects', 'crate.png')),
360 )
Note: See TracBrowser for help on using the repository browser.