comparison nagslang/game_object.py @ 346:282113d86d75

Save door and lever state.
author Jeremy Thurgood <firxen@gmail.com>
date Fri, 06 Sep 2013 15:14:27 +0200
parents 78b805549b4e
children 50fce787ae17
comparison
equal deleted inserted replaced
345:4708e86a9a3c 346:282113d86d75
125 self.interactible = interactible 125 self.interactible = interactible
126 if interactible is not None: 126 if interactible is not None:
127 self.interactible.set_game_object(self) 127 self.interactible.set_game_object(self)
128 self.remove = False # If true, will be removed from drawables 128 self.remove = False # If true, will be removed from drawables
129 129
130 def set_stored_state_dict(self, stored_state):
131 """Override this to set up whatever state storage you want.
132
133 The `stored_state` dict passed in contains whatever saved state we
134 might have for this object. If the return value of this method
135 evaluates to `True`, the contents of the `stored_state` dict will be
136 saved, otherwise it will be discarded.
137 """
138 pass
139
130 def get_space(self): 140 def get_space(self):
131 return self.physicser.get_space() 141 return self.physicser.get_space()
132 142
133 def get_shape(self): 143 def get_shape(self):
134 return self.physicser.get_shape() 144 return self.physicser.get_shape()
268 self.shape.collision_type = COLLISION_TYPE_DOOR 278 self.shape.collision_type = COLLISION_TYPE_DOOR
269 self.shape.body.angle = float(angle) / 180 * math.pi 279 self.shape.body.angle = float(angle) / 180 * math.pi
270 self.shape.sensor = True 280 self.shape.sensor = True
271 self.destination = destination 281 self.destination = destination
272 self.dest_pos = tuple(dest_pos) 282 self.dest_pos = tuple(dest_pos)
273 puzzler = None 283 self._key_state = key_state
274 action = environment.Action(self._post_door_event)
275 if key_state is not None:
276 puzzler = puzzle.StateProxyPuzzler(key_state)
277 action.condition = environment.PuzzleStateCondition(puzzler)
278 super(Door, self).__init__( 284 super(Door, self).__init__(
279 SingleShapePhysicser(space, self.shape), 285 SingleShapePhysicser(space, self.shape),
280 render.ImageRenderer(resources.get_image('objects', 'door.png')), 286 render.ImageStateRenderer({
281 puzzler, 287 True: resources.get_image('objects', 'door.png'),
282 interactible=environment.Interactible(action), 288 # TODO: Locked door image.
283 ) 289 False: resources.get_image('objects', 'door.png'),
290 }),
291 puzzle.ParentAttrPuzzler('is_open'),
292 interactible=environment.Interactible(
293 environment.Action(
294 self._post_door_event,
295 environment.FunctionCondition(lambda p: self.is_open))),
296 )
297
298 @property
299 def is_open(self):
300 return self._stored_state['is_open']
284 301
285 def _post_door_event(self, protagonist): 302 def _post_door_event(self, protagonist):
286 DoorEvent.post(self.destination, self.dest_pos) 303 DoorEvent.post(self.destination, self.dest_pos)
304
305 def set_stored_state_dict(self, stored_state):
306 self._stored_state = stored_state
307 if self._key_state is not None:
308 # We're lockable, so we start locked and want to save our state.
309 self._stored_state.setdefault('is_open', False)
310 return True
311 # Not lockable, so we're always open and don't bother saving state.
312 self._stored_state['is_open'] = True
313 return False
314
315 def update(self, dt):
316 if not self.is_open:
317 self._stored_state['is_open'] = self.puzzler.glue.get_state_of(
318 self._key_state)
319 super(Door, self).update(dt)
287 320
288 @classmethod 321 @classmethod
289 def requires(cls): 322 def requires(cls):
290 return [("name", "string"), ("position", "coordinates"), 323 return [("name", "string"), ("position", "coordinates"),
291 ("destination", "level name"), ("dest_pos", "coordinate"), 324 ("destination", "level name"), ("dest_pos", "coordinate"),
329 362
330 def __init__(self, space, position): 363 def __init__(self, space, position):
331 body = make_body(None, None, position) 364 body = make_body(None, None, position)
332 self.shape = pymunk.Circle(body, 20) 365 self.shape = pymunk.Circle(body, 20)
333 self.shape.sensor = True 366 self.shape.sensor = True
334 self.toggle_on = False
335 super(ToggleSwitch, self).__init__( 367 super(ToggleSwitch, self).__init__(
336 SingleShapePhysicser(space, self.shape), 368 SingleShapePhysicser(space, self.shape),
337 render.ImageStateRenderer({ 369 render.ImageStateRenderer({
338 True: resources.get_image('objects', 'lever.png'), 370 True: resources.get_image('objects', 'lever.png'),
339 False: resources.get_image( 371 False: resources.get_image(
342 puzzle.ParentAttrPuzzler('toggle_on'), 374 puzzle.ParentAttrPuzzler('toggle_on'),
343 interactible=environment.Interactible( 375 interactible=environment.Interactible(
344 environment.Action(self._toggle)), 376 environment.Action(self._toggle)),
345 ) 377 )
346 378
379 @property
380 def toggle_on(self):
381 return self._stored_state['toggle_on']
382
347 def _toggle(self, protagonist): 383 def _toggle(self, protagonist):
348 self.toggle_on = not self.toggle_on 384 self._stored_state['toggle_on'] = not self.toggle_on
385
386 def set_stored_state_dict(self, stored_state):
387 self._stored_state = stored_state
388 # We start in the "off" position.
389 self._stored_state.setdefault('toggle_on', False)
390 return True
349 391
350 @classmethod 392 @classmethod
351 def requires(cls): 393 def requires(cls):
352 return [("name", "string"), ("position", "coordinates")] 394 return [("name", "string"), ("position", "coordinates")]
353 395