1 | import pymunk
|
---|
2 | import pymunk.pygame_util
|
---|
3 |
|
---|
4 | from nagslang import puzzle
|
---|
5 | from nagslang import render
|
---|
6 | from nagslang.constants import (
|
---|
7 | SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW,
|
---|
8 | ZORDER_FLOOR, COLLISION_TYPE_DOOR)
|
---|
9 | from nagslang.resources import resources
|
---|
10 | from nagslang.events import DoorEvent
|
---|
11 | from nagslang.widgets.text import LabelWidget
|
---|
12 |
|
---|
13 |
|
---|
14 | # For levels to import, until we get module names in 'classname'
|
---|
15 | StateProxyPuzzler = puzzle.StateProxyPuzzler
|
---|
16 | StateLogicalAndPuzzler = puzzle.StateLogicalAndPuzzler
|
---|
17 | CollidePuzzler = puzzle.CollidePuzzler
|
---|
18 |
|
---|
19 |
|
---|
20 | class Physicser(object):
|
---|
21 | def __init__(self, space):
|
---|
22 | self._space = space
|
---|
23 |
|
---|
24 | def get_space(self):
|
---|
25 | return self._space
|
---|
26 |
|
---|
27 | def set_game_object(self, game_object):
|
---|
28 | self.game_object = game_object
|
---|
29 |
|
---|
30 | def get_shape(self):
|
---|
31 | raise NotImplementedError()
|
---|
32 |
|
---|
33 | def add_to_space(self):
|
---|
34 | raise NotImplementedError()
|
---|
35 |
|
---|
36 | def remove_from_space(self):
|
---|
37 | raise NotImplementedError()
|
---|
38 |
|
---|
39 | def get_render_position(self, surface):
|
---|
40 | raise NotImplementedError()
|
---|
41 |
|
---|
42 | def get_angle(self):
|
---|
43 | raise NotImplementedError()
|
---|
44 |
|
---|
45 | def apply_impulse(self, j, r=(0, 0)):
|
---|
46 | raise NotImplementedError()
|
---|
47 |
|
---|
48 |
|
---|
49 | class SingleShapePhysicser(Physicser):
|
---|
50 | def __init__(self, space, shape):
|
---|
51 | super(SingleShapePhysicser, self).__init__(space)
|
---|
52 | self._shape = shape
|
---|
53 | shape.physicser = self
|
---|
54 |
|
---|
55 | def get_shape(self):
|
---|
56 | return self._shape
|
---|
57 |
|
---|
58 | def add_to_space(self):
|
---|
59 | self.get_space().add(self._shape)
|
---|
60 | if not self._shape.body.is_static:
|
---|
61 | self.get_space().add(self._shape.body)
|
---|
62 |
|
---|
63 | def remove_from_space(self):
|
---|
64 | self.get_space().remove(self._shape)
|
---|
65 | if not self._shape.body.is_static:
|
---|
66 | self.get_space().remove(self._shape.body)
|
---|
67 |
|
---|
68 | def get_render_position(self, surface):
|
---|
69 | pos = self._shape.body.position
|
---|
70 | return pymunk.pygame_util.to_pygame(pos, surface)
|
---|
71 |
|
---|
72 | def get_angle(self):
|
---|
73 | return self._shape.body.angle
|
---|
74 |
|
---|
75 | def apply_impulse(self, j, r=(0, 0)):
|
---|
76 | return self._shape.body.apply_impulse(j, r)
|
---|
77 |
|
---|
78 |
|
---|
79 | def damping_velocity_func(body, gravity, damping, dt):
|
---|
80 | """Apply custom damping to this body's velocity.
|
---|
81 | """
|
---|
82 | damping = getattr(body, 'damping', damping)
|
---|
83 | return pymunk.Body.update_velocity(body, gravity, damping, dt)
|
---|
84 |
|
---|
85 |
|
---|
86 | def make_body(mass, moment, position, damping=None):
|
---|
87 | body = pymunk.Body(mass, moment)
|
---|
88 | body.position = tuple(position)
|
---|
89 | if damping is not None:
|
---|
90 | body.damping = damping
|
---|
91 | body.velocity_func = damping_velocity_func
|
---|
92 | return body
|
---|
93 |
|
---|
94 |
|
---|
95 | class Overlay(object):
|
---|
96 | def set_game_object(self, game_object):
|
---|
97 | self.game_object = game_object
|
---|
98 |
|
---|
99 | def render(self, surface):
|
---|
100 | pass
|
---|
101 |
|
---|
102 | def is_visible(self):
|
---|
103 | return self.game_object.puzzler.get_state()
|
---|
104 |
|
---|
105 |
|
---|
106 | class TextOverlay(Overlay):
|
---|
107 | def __init__(self, text):
|
---|
108 | self.text = text
|
---|
109 | self.widget = LabelWidget((20, 20), self.text)
|
---|
110 |
|
---|
111 | def render(self, surface):
|
---|
112 | self.widget.draw(surface)
|
---|
113 |
|
---|
114 |
|
---|
115 | class GameObject(object):
|
---|
116 | """A representation of a thing in the game world.
|
---|
117 |
|
---|
118 | This has a rendery thing, physicsy things and maybe some other things.
|
---|
119 | """
|
---|
120 |
|
---|
121 | zorder = ZORDER_LOW
|
---|
122 |
|
---|
123 | def __init__(self, physicser, renderer, puzzler=None, overlay=None):
|
---|
124 | self.physicser = physicser
|
---|
125 | physicser.set_game_object(self)
|
---|
126 | self.physicser.add_to_space()
|
---|
127 | self.renderer = renderer
|
---|
128 | renderer.set_game_object(self)
|
---|
129 | self.puzzler = puzzler
|
---|
130 | if puzzler is not None:
|
---|
131 | puzzler.set_game_object(self)
|
---|
132 | self.overlay = overlay
|
---|
133 | if overlay is not None:
|
---|
134 | self.overlay.set_game_object(self)
|
---|
135 |
|
---|
136 | def get_space(self):
|
---|
137 | return self.physicser.get_space()
|
---|
138 |
|
---|
139 | def get_shape(self):
|
---|
140 | return self.physicser.get_shape()
|
---|
141 |
|
---|
142 | def get_render_position(self, surface):
|
---|
143 | return self.physicser.get_render_position(surface)
|
---|
144 |
|
---|
145 | def get_render_angle(self):
|
---|
146 | return self.physicser.get_angle()
|
---|
147 |
|
---|
148 | def render(self, surface):
|
---|
149 | return self.renderer.render(surface)
|
---|
150 |
|
---|
151 | def animate(self):
|
---|
152 | self.renderer.animate()
|
---|
153 |
|
---|
154 | def collide_with_protagonist(self):
|
---|
155 | """Called as a `pre_solve` collision callback with the protagonist.
|
---|
156 |
|
---|
157 | You can return `False` to ignore the collision, anything else
|
---|
158 | (including `None`) to process the collision as normal.
|
---|
159 | """
|
---|
160 | return True
|
---|
161 |
|
---|
162 |
|
---|
163 | class FloorSwitch(GameObject):
|
---|
164 | zorder = ZORDER_FLOOR
|
---|
165 |
|
---|
166 | def __init__(self, space, position):
|
---|
167 | body = make_body(None, None, position)
|
---|
168 | self.shape = pymunk.Circle(body, 30)
|
---|
169 | self.shape.collision_type = COLLISION_TYPE_SWITCH
|
---|
170 | self.shape.sensor = True
|
---|
171 | super(FloorSwitch, self).__init__(
|
---|
172 | SingleShapePhysicser(space, self.shape),
|
---|
173 | render.ImageStateRenderer({
|
---|
174 | True: resources.get_image('objects', 'sensor_on.png'),
|
---|
175 | False: resources.get_image('objects', 'sensor_off.png'),
|
---|
176 | }),
|
---|
177 | puzzle.CollidePuzzler(*SWITCH_PUSHERS),
|
---|
178 | )
|
---|
179 |
|
---|
180 |
|
---|
181 | class Note(GameObject):
|
---|
182 | zorder = ZORDER_FLOOR
|
---|
183 |
|
---|
184 | def __init__(self, space, position, message):
|
---|
185 | body = make_body(None, None, position)
|
---|
186 | self.shape = pymunk.Circle(body, 30)
|
---|
187 | self.shape.sensor = True
|
---|
188 | super(Note, self).__init__(
|
---|
189 | SingleShapePhysicser(space, self.shape),
|
---|
190 | render.ImageRenderer(resources.get_image('objects', 'note.png')),
|
---|
191 | puzzle.CollidePuzzler(),
|
---|
192 | TextOverlay(message),
|
---|
193 | )
|
---|
194 |
|
---|
195 |
|
---|
196 | class FloorLight(GameObject):
|
---|
197 | zorder = ZORDER_FLOOR
|
---|
198 |
|
---|
199 | def __init__(self, space, position, state_source):
|
---|
200 | body = make_body(None, None, position)
|
---|
201 | self.shape = pymunk.Circle(body, 10)
|
---|
202 | self.shape.collision_type = COLLISION_TYPE_SWITCH
|
---|
203 | self.shape.sensor = True
|
---|
204 | super(FloorLight, self).__init__(
|
---|
205 | SingleShapePhysicser(space, self.shape),
|
---|
206 | render.ImageStateRenderer({
|
---|
207 | True: resources.get_image('objects', 'light_on.png'),
|
---|
208 | False: resources.get_image('objects', 'light_off.png'),
|
---|
209 | }),
|
---|
210 | puzzle.StateProxyPuzzler(state_source),
|
---|
211 | )
|
---|
212 |
|
---|
213 |
|
---|
214 | class Box(GameObject):
|
---|
215 | def __init__(self, space, position):
|
---|
216 | body = make_body(10, 10000, position, damping=0.5)
|
---|
217 | self.shape = pymunk.Poly(
|
---|
218 | body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
|
---|
219 | self.shape.collision_type = COLLISION_TYPE_BOX
|
---|
220 | super(Box, self).__init__(
|
---|
221 | SingleShapePhysicser(space, self.shape),
|
---|
222 | render.ImageRenderer(resources.get_image('objects', 'crate.png')),
|
---|
223 | )
|
---|
224 |
|
---|
225 |
|
---|
226 | class Door(GameObject):
|
---|
227 | zorder = ZORDER_FLOOR
|
---|
228 |
|
---|
229 | def __init__(self, space, position, destination, dest_pos, key_state=None):
|
---|
230 | body = make_body(pymunk.inf, pymunk.inf, position, damping=0.5)
|
---|
231 | self.shape = pymunk.Poly(
|
---|
232 | body, [(-32, -32), (32, -32), (32, 32), (-32, 32)])
|
---|
233 | self.shape.collision_type = COLLISION_TYPE_DOOR
|
---|
234 | self.shape.sensor = True
|
---|
235 | self.destination = destination
|
---|
236 | self.dest_pos = tuple(dest_pos)
|
---|
237 | if key_state is None:
|
---|
238 | puzzler = puzzle.YesPuzzler()
|
---|
239 | else:
|
---|
240 | puzzler = puzzle.StateProxyPuzzler(key_state)
|
---|
241 | super(Door, self).__init__(
|
---|
242 | SingleShapePhysicser(space, self.shape),
|
---|
243 | render.ImageRenderer(resources.get_image('objects', 'door.png')),
|
---|
244 | puzzler,
|
---|
245 | )
|
---|
246 |
|
---|
247 | def collide_with_protagonist(self):
|
---|
248 | if self.puzzler.get_state():
|
---|
249 | DoorEvent.post(self.destination, self.dest_pos)
|
---|