Changeset 81:a1b4d09e6f23


Ignore:
Timestamp:
Sep 1, 2013, 8:53:37 PM (7 years ago)
Author:
davidsharpe@…
Branch:
default
Message:

Floor switch with horrible hackery.

Location:
nagslang
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • nagslang/constants.py

    r29 r81  
    88    sound=True,
    99)
     10
     11COLLISION_TYPE_OTHER = 0
     12COLLISION_TYPE_PLAYER = 1
     13COLLISION_TYPE_WALL = 2
     14COLLISION_TYPE_SWITCH = 3
     15COLLISION_TYPE_BOX = 4
     16
     17SWITCH_PUSHERS = [COLLISION_TYPE_PLAYER, COLLISION_TYPE_BOX]
  • 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)
  • nagslang/protagonist.py

    r76 r81  
    22import pymunk.pygame_util
    33
     4from nagslang.constants import COLLISION_TYPE_PLAYER
    45from nagslang.game_object import (
    56    GameObject, SingleShapePhysicser, FacingImageRenderer)
     
    3637        self.shape.elasticity = 1.0
    3738        self.shape.friction = 10.0
     39        self.shape.collision_type = COLLISION_TYPE_PLAYER
    3840
    3941    def _get_image(self, name, *transforms):
  • nagslang/screens/area.py

    r65 r81  
    66
    77from nagslang.events import ScreenChange
     8from nagslang.game_object import FloorSwitch
    89from nagslang.level import Level
    910from nagslang.protagonist import Protagonist
    1011from nagslang.screens.base import Screen
    11 
    1212
    1313class ControlKeys(object):
     
    5555        self.protagonist = Protagonist((400, 300))
    5656        self.protagonist.add_to_space(self.space)
     57        self.toy_switch = FloorSwitch((300, 400))
     58        self.toy_switch.add_to_space(self.space)
    5759
    5860    def handle_event(self, ev):
     
    7072        #pymunk.pygame_util.draw(surface, *self.walls)
    7173        self.protagonist.render(surface)
     74        self.toy_switch.render(surface)
    7275
    7376    def tick_protagonist(self):
     
    8588        self.tick_protagonist()
    8689        super(AreaScreen, self).tick(seconds)
     90
Note: See TracChangeset for help on using the changeset viewer.