source: nagslang/game_object.py@ 226:e89a43d208b9

Last change on this file since 226:e89a43d208b9 was 224:b6db213e53a2, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Bulkheads are bits of wall you can walk through.

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