Ignore:
Timestamp:
09/02/13 09:10:50 (8 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Phase:
public
Message:

Rectangular human protagonist shape, refactored physicsers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r91 r93  
    33import pygame
    44import pymunk
     5import pymunk.pygame_util
    56
    67from nagslang.constants import SWITCH_PUSHERS, COLLISION_TYPE_SWITCH
     
    1617
    1718class FloorSwitchPuzzler(Puzzler):
    18     def __init__(self, shape):
    19         self.shape = shape
     19    def __init__(self, space, shape):
     20        self._space = space
     21        self._shape = shape
    2022
    21     def get_state(self, space):
    22         for shape in space.shape_query(self.shape):
     23    def get_state(self):
     24        for shape in self._space.shape_query(self._shape):
    2325            if shape.collision_type in SWITCH_PUSHERS:
    2426                return True
     
    2729
    2830class Physicser(object):
    29     def add_to_space(self, space):
     31    def __init__(self, space):
     32        self.space = space
     33
     34    def add_to_space(self):
    3035        raise NotImplementedError()
    3136
    32     def remove_from_space(self, space):
     37    def remove_from_space(self):
    3338        raise NotImplementedError()
    3439
    35     def render_position(self, surface):
     40    def get_render_position(self, surface):
    3641        raise NotImplementedError()
    3742
    38     def render_angle(self):
     43    def get_angle(self):
     44        raise NotImplementedError()
     45
     46    def apply_impulse(self, j, r=(0, 0)):
    3947        raise NotImplementedError()
    4048
    4149
    4250class SingleShapePhysicser(Physicser):
    43     def __init__(self, shape):
     51    def __init__(self, space, shape):
     52        super(SingleShapePhysicser, self).__init__(space)
    4453        self._shape = shape
    4554
    46     def add_to_space(self, space):
    47         space.add(self._shape, self._shape.body)
     55    def add_to_space(self):
     56        self.space.add(self._shape)
     57        if not self._shape.body.is_static:
     58            self.space.add(self._shape.body)
    4859
    49     def remove_from_space(self, space):
    50         space.remove(self._shape, self._shape.body)
     60    def remove_from_space(self):
     61        self.space.remove(self._shape)
     62        if not self._shape.body.is_static:
     63            self.space.remove(self._shape.body)
    5164
    52     def render_position(self, surface):
     65    def get_render_position(self, surface):
    5366        pos = self._shape.body.position
    54         import pymunk.pygame_util
    5567        return pymunk.pygame_util.to_pygame(pos, surface)
    5668
    57     def render_angle(self):
     69    def get_angle(self):
    5870        return self._shape.body.angle
     71
     72    def apply_impulse(self, j, r=(0, 0)):
     73        return self._shape.body.apply_impulse(j, r)
    5974
    6075
     
    109124    """
    110125
    111     def __init__(self, renderer, physicser, puzzler=None):
     126    def __init__(self, physicser, renderer, puzzler=None):
     127        self.physicser = physicser
     128        self.physicser.add_to_space()
    112129        self.renderer = renderer
    113         self.physicser = physicser
    114130        self.puzzler = puzzler
    115131
    116     def add_to_space(self, space):
    117         self.physicser.add_to_space(space)
     132    def get_render_position(self, surface):
     133        return self.physicser.get_render_position(surface)
     134
     135    def get_render_angle(self):
     136        return self.physicser.get_angle()
    118137
    119138    def render(self, surface):
    120139        return self.renderer.render(
    121             surface, self.physicser.render_position(surface),
    122             self.physicser.render_angle())
     140            surface, self.get_render_position(surface),
     141            self.get_render_angle())
    123142
    124143
    125144class FloorSwitch(GameObject):
    126     def __init__(self, position):
     145    def __init__(self, space, position):
    127146        body = pymunk.Body()
    128147        body.position = position
     
    131150        self.shape.sensor = True
    132151        super(FloorSwitch, self).__init__(
     152            SingleShapePhysicser(space, self.shape),
    133153            ShapeRenderer(self.shape),
    134             SingleShapePhysicser(self.shape),
    135             FloorSwitchPuzzler(self.shape),
     154            FloorSwitchPuzzler(space, self.shape),
    136155        )
    137156
    138     def add_to_space(self, space):
    139         # XXX: Hacky hack.
    140         self._space = space
    141 
    142157    def render(self, surface):
    143         if self.puzzler.get_state(self._space):
     158        if self.puzzler.get_state():
    144159            self.shape.color = pygame.color.THECOLORS['green']
    145160        else:
Note: See TracChangeset for help on using the changeset viewer.