source: nagslang/game_object.py@ 123:23b533d6f27e

Last change on this file since 123:23b533d6f27e was 123:23b533d6f27e, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Rearrange game objects a bit.

File size: 7.4 KB
RevLine 
[63]1import math
[62]2
[81]3import pygame
4import pymunk
[93]5import pymunk.pygame_util
[81]6
[107]7from nagslang.constants import (
8 SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, 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
[59]56class Physicser(object):
[93]57 def __init__(self, space):
[123]58 self._space = space
59
60 def get_space(self):
61 return self._space
62
63 def set_game_object(self, game_object):
64 self.game_object = game_object
65
66 def get_shape(self):
67 raise NotImplementedError()
[93]68
69 def add_to_space(self):
[59]70 raise NotImplementedError()
71
[93]72 def remove_from_space(self):
[59]73 raise NotImplementedError()
74
[93]75 def get_render_position(self, surface):
[63]76 raise NotImplementedError()
77
[93]78 def get_angle(self):
79 raise NotImplementedError()
80
81 def apply_impulse(self, j, r=(0, 0)):
[59]82 raise NotImplementedError()
83
84
85class SingleShapePhysicser(Physicser):
[93]86 def __init__(self, space, shape):
87 super(SingleShapePhysicser, self).__init__(space)
[59]88 self._shape = shape
89
[123]90 def get_shape(self):
91 return self._shape
92
[93]93 def add_to_space(self):
[123]94 self.get_space().add(self._shape)
[93]95 if not self._shape.body.is_static:
[123]96 self.get_space().add(self._shape.body)
[59]97
[93]98 def remove_from_space(self):
[123]99 self.get_space().remove(self._shape)
[93]100 if not self._shape.body.is_static:
[123]101 self.get_space().remove(self._shape.body)
[59]102
[93]103 def get_render_position(self, surface):
[59]104 pos = self._shape.body.position
105 return pymunk.pygame_util.to_pygame(pos, surface)
[63]106
[93]107 def get_angle(self):
[63]108 return self._shape.body.angle
[59]109
[93]110 def apply_impulse(self, j, r=(0, 0)):
111 return self._shape.body.apply_impulse(j, r)
112
[59]113
114class Renderer(object):
[123]115 def set_game_object(self, game_object):
116 self.game_object = game_object
[104]117
[123]118 def _render_shape(self, surface):
119 shape = self.game_object.get_shape()
[104]120 # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
[123]121 color = getattr(shape, 'color', pygame.color.THECOLORS['lightblue'])
[104]122 # We only explicitly draw Circle and Poly shapes. Everything else we
123 # forward to pymunk.
[123]124 if isinstance(shape, pymunk.Circle):
125 centre = pymunk.pygame_util.to_pygame(shape.body.position, surface)
126 radius = int(shape.radius)
[104]127 pygame.draw.circle(surface, color, centre, radius, 2)
[123]128 elif isinstance(shape, pymunk.Poly):
[104]129 # polygon bounding box
130 points = [pymunk.pygame_util.to_pygame(p, surface)
[123]131 for p in shape.get_vertices()]
[104]132 pygame.draw.lines(surface, color, True, points, 2)
133 else:
[123]134 pymunk.pygame_util.draw(surface, shape)
[104]135
[123]136 def render(self, surface):
137 pos = self.game_object.get_render_position(surface)
138 angle = self.game_object.get_render_angle()
[104]139 if options.debug:
140 self._render_shape(surface, pos, angle)
[59]141
142
[63]143def image_pos(image, pos):
144 return (pos[0] - image.get_width() / 2,
145 pos[1] - image.get_height() / 2)
146
147
[59]148class ImageRenderer(Renderer):
[123]149 def __init__(self, image):
[59]150 self._image = image
151
[123]152 def render(self, surface):
153 pos = self.game_object.get_render_position(surface)
[63]154 surface.blit(self._image, image_pos(self._image, pos))
[123]155 super(ImageRenderer, self).render(surface)
[63]156
157
158class FacingImageRenderer(Renderer):
[123]159 def __init__(self, left_image, right_image):
[63]160 self._images = {
161 'left': left_image,
162 'right': right_image,
163 }
164
165 def get_image(self, angle):
166 if abs(angle) < math.pi / 2:
167 return self._images['right']
168 return self._images['left']
169
[123]170 def render(self, surface):
171 pos = self.game_object.get_render_position(surface)
172 image = self.get_image(self.game_object.get_render_angle())
[63]173 surface.blit(image, image_pos(image, pos))
[123]174 super(FacingImageRenderer, self).render(surface)
[59]175
176
177class ShapeRenderer(Renderer):
[123]178 def render(self, surface):
179 self._render_shape(surface)
180 super(ShapeRenderer, self).render(surface)
[59]181
182
183class GameObject(object):
184 """A representation of a thing in the game world.
185
186 This has a rendery thing, physicsy things and maybe some other things.
187 """
188
[93]189 def __init__(self, physicser, renderer, puzzler=None):
190 self.physicser = physicser
[123]191 physicser.set_game_object(self)
[93]192 self.physicser.add_to_space()
[59]193 self.renderer = renderer
[123]194 renderer.set_game_object(self)
[81]195 self.puzzler = puzzler
[123]196 if puzzler is not None:
197 puzzler.set_game_object(self)
[107]198 self.zorder = ZORDER_LOW
[59]199
[123]200 def get_space(self):
201 return self.physicser.get_space()
202
203 def get_shape(self):
204 return self.physicser.get_shape()
205
[93]206 def get_render_position(self, surface):
207 return self.physicser.get_render_position(surface)
208
209 def get_render_angle(self):
210 return self.physicser.get_angle()
[59]211
212 def render(self, surface):
[123]213 return self.renderer.render(surface)
[81]214
215
216class FloorSwitch(GameObject):
[93]217 def __init__(self, space, position):
[81]218 body = pymunk.Body()
219 body.position = position
220 self.shape = pymunk.Circle(body, 30)
221 self.shape.collision_type = COLLISION_TYPE_SWITCH
222 self.shape.sensor = True
223 super(FloorSwitch, self).__init__(
[93]224 SingleShapePhysicser(space, self.shape),
[123]225 ShapeRenderer(),
226 FloorSwitchPuzzler(),
[81]227 )
228
229 def render(self, surface):
[93]230 if self.puzzler.get_state():
[81]231 self.shape.color = pygame.color.THECOLORS['green']
232 else:
233 self.shape.color = pygame.color.THECOLORS['red']
[82]234 super(FloorSwitch, self).render(surface)
[106]235
236
237class FloorLight(GameObject):
238 def __init__(self, space, position, state_source):
239 body = pymunk.Body()
240 body.position = position
241 self.shape = pymunk.Circle(body, 10)
242 self.shape.collision_type = COLLISION_TYPE_SWITCH
243 self.shape.sensor = True
244 super(FloorLight, self).__init__(
245 SingleShapePhysicser(space, self.shape),
[123]246 ShapeRenderer(),
[106]247 StateProxyPuzzler(state_source),
248 )
249
250 def render(self, surface):
251 if self.puzzler.get_state():
252 self.shape.color = pygame.color.THECOLORS['green']
253 else:
254 self.shape.color = pygame.color.THECOLORS['red']
255 super(FloorLight, self).render(surface)
Note: See TracBrowser for help on using the repository browser.