source: nagslang/game_object.py@ 128:fbb073720bac

Last change on this file since 128:fbb073720bac was 126:c3af35561494, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Cleaner switch/light rendering.

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