Ignore:
Timestamp:
09/01/13 20:53:37 (8 years ago)
Author:
davidsharpe@…
Branch:
default
Phase:
public
Message:

Floor switch with horrible hackery.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r63 r81  
    11import math
     2
     3import pygame
     4import pymunk
     5
     6from nagslang.constants import SWITCH_PUSHERS, COLLISION_TYPE_SWITCH
     7
     8class Puzzler(object):
     9    def get_state(self, space):
     10        raise NotImplementedError()
     11
     12    def notify(self):
     13        pass
     14
     15
     16class FloorSwitchPuzzler(Puzzler):
     17    def __init__(self, shape):
     18        self.shape = shape
     19
     20    def get_state(self, space):
     21        for shape in space.shape_query(self.shape):
     22            if shape.collision_type in SWITCH_PUSHERS:
     23                return True
     24        return False
    225
    326
     
    99122    """
    100123
    101     def __init__(self, renderer, physicser=None):
     124    def __init__(self, renderer, physicser=None, puzzler=None):
    102125        self.renderer = renderer
    103126        if physicser is None:
    104127            physicser = NullPhysicser()
    105128        self.physicser = physicser
     129        self.puzzler = puzzler
    106130
    107131    def add_to_space(self, space):
     
    112136            surface, self.physicser.render_position(surface),
    113137            self.physicser.render_angle())
     138
     139
     140class FloorSwitch(GameObject):
     141    def __init__(self, position):
     142        body = pymunk.Body()
     143        body.position = position
     144        self.shape = pymunk.Circle(body, 30)
     145        self.shape.collision_type = COLLISION_TYPE_SWITCH
     146        self.shape.sensor = True
     147        super(FloorSwitch, self).__init__(
     148            ShapeRenderer(self.shape),
     149            SingleShapePhysicser(self.shape),
     150            FloorSwitchPuzzler(self.shape),
     151        )
     152
     153    def add_to_space(self, space):
     154        # XXX: Hacky hack.
     155        self._space = space
     156
     157    def render(self, surface):
     158        if self.puzzler.get_state(self._space):
     159            self.shape.color = pygame.color.THECOLORS['green']
     160        else:
     161            self.shape.color = pygame.color.THECOLORS['red']
     162        super(FloorSwitch, self).render(surface)
Note: See TracChangeset for help on using the changeset viewer.