source: nagslang/game_object.py@ 118:c02a99502a90

Last change on this file since 118:c02a99502a90 was 107:b90d01e4d9d4, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Layered drawing.

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