source: nagslang/game_object.py@ 125:12be9632fa15

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

Rearrange game objects a bit.

File size: 7.4 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 ShapeRenderer(Renderer):
178 def render(self, surface):
179 self._render_shape(surface)
180 super(ShapeRenderer, self).render(surface)
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
189 def __init__(self, physicser, renderer, puzzler=None):
190 self.physicser = physicser
191 physicser.set_game_object(self)
192 self.physicser.add_to_space()
193 self.renderer = renderer
194 renderer.set_game_object(self)
195 self.puzzler = puzzler
196 if puzzler is not None:
197 puzzler.set_game_object(self)
198 self.zorder = ZORDER_LOW
199
200 def get_space(self):
201 return self.physicser.get_space()
202
203 def get_shape(self):
204 return self.physicser.get_shape()
205
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()
211
212 def render(self, surface):
213 return self.renderer.render(surface)
214
215
216class FloorSwitch(GameObject):
217 def __init__(self, space, position):
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__(
224 SingleShapePhysicser(space, self.shape),
225 ShapeRenderer(),
226 FloorSwitchPuzzler(),
227 )
228
229 def render(self, surface):
230 if self.puzzler.get_state():
231 self.shape.color = pygame.color.THECOLORS['green']
232 else:
233 self.shape.color = pygame.color.THECOLORS['red']
234 super(FloorSwitch, self).render(surface)
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),
246 ShapeRenderer(),
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.