Ignore:
Timestamp:
09/03/13 21:27:25 (8 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
Phase:
public
Rebase:
63623463393338663838326136643963333235663133626461393565376633313739626334343735
Message:

Break puzzlers out of game_object.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r196 r201  
    55import pymunk.pygame_util
    66
     7from nagslang import puzzle
    78from nagslang.constants import (
    89    SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW,
    9     ZORDER_FLOOR, COLLISION_TYPE_DOOR, COLLISION_TYPE_PLAYER)
     10    ZORDER_FLOOR, COLLISION_TYPE_DOOR)
    1011from nagslang.options import options
    1112from nagslang.resources import resources
     
    1415
    1516
    16 class PuzzleGlue(object):
    17     """Glue that holds bits of a puzzle together.
    18     """
    19     def __init__(self):
    20         self._components = {}
    21 
    22     def add_component(self, name, puzzler):
    23         if not isinstance(puzzler, Puzzler):
    24             puzzler = puzzler.puzzler
    25         self._components[name] = puzzler
    26         puzzler.set_glue(self)
    27 
    28     def get_state_of(self, name):
    29         return self._components[name].get_state()
    30 
    31 
    32 class Puzzler(object):
    33     """Behaviour specific to a puzzle component.
    34     """
    35     def set_glue(self, glue):
    36         self.glue = glue
    37 
    38     def set_game_object(self, game_object):
    39         self.game_object = game_object
    40 
    41     def get_state(self):
    42         raise NotImplementedError()
    43 
    44 
    45 class YesPuzzler(Puzzler):
    46     """Yes sir, I'm always on.
    47     """
    48     def get_state(self):
    49         return True
    50 
    51 
    52 class NoPuzzler(Puzzler):
    53     """No sir, I'm always off.
    54     """
    55     def get_state(self):
    56         return False
    57 
    58 
    59 class CollidePuzzler(Puzzler):
    60     def __init__(self, *collision_types):
    61         if not collision_types:
    62             collision_types = (COLLISION_TYPE_PLAYER,)
    63         self._collision_types = collision_types
    64 
    65     def get_state(self):
    66         space = self.game_object.get_space()
    67         for shape in space.shape_query(self.game_object.get_shape()):
    68             if shape.collision_type in self._collision_types:
    69                 return True
    70         return False
    71 
    72 
    73 class StateProxyPuzzler(Puzzler):
    74     def __init__(self, state_source):
    75         self._state_source = state_source
    76 
    77     def get_state(self):
    78         return self.glue.get_state_of(self._state_source)
    79 
    80 
    81 class StateLogicalAndPuzzler(Puzzler):
    82     def __init__(self, *state_sources):
    83         self._state_sources = state_sources
    84 
    85     def get_state(self):
    86         for state_source in self._state_sources:
    87             if not self.glue.get_state_of(state_source):
    88                 return False
    89         return True
     17# For levels to import, until we get module names in 'classname'
     18StateProxyPuzzler = puzzle.StateProxyPuzzler
     19StateLogicalAndPuzzler = puzzle.StateLogicalAndPuzzler
    9020
    9121
     
    404334                False: resources.get_image('objects', 'sensor_off.png'),
    405335            }),
    406             CollidePuzzler(*SWITCH_PUSHERS),
     336            puzzle.CollidePuzzler(*SWITCH_PUSHERS),
    407337        )
    408338
     
    418348            SingleShapePhysicser(space, self.shape),
    419349            ImageRenderer(resources.get_image('objects', 'note.png')),
    420             CollidePuzzler(),
     350            puzzle.CollidePuzzler(),
    421351            TextOverlay(message),
    422352        )
     
    437367                False: resources.get_image('objects', 'light_off.png'),
    438368            }),
    439             StateProxyPuzzler(state_source),
     369            puzzle.StateProxyPuzzler(state_source),
    440370        )
    441371
     
    465395        self.dest_pos = tuple(dest_pos)
    466396        if key_state is None:
    467             puzzler = YesPuzzler()
     397            puzzler = puzzle.YesPuzzler()
    468398        else:
    469             puzzler = StateProxyPuzzler(key_state)
     399            puzzler = puzzle.StateProxyPuzzler(key_state)
    470400        super(Door, self).__init__(
    471401            SingleShapePhysicser(space, self.shape),
Note: See TracChangeset for help on using the changeset viewer.