Changeset 346:282113d86d75 for nagslang


Ignore:
Timestamp:
Sep 6, 2013, 1:14:27 PM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Save door and lever state.

Location:
nagslang
Files:
4 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
  • nagslang/level.py

    r321 r346  
    3131        self.lines = []
    3232        self.world = world
     33        self.world.level_state.setdefault(name, {})
    3334        self.basetile = 'tiles/floor.png'
    3435        self._tile_image = None
     
    9495        elif issubclass(cls, go.GameObject):
    9596            gobj = cls(space, *args)
     97            level_state = self.world.level_state[self.name]
     98            stored_state = level_state.get(name, {})
     99            should_save = bool(gobj.set_stored_state_dict(stored_state))
     100            if should_save:
     101                if name is None:
     102                    raise Exception(
     103                        "Unnamed game object wants to save state:" % (gobj,))
     104                level_state[name] = stored_state
    96105            self.drawables.append(gobj)
    97106            if gobj.overlay:
  • nagslang/screens/menu.py

    r345 r346  
    4848    def play(self):
    4949        level_name, pos = self.world.level
    50         self.world.protagonist.set_position(pos)
     50        self.world.protagonist.set_position(tuple(pos))
    5151        ScreenChange.post(level_name)
    5252
  • nagslang/world.py

    r345 r346  
    2727        self.protagonist = Protagonist(pymunk.Space(), self, starting_position)
    2828        self.level = (first_level, starting_position)
     29        self.level_state = {}
    2930
    3031    def _save_location(self):
     
    4748                continue
    4849            # Hack until we save protagonist state
    49             if hasattr(value, 'update'):
     50            if hasattr(value, 'update') and not isinstance(value, dict):
    5051                continue
    5152            data[attr] = value
Note: See TracChangeset for help on using the changeset viewer.