Changeset 359:d42752ab3231


Ignore:
Timestamp:
Sep 6, 2013, 5:42:01 PM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
rebase_source:
33176d08ea44cccf5370f46787299141d9880e7e
Message:

Refactor doors and add horrible temporoary image for someone to fix later.

Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • data/levels/level1

    r346 r359  
    6060  - 0
    6161  - door_switch
    62   classname: Door
     62  classname: PuzzleDoor
    6363  name: switch_door
    6464lines:
  • nagslang/environment.py

    r281 r359  
    77        """
    88        raise NotImplementedError()
     9
     10
     11class YesCondition(ProtagonistCondition):
     12    """Condition that is always met.
     13    """
     14    def check(self, protagonist):
     15        return True
    916
    1017
  • nagslang/game_object.py

    r357 r359  
    269269
    270270
    271 class Door(GameObject):
     271class BaseDoor(GameObject):
    272272    zorder = ZORDER_FLOOR
     273    is_open = True
    273274
    274275    def __init__(self, space, position, destination, dest_pos, angle,
    275                  key_state=None):
     276                 renderer, condition):
    276277        body = make_body(pymunk.inf, pymunk.inf, position, damping=0.5)
    277278        self.shape = pymunk.Circle(body, 30)
     
    281282        self.destination = destination
    282283        self.dest_pos = tuple(dest_pos)
     284        super(BaseDoor, self).__init__(
     285            SingleShapePhysicser(space, self.shape),
     286            renderer,
     287            puzzle.ParentAttrPuzzler('is_open'),
     288            interactible=environment.Interactible(
     289                environment.Action(self._post_door_event, condition)),
     290        )
     291
     292    def _post_door_event(self, protagonist):
     293        DoorEvent.post(self.destination, self.dest_pos)
     294
     295
     296class Door(BaseDoor):
     297    def __init__(self, space, position, destination, dest_pos, angle):
     298        super(Door, self).__init__(
     299            space, position, destination, dest_pos, angle,
     300            render.ImageRenderer(resources.get_image('objects', 'door.png')),
     301            environment.YesCondition(),
     302        )
     303
     304    @classmethod
     305    def requires(cls):
     306        return [("name", "string"), ("position", "coordinates"),
     307                ("destination", "level name"), ("dest_pos", "coordinate"),
     308                ("angle", "degrees")]
     309
     310
     311class PuzzleDoor(BaseDoor):
     312    def __init__(self, space, position, destination, dest_pos, angle,
     313                 key_state):
    283314        self._key_state = key_state
    284         super(Door, self).__init__(
    285             SingleShapePhysicser(space, self.shape),
     315        super(PuzzleDoor, self).__init__(
     316            space, position, destination, dest_pos, angle,
    286317            render.ImageStateRenderer({
    287318                True: resources.get_image('objects', 'door.png'),
    288                 # TODO: Locked door image.
    289                 False: resources.get_image('objects', 'door.png'),
     319                False: resources.get_image('objects', 'locked_door.png'),
    290320            }),
    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))),
     321            environment.FunctionCondition(lambda p: self.is_open),
    296322        )
    297323
     
    300326        return self._stored_state['is_open']
    301327
    302     def _post_door_event(self, protagonist):
    303         DoorEvent.post(self.destination, self.dest_pos)
    304 
    305328    def set_stored_state_dict(self, stored_state):
    306329        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
     330        self._stored_state.setdefault('is_open', False)
     331        return True
    314332
    315333    def update(self, dt):
     
    317335            self._stored_state['is_open'] = self.puzzler.glue.get_state_of(
    318336                self._key_state)
    319         super(Door, self).update(dt)
     337        super(PuzzleDoor, self).update(dt)
    320338
    321339    @classmethod
     
    324342                ("destination", "level name"), ("dest_pos", "coordinate"),
    325343                ("angle", "degrees"),
    326                 ("key_state", "puzzler (optional)")]
     344                ("key_state", "puzzler")]
     345
     346
     347class KeyedDoor(BaseDoor):
     348    def __init__(self, space, position, destination, dest_pos, angle,
     349                 key_item=None):
     350        self._key_item = key_item
     351        super(KeyedDoor, self).__init__(
     352            space, position, destination, dest_pos, angle,
     353            render.ImageRenderer(resources.get_image('objects', 'door.png')),
     354            environment.ItemRequiredCondition(key_item),
     355        )
     356
     357    @classmethod
     358    def requires(cls):
     359        return [("name", "string"), ("position", "coordinates"),
     360                ("destination", "level name"), ("dest_pos", "coordinate"),
     361                ("angle", "degrees"), ("key_item", "item name")]
    327362
    328363
Note: See TracChangeset for help on using the changeset viewer.