source: nagslang/game_object.py@ 106:bce9cd8a4a8c

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

FloorLight, linked to a FloorSwitch.

File size: 6.8 KB
Line 
1import math
2
3import pygame
4import pymunk
5import pymunk.pygame_util
6
7from nagslang.constants import SWITCH_PUSHERS, COLLISION_TYPE_SWITCH
8from nagslang.options import options
9
10
11class PuzzleGlue(object):
12 """Glue that holds bits of a puzzle together.
13 """
14 def __init__(self):
15 self._components = {}
16
17 def add_component(self, name, puzzler):
18 self._components[name] = puzzler
19 puzzler.set_glue(self)
20
21 def get_state_of(self, name):
22 return self._components[name].get_state()
23
24
25class Puzzler(object):
26 """Behaviour specific to a puzzle component.
27 """
28 def set_glue(self, glue):
29 self.glue = glue
30
31 def get_state(self):
32 raise NotImplementedError()
33
34
35class FloorSwitchPuzzler(Puzzler):
36 def __init__(self, space, shape):
37 self._space = space
38 self._shape = shape
39
40 def get_state(self):
41 for shape in self._space.shape_query(self._shape):
42 if shape.collision_type in SWITCH_PUSHERS:
43 return True
44 return False
45
46
47class StateProxyPuzzler(Puzzler):
48 def __init__(self, state_source):
49 self._state_source = state_source
50
51 def get_state(self):
52 return self.glue.get_state_of(self._state_source)
53
54
55class Physicser(object):
56 def __init__(self, space):
57 self.space = space
58
59 def add_to_space(self):
60 raise NotImplementedError()
61
62 def remove_from_space(self):
63 raise NotImplementedError()
64
65 def get_render_position(self, surface):
66 raise NotImplementedError()
67
68 def get_angle(self):
69 raise NotImplementedError()
70
71 def apply_impulse(self, j, r=(0, 0)):
72 raise NotImplementedError()
73
74
75class SingleShapePhysicser(Physicser):
76 def __init__(self, space, shape):
77 super(SingleShapePhysicser, self).__init__(space)
78 self._shape = shape
79
80 def add_to_space(self):
81 self.space.add(self._shape)
82 if not self._shape.body.is_static:
83 self.space.add(self._shape.body)
84
85 def remove_from_space(self):
86 self.space.remove(self._shape)
87 if not self._shape.body.is_static:
88 self.space.remove(self._shape.body)
89
90 def get_render_position(self, surface):
91 pos = self._shape.body.position
92 return pymunk.pygame_util.to_pygame(pos, surface)
93
94 def get_angle(self):
95 return self._shape.body.angle
96
97 def apply_impulse(self, j, r=(0, 0)):
98 return self._shape.body.apply_impulse(j, r)
99
100
101class Renderer(object):
102 def __init__(self, shape):
103 self._shape = shape
104
105 def _render_shape(self, surface, pos, angle):
106 # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
107 color = getattr(
108 self._shape, 'color', pygame.color.THECOLORS['lightblue'])
109 # We only explicitly draw Circle and Poly shapes. Everything else we
110 # forward to pymunk.
111 if isinstance(self._shape, pymunk.Circle):
112 centre = pymunk.pygame_util.to_pygame(
113 self._shape.body.position, surface)
114 radius = int(self._shape.radius)
115 pygame.draw.circle(surface, color, centre, radius, 2)
116 elif isinstance(self._shape, pymunk.Poly):
117 # polygon bounding box
118 points = [pymunk.pygame_util.to_pygame(p, surface)
119 for p in self._shape.get_vertices()]
120 pygame.draw.lines(surface, color, True, points, 2)
121 else:
122 pymunk.pygame_util.draw(surface, self._shape)
123
124 def render(self, surface, pos, angle):
125 if options.debug:
126 self._render_shape(surface, pos, angle)
127
128
129def image_pos(image, pos):
130 return (pos[0] - image.get_width() / 2,
131 pos[1] - image.get_height() / 2)
132
133
134class ImageRenderer(Renderer):
135 def __init__(self, shape, image):
136 super(ImageRenderer, self).__init__(shape)
137 self._image = image
138
139 def render(self, surface, pos, angle):
140 surface.blit(self._image, image_pos(self._image, pos))
141 super(ImageRenderer, self).render(surface, pos, angle)
142
143
144class FacingImageRenderer(Renderer):
145 def __init__(self, shape, left_image, right_image):
146 super(FacingImageRenderer, self).__init__(shape)
147 self._images = {
148 'left': left_image,
149 'right': right_image,
150 }
151
152 def get_image(self, angle):
153 if abs(angle) < math.pi / 2:
154 return self._images['right']
155 return self._images['left']
156
157 def render(self, surface, pos, angle):
158 image = self.get_image(angle)
159 surface.blit(image, image_pos(image, pos))
160 super(FacingImageRenderer, self).render(surface, pos, angle)
161
162
163class ShapeRenderer(Renderer):
164 def render(self, surface, pos, angle):
165 self._render_shape(surface, pos, angle)
166 super(ShapeRenderer, self).render(surface, pos, angle)
167
168
169class GameObject(object):
170 """A representation of a thing in the game world.
171
172 This has a rendery thing, physicsy things and maybe some other things.
173 """
174
175 def __init__(self, physicser, renderer, puzzler=None):
176 self.physicser = physicser
177 self.physicser.add_to_space()
178 self.renderer = renderer
179 self.puzzler = puzzler
180
181 def get_render_position(self, surface):
182 return self.physicser.get_render_position(surface)
183
184 def get_render_angle(self):
185 return self.physicser.get_angle()
186
187 def render(self, surface):
188 return self.renderer.render(
189 surface, self.get_render_position(surface),
190 self.get_render_angle())
191
192
193class FloorSwitch(GameObject):
194 def __init__(self, space, position):
195 body = pymunk.Body()
196 body.position = position
197 self.shape = pymunk.Circle(body, 30)
198 self.shape.collision_type = COLLISION_TYPE_SWITCH
199 self.shape.sensor = True
200 super(FloorSwitch, self).__init__(
201 SingleShapePhysicser(space, self.shape),
202 ShapeRenderer(self.shape),
203 FloorSwitchPuzzler(space, self.shape),
204 )
205
206 def render(self, surface):
207 if self.puzzler.get_state():
208 self.shape.color = pygame.color.THECOLORS['green']
209 else:
210 self.shape.color = pygame.color.THECOLORS['red']
211 super(FloorSwitch, self).render(surface)
212
213
214class FloorLight(GameObject):
215 def __init__(self, space, position, state_source):
216 body = pymunk.Body()
217 body.position = position
218 self.shape = pymunk.Circle(body, 10)
219 self.shape.collision_type = COLLISION_TYPE_SWITCH
220 self.shape.sensor = True
221 super(FloorLight, self).__init__(
222 SingleShapePhysicser(space, self.shape),
223 ShapeRenderer(self.shape),
224 StateProxyPuzzler(state_source),
225 )
226
227 def render(self, surface):
228 if self.puzzler.get_state():
229 self.shape.color = pygame.color.THECOLORS['green']
230 else:
231 self.shape.color = pygame.color.THECOLORS['red']
232 super(FloorLight, self).render(surface)
Note: See TracBrowser for help on using the repository browser.