Ignore:
Timestamp:
09/06/13 13:14:27 (8 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Phase:
public
Message:

Save door and lever state.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r335 r346  
    128128        self.remove = False  # If true, will be removed from drawables
    129129
     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
    130140    def get_space(self):
    131141        return self.physicser.get_space()
     
    271281        self.destination = destination
    272282        self.dest_pos = tuple(dest_pos)
    273         puzzler = None
    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)
     283        self._key_state = key_state
    278284        super(Door, self).__init__(
    279285            SingleShapePhysicser(space, self.shape),
    280             render.ImageRenderer(resources.get_image('objects', 'door.png')),
    281             puzzler,
    282             interactible=environment.Interactible(action),
    283         )
     286            render.ImageStateRenderer({
     287                True: resources.get_image('objects', 'door.png'),
     288                # TODO: Locked door image.
     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']
    284301
    285302    def _post_door_event(self, protagonist):
    286303        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)
    287320
    288321    @classmethod
     
    332365        self.shape = pymunk.Circle(body, 20)
    333366        self.shape.sensor = True
    334         self.toggle_on = False
    335367        super(ToggleSwitch, self).__init__(
    336368            SingleShapePhysicser(space, self.shape),
     
    345377        )
    346378
     379    @property
     380    def toggle_on(self):
     381        return self._stored_state['toggle_on']
     382
    347383    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
    349391
    350392    @classmethod
Note: See TracChangeset for help on using the changeset viewer.