source: nagslang/game_object.py@ 264:c1d862334e3d

Last change on this file since 264:c1d862334e3d was 264:c1d862334e3d, checked in by Neil Muller <drnlmuller@…>, 8 years ago

PEP8

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