Changeset 186:d63c19003aec


Ignore:
Timestamp:
Sep 3, 2013, 6:06:12 PM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Some refactoring and fixing, start of better collision handling.

Location:
nagslang
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • nagslang/constants.py

    r176 r186  
    1919SWITCH_PUSHERS = [COLLISION_TYPE_PLAYER, COLLISION_TYPE_BOX]
    2020
     21CALLBACK_COLLIDERS = [
     22    # Collisions between the player and shapes with these collision types will
     23    # fire callbacks on the game object associated with the shape.
     24    COLLISION_TYPE_SWITCH,
     25    COLLISION_TYPE_BOX,
     26    COLLISION_TYPE_ENEMY,
     27    COLLISION_TYPE_DOOR,
     28]
     29
    2130ZORDER_FLOOR = 0
    2231ZORDER_LOW = 1
  • nagslang/events.py

    r180 r186  
    22
    33import pygame
     4import pygame.locals
    45
    56
  • nagslang/game_object.py

    r185 r186  
    4242
    4343
     44class YesPuzzler(Puzzler):
     45    """Yes sir, I'm always on.
     46    """
     47    def get_state(self):
     48        return True
     49
     50
     51class NoPuzzler(Puzzler):
     52    """No sir, I'm always off.
     53    """
     54    def get_state(self):
     55        return False
     56
     57
    4458class CollidePuzzler(Puzzler):
    4559    def __init__(self, *collision_types):
     
    108122        super(SingleShapePhysicser, self).__init__(space)
    109123        self._shape = shape
     124        shape.physicser = self
    110125
    111126    def get_shape(self):
     
    342357        self.renderer.animate()
    343358
     359    def collide_with_protagonist(self):
     360        """Called as a `pre_solve` collision callback with the protagonist.
     361
     362        You can return `False` to ignore the collision, anything else
     363        (including `None`) to process the collision as normal.
     364        """
     365        pass
     366
    344367
    345368class FloorSwitch(GameObject):
     
    394417    zorder = ZORDER_FLOOR
    395418
    396     def __init__(self, space, position, destination, dest_pos):
     419    def __init__(self, space, position, destination, dest_pos, key_state=None):
    397420        body = make_body(pymunk.inf, pymunk.inf, position, damping=0.5)
    398421        self.shape = pymunk.Poly(
     
    402425        self.destination = destination
    403426        self.dest_pos = tuple(dest_pos)
     427        if key_state is None:
     428            puzzler = YesPuzzler()
     429        else:
     430            puzzler = StateProxyPuzzler(key_state)
    404431        super(Door, self).__init__(
    405432            SingleShapePhysicser(space, self.shape),
    406433            ImageRenderer(resources.get_image('objects', 'door.png')),
     434            puzzler,
    407435        )
    408436
  • nagslang/tests/test_game_object.py

    r145 r186  
    4444        return puzzler
    4545
    46     def assert_floor_switch(self, expected, shapes):
     46    def assert_collide_state(self, expected, shapes, collision_types):
    4747        gobj = FakeGameObject(None, FakeSpace(*shapes))
    48         puzzler = self.mkpuzzler(gobj, game_object.FloorSwitchPuzzler)
     48        puzzler = self.mkpuzzler(
     49            gobj, game_object.CollidePuzzler, *collision_types)
    4950        self.assertEqual(expected, puzzler.get_state())
    5051
    51     def test_floor_switch_puzzler(self):
    52         self.assert_floor_switch(False, [])
    53         self.assert_floor_switch(False, [FakeShape()])
     52    def test_collide_puzzler(self):
     53        self.assert_collide_state(False, [], [])
     54        self.assert_collide_state(False, [FakeShape()], SWITCH_PUSHERS)
    5455
    5556        for collision_type in SWITCH_PUSHERS:
    56             self.assert_floor_switch(True, [FakeShape(collision_type)])
    57             self.assert_floor_switch(
    58                 True, [FakeShape(), FakeShape(collision_type)])
     57            self.assert_collide_state(
     58                True, [FakeShape(collision_type)], SWITCH_PUSHERS)
     59            self.assert_collide_state(
     60                True, [FakeShape(), FakeShape(collision_type)], SWITCH_PUSHERS)
    5961
    6062    def test_state_proxy_puzzler(self):
  • nagslang/tests/test_level.py

    r164 r186  
    7979                isinstance(puzzle_bits['foo_proxy'], go.StateProxyPuzzler))
    8080            self.assertEqual('foo', puzzle_bits['foo_proxy']._state_source)
    81             self.assertTrue(isinstance(puzzle_bits['foo'],
    82                             go.FloorSwitchPuzzler))
     81            self.assertTrue(isinstance(puzzle_bits['foo'], go.CollidePuzzler))
    8382
    8483        level = self.make_level('foo', {
Note: See TracChangeset for help on using the changeset viewer.