source: nagslang/game_object.py@ 142:cd77974b2a87

Last change on this file since 142:cd77974b2a87 was 140:f36a7075d9a0, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Two switch puzzle!

File size: 8.6 KB
RevLine 
[63]1import math
[62]2
[81]3import pygame
4import pymunk
[93]5import pymunk.pygame_util
[81]6
[107]7from nagslang.constants import (
[133]8 SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW)
[104]9from nagslang.options import options
[81]10
[82]11
[106]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
[81]26class Puzzler(object):
[106]27 """Behaviour specific to a puzzle component.
28 """
29 def set_glue(self, glue):
30 self.glue = glue
31
[123]32 def set_game_object(self, game_object):
33 self.game_object = game_object
34
[106]35 def get_state(self):
[81]36 raise NotImplementedError()
37
38
39class FloorSwitchPuzzler(Puzzler):
[93]40 def get_state(self):
[123]41 space = self.game_object.get_space()
42 for shape in space.shape_query(self.game_object.get_shape()):
[81]43 if shape.collision_type in SWITCH_PUSHERS:
44 return True
45 return False
46
[59]47
[106]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
[140]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
[59]67class Physicser(object):
[93]68 def __init__(self, space):
[123]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()
[93]79
80 def add_to_space(self):
[59]81 raise NotImplementedError()
82
[93]83 def remove_from_space(self):
[59]84 raise NotImplementedError()
85
[93]86 def get_render_position(self, surface):
[63]87 raise NotImplementedError()
88
[93]89 def get_angle(self):
90 raise NotImplementedError()
91
92 def apply_impulse(self, j, r=(0, 0)):
[59]93 raise NotImplementedError()
94
95
96class SingleShapePhysicser(Physicser):
[93]97 def __init__(self, space, shape):
98 super(SingleShapePhysicser, self).__init__(space)
[59]99 self._shape = shape
100
[123]101 def get_shape(self):
102 return self._shape
103
[93]104 def add_to_space(self):
[123]105 self.get_space().add(self._shape)
[93]106 if not self._shape.body.is_static:
[123]107 self.get_space().add(self._shape.body)
[59]108
[93]109 def remove_from_space(self):
[123]110 self.get_space().remove(self._shape)
[93]111 if not self._shape.body.is_static:
[123]112 self.get_space().remove(self._shape.body)
[59]113
[93]114 def get_render_position(self, surface):
[59]115 pos = self._shape.body.position
116 return pymunk.pygame_util.to_pygame(pos, surface)
[63]117
[93]118 def get_angle(self):
[63]119 return self._shape.body.angle
[59]120
[93]121 def apply_impulse(self, j, r=(0, 0)):
122 return self._shape.body.apply_impulse(j, r)
123
[59]124
125class Renderer(object):
[123]126 def set_game_object(self, game_object):
127 self.game_object = game_object
[104]128
[123]129 def _render_shape(self, surface):
130 shape = self.game_object.get_shape()
[104]131 # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
[123]132 color = getattr(shape, 'color', pygame.color.THECOLORS['lightblue'])
[104]133 # We only explicitly draw Circle and Poly shapes. Everything else we
134 # forward to pymunk.
[123]135 if isinstance(shape, pymunk.Circle):
136 centre = pymunk.pygame_util.to_pygame(shape.body.position, surface)
137 radius = int(shape.radius)
[104]138 pygame.draw.circle(surface, color, centre, radius, 2)
[123]139 elif isinstance(shape, pymunk.Poly):
[104]140 # polygon bounding box
141 points = [pymunk.pygame_util.to_pygame(p, surface)
[123]142 for p in shape.get_vertices()]
[104]143 pygame.draw.lines(surface, color, True, points, 2)
144 else:
[123]145 pymunk.pygame_util.draw(surface, shape)
[104]146
[123]147 def render(self, surface):
148 pos = self.game_object.get_render_position(surface)
149 angle = self.game_object.get_render_angle()
[104]150 if options.debug:
151 self._render_shape(surface, pos, angle)
[59]152
153
[63]154def image_pos(image, pos):
155 return (pos[0] - image.get_width() / 2,
156 pos[1] - image.get_height() / 2)
157
158
[59]159class ImageRenderer(Renderer):
[123]160 def __init__(self, image):
[59]161 self._image = image
162
[123]163 def render(self, surface):
164 pos = self.game_object.get_render_position(surface)
[63]165 surface.blit(self._image, image_pos(self._image, pos))
[123]166 super(ImageRenderer, self).render(surface)
[63]167
168
169class FacingImageRenderer(Renderer):
[123]170 def __init__(self, left_image, right_image):
[63]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
[123]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())
[63]184 surface.blit(image, image_pos(image, pos))
[123]185 super(FacingImageRenderer, self).render(surface)
[59]186
187
[133]188class ShapeRenderer(Renderer):
189 def render(self, surface):
190 self._render_shape(surface)
191 super(ShapeRenderer, self).render(surface)
192
193
194class ShapeStateRenderer(ShapeRenderer):
[126]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 """
[123]199 def render(self, surface):
[126]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)
[59]207
208
[133]209def 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
216def 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
[59]225class 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
[93]231 def __init__(self, physicser, renderer, puzzler=None):
232 self.physicser = physicser
[123]233 physicser.set_game_object(self)
[93]234 self.physicser.add_to_space()
[59]235 self.renderer = renderer
[123]236 renderer.set_game_object(self)
[81]237 self.puzzler = puzzler
[123]238 if puzzler is not None:
239 puzzler.set_game_object(self)
[107]240 self.zorder = ZORDER_LOW
[59]241
[123]242 def get_space(self):
243 return self.physicser.get_space()
244
245 def get_shape(self):
246 return self.physicser.get_shape()
247
[93]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()
[59]253
254 def render(self, surface):
[123]255 return self.renderer.render(surface)
[81]256
257
258class FloorSwitch(GameObject):
[93]259 def __init__(self, space, position):
[81]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__(
[93]266 SingleShapePhysicser(space, self.shape),
[126]267 ShapeStateRenderer(),
[123]268 FloorSwitchPuzzler(),
[81]269 )
270
[106]271
272class 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),
[126]281 ShapeStateRenderer(),
[106]282 StateProxyPuzzler(state_source),
283 )
[133]284
285
286class 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 )
Note: See TracBrowser for help on using the repository browser.