Changeset 106:bce9cd8a4a8c


Ignore:
Timestamp:
Sep 2, 2013, 11:48:24 AM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

FloorLight?, linked to a FloorSwitch?.

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r104 r106  
    99
    1010
     11class PuzzleGlue(object):
     12    """Glue that holds bits of a puzzle together.
     13    """
     14    def __init__(self):
     15        self._components = {}
     16
     17    def add_component(self, name, puzzler):
     18        self._components[name] = puzzler
     19        puzzler.set_glue(self)
     20
     21    def get_state_of(self, name):
     22        return self._components[name].get_state()
     23
     24
    1125class Puzzler(object):
    12     def get_state(self, space):
    13         raise NotImplementedError()
    14 
    15     def notify(self):
    16         pass
     26    """Behaviour specific to a puzzle component.
     27    """
     28    def set_glue(self, glue):
     29        self.glue = glue
     30
     31    def get_state(self):
     32        raise NotImplementedError()
    1733
    1834
     
    2743                return True
    2844        return False
     45
     46
     47class StateProxyPuzzler(Puzzler):
     48    def __init__(self, state_source):
     49        self._state_source = state_source
     50
     51    def get_state(self):
     52        return self.glue.get_state_of(self._state_source)
    2953
    3054
     
    186210            self.shape.color = pygame.color.THECOLORS['red']
    187211        super(FloorSwitch, self).render(surface)
     212
     213
     214class FloorLight(GameObject):
     215    def __init__(self, space, position, state_source):
     216        body = pymunk.Body()
     217        body.position = position
     218        self.shape = pymunk.Circle(body, 10)
     219        self.shape.collision_type = COLLISION_TYPE_SWITCH
     220        self.shape.sensor = True
     221        super(FloorLight, self).__init__(
     222            SingleShapePhysicser(space, self.shape),
     223            ShapeRenderer(self.shape),
     224            StateProxyPuzzler(state_source),
     225        )
     226
     227    def render(self, surface):
     228        if self.puzzler.get_state():
     229            self.shape.color = pygame.color.THECOLORS['green']
     230        else:
     231            self.shape.color = pygame.color.THECOLORS['red']
     232        super(FloorLight, self).render(surface)
  • nagslang/screens/area.py

    r105 r106  
    66
    77from nagslang.events import ScreenChange
    8 from nagslang.game_object import FloorSwitch
     8from nagslang.game_object import FloorSwitch, FloorLight, PuzzleGlue
    99from nagslang.level import Level
    1010from nagslang.protagonist import Protagonist
     
    5454        self.add_protagonist()
    5555        # TODO: Put this in a level instead:
    56         self._drawables.append(FloorSwitch(self.space, (300, 400)))
     56        glue = PuzzleGlue()
     57        switch = FloorSwitch(self.space, (300, 400))
     58        light = FloorLight(self.space, (300, 500), 'switch')
     59        glue.add_component('switch', switch.puzzler)
     60        glue.add_component('light', light.puzzler)
     61        self._drawables.append(switch)
     62        self._drawables.append(light)
    5763
    5864    def add_walls(self):
Note: See TracChangeset for help on using the changeset viewer.