Changeset 201:3495a2025bc6


Ignore:
Timestamp:
Sep 3, 2013, 9:27:25 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
rebase_source:
cb4c938f882a6d9c325f13bda95e7f3179bc4475
Message:

Break puzzlers out of game_object.py

Location:
nagslang
Files:
1 added
2 edited
1 moved

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),
  • nagslang/level.py

    r197 r201  
    44from nagslang import game_object as go
    55from nagslang import enemies
     6from nagslang import puzzle
    67from nagslang.resources import resources
    78from nagslang.yamlish import load, dump
     
    3334        self._surface = None
    3435        self._exterior = False
    35         self._glue = go.PuzzleGlue()
     36        self._glue = puzzle.PuzzleGlue()
    3637        self.drawables = []
    3738        self.overlay_drawables = []
     
    7576        # At least this is better than just calling `eval`, right?
    7677        cls = getattr(go, classname)
    77         if issubclass(cls, go.Puzzler):
     78        if issubclass(cls, puzzle.Puzzler):
    7879            gobj = cls(*args)
    7980        elif issubclass(cls, go.GameObject):
  • nagslang/tests/test_puzzle.py

    r186 r201  
    22
    33from nagslang.constants import COLLISION_TYPE_OTHER, SWITCH_PUSHERS
    4 from nagslang import game_object
     4from nagslang import puzzle
    55
    66
     
    3030
    3131
    32 class FakePuzzler(game_object.Puzzler):
     32class FakePuzzler(puzzle.Puzzler):
    3333    def __init__(self, fake_state):
    3434        self.fake_state = fake_state
     
    4747        gobj = FakeGameObject(None, FakeSpace(*shapes))
    4848        puzzler = self.mkpuzzler(
    49             gobj, game_object.CollidePuzzler, *collision_types)
     49            gobj, puzzle.CollidePuzzler, *collision_types)
    5050        self.assertEqual(expected, puzzler.get_state())
    5151
     
    6161
    6262    def test_state_proxy_puzzler(self):
    63         glue = game_object.PuzzleGlue()
    64         puzzler = game_object.StateProxyPuzzler('faker')
     63        glue = puzzle.PuzzleGlue()
     64        puzzler = puzzle.StateProxyPuzzler('faker')
    6565        glue.add_component('puzzler', puzzler)
    6666        faker = FakePuzzler('foo')
     
    7272
    7373    def test_glue_add_component(self):
    74         glue = game_object.PuzzleGlue()
     74        glue = puzzle.PuzzleGlue()
    7575        puzzler = FakePuzzler('foo')
    7676        gobj = FakeGameObject(None, None)
Note: See TracChangeset for help on using the changeset viewer.