1 | import math
|
---|
2 |
|
---|
3 | import pygame
|
---|
4 | import pymunk
|
---|
5 | import pymunk.pygame_util
|
---|
6 |
|
---|
7 | from nagslang.constants import (
|
---|
8 | SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW)
|
---|
9 | from nagslang.options import options
|
---|
10 |
|
---|
11 |
|
---|
12 | class 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 |
|
---|
26 | class 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 |
|
---|
39 | class 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 |
|
---|
48 | class 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 |
|
---|
56 | class 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 |
|
---|
67 | class 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 |
|
---|
96 | class 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 |
|
---|
125 | class 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 |
|
---|
154 | def image_pos(image, pos):
|
---|
155 | return (pos[0] - image.get_width() / 2,
|
---|
156 | pos[1] - image.get_height() / 2)
|
---|
157 |
|
---|
158 |
|
---|
159 | class ImageRenderer(Renderer):
|
---|
160 | def __init__(self, image):
|
---|
161 | self._image = image
|
---|
162 |
|
---|
163 | def render(self, surface):
|
---|
164 | pos = self.game_object.get_render_position(surface)
|
---|
165 | surface.blit(self._image, image_pos(self._image, pos))
|
---|
166 | super(ImageRenderer, self).render(surface)
|
---|
167 |
|
---|
168 |
|
---|
169 | class FacingImageRenderer(Renderer):
|
---|
170 | def __init__(self, left_image, right_image):
|
---|
171 | self._images = {
|
---|
172 | 'left': left_image,
|
---|
173 | 'right': right_image,
|
---|
174 | }
|
---|
175 |
|
---|
176 | def get_image(self, angle):
|
---|
177 | if abs(angle) < math.pi / 2:
|
---|
178 | return self._images['right']
|
---|
179 | return self._images['left']
|
---|
180 |
|
---|
181 | def render(self, surface):
|
---|
182 | pos = self.game_object.get_render_position(surface)
|
---|
183 | image = self.get_image(self.game_object.get_render_angle())
|
---|
184 | surface.blit(image, image_pos(image, pos))
|
---|
185 | super(FacingImageRenderer, self).render(surface)
|
---|
186 |
|
---|
187 |
|
---|
188 | class ShapeRenderer(Renderer):
|
---|
189 | def render(self, surface):
|
---|
190 | self._render_shape(surface)
|
---|
191 | super(ShapeRenderer, self).render(surface)
|
---|
192 |
|
---|
193 |
|
---|
194 | class ShapeStateRenderer(ShapeRenderer):
|
---|
195 | """Renders the shape in a different colour depending on the state.
|
---|
196 |
|
---|
197 | Requires the game object it's attached to to have a puzzler.
|
---|
198 | """
|
---|
199 | def render(self, surface):
|
---|
200 | if self.game_object.puzzler.get_state():
|
---|
201 | color = pygame.color.THECOLORS['green']
|
---|
202 | else:
|
---|
203 | color = pygame.color.THECOLORS['red']
|
---|
204 |
|
---|
205 | self.game_object.get_shape().color = color
|
---|
206 | super(ShapeStateRenderer, self).render(surface)
|
---|
207 |
|
---|
208 |
|
---|
209 | def damping_velocity_func(body, gravity, damping, dt):
|
---|
210 | """Apply custom damping to this body's velocity.
|
---|
211 | """
|
---|
212 | damping = getattr(body, 'damping', damping)
|
---|
213 | return pymunk.Body.update_velocity(body, gravity, damping, dt)
|
---|
214 |
|
---|
215 |
|
---|
216 | def make_body(mass, moment, position, damping=None):
|
---|
217 | body = pymunk.Body(mass, moment)
|
---|
218 | body.position = position
|
---|
219 | if damping is not None:
|
---|
220 | body.damping = damping
|
---|
221 | body.velocity_func = damping_velocity_func
|
---|
222 | return body
|
---|
223 |
|
---|
224 |
|
---|
225 | class GameObject(object):
|
---|
226 | """A representation of a thing in the game world.
|
---|
227 |
|
---|
228 | This has a rendery thing, physicsy things and maybe some other things.
|
---|
229 | """
|
---|
230 |
|
---|
231 | def __init__(self, physicser, renderer, puzzler=None):
|
---|
232 | self.physicser = physicser
|
---|
233 | physicser.set_game_object(self)
|
---|
234 | self.physicser.add_to_space()
|
---|
235 | self.renderer = renderer
|
---|
236 | renderer.set_game_object(self)
|
---|
237 | self.puzzler = puzzler
|
---|
238 | if puzzler is not None:
|
---|
239 | puzzler.set_game_object(self)
|
---|
240 | self.zorder = ZORDER_LOW
|
---|
241 |
|
---|
242 | def get_space(self):
|
---|
243 | return self.physicser.get_space()
|
---|
244 |
|
---|
245 | def get_shape(self):
|
---|
246 | return self.physicser.get_shape()
|
---|
247 |
|
---|
248 | def get_render_position(self, surface):
|
---|
249 | return self.physicser.get_render_position(surface)
|
---|
250 |
|
---|
251 | def get_render_angle(self):
|
---|
252 | return self.physicser.get_angle()
|
---|
253 |
|
---|
254 | def render(self, surface):
|
---|
255 | return self.renderer.render(surface)
|
---|
256 |
|
---|
257 |
|
---|
258 | class FloorSwitch(GameObject):
|
---|
259 | def __init__(self, space, position):
|
---|
260 | body = pymunk.Body()
|
---|
261 | body.position = position
|
---|
262 | self.shape = pymunk.Circle(body, 30)
|
---|
263 | self.shape.collision_type = COLLISION_TYPE_SWITCH
|
---|
264 | self.shape.sensor = True
|
---|
265 | super(FloorSwitch, self).__init__(
|
---|
266 | SingleShapePhysicser(space, self.shape),
|
---|
267 | ShapeStateRenderer(),
|
---|
268 | FloorSwitchPuzzler(),
|
---|
269 | )
|
---|
270 |
|
---|
271 |
|
---|
272 | class FloorLight(GameObject):
|
---|
273 | def __init__(self, space, position, state_source):
|
---|
274 | body = pymunk.Body()
|
---|
275 | body.position = position
|
---|
276 | self.shape = pymunk.Circle(body, 10)
|
---|
277 | self.shape.collision_type = COLLISION_TYPE_SWITCH
|
---|
278 | self.shape.sensor = True
|
---|
279 | super(FloorLight, self).__init__(
|
---|
280 | SingleShapePhysicser(space, self.shape),
|
---|
281 | ShapeStateRenderer(),
|
---|
282 | StateProxyPuzzler(state_source),
|
---|
283 | )
|
---|
284 |
|
---|
285 |
|
---|
286 | class Box(GameObject):
|
---|
287 | def __init__(self, space, position):
|
---|
288 | body = make_body(10, 10000, position, damping=0.5)
|
---|
289 | self.shape = pymunk.Poly(
|
---|
290 | body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
|
---|
291 | self.shape.collision_type = COLLISION_TYPE_BOX
|
---|
292 | super(Box, self).__init__(
|
---|
293 | SingleShapePhysicser(space, self.shape),
|
---|
294 | ShapeRenderer(),
|
---|
295 | )
|
---|