Changeset 93:d6a49f0c1e6e


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

Rectangular human protagonist shape, refactored physicsers.

Location:
nagslang
Files:
3 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:
  • nagslang/protagonist.py

    r81 r93  
    1818    WOLF_FORM = 'wolf'
    1919
    20     def __init__(self, position):
    21         self._setup_physics(position)
     20    def __init__(self, space, position):
     21        self._setup_physics(space, position)
    2222        self._setup_renderers()
    2323        self.inventory = {}
     
    2525
    2626        super(Protagonist, self).__init__(
    27             self._renderers[self.form], SingleShapePhysicser(self.shape))
     27            self._physicsers[self.form], self._renderers[self.form])
    2828
    2929        self.go_human()
    3030
    31     def _setup_physics(self, position):
    32         self._body = pymunk.Body(10, 10000)
     31    def _setup_physics(self, space, position):
     32        self._body = pymunk.Body(10, pymunk.inf)
    3333        self._body.position = position
    3434        self._body.velocity_func = self.velocity_func
    3535
    36         self.shape = pymunk.Circle(self._body, 30)
    37         self.shape.elasticity = 1.0
    38         self.shape.friction = 10.0
    39         self.shape.collision_type = COLLISION_TYPE_PLAYER
     36        self._shapes = {
     37            self.HUMAN_FORM: pymunk.Poly(
     38                self._body, [(-15, -30), (15, -30), (15, 30), (-15, 30)]),
     39            self.WOLF_FORM: pymunk.Circle(self._body, 30),
     40        }
     41        self._physicsers = {}
     42        for form, shape in self._shapes.iteritems():
     43            shape.elasticity = 1.0
     44            shape.friction = 10.0
     45            shape.collision_type = COLLISION_TYPE_PLAYER
     46            self._physicsers[form] = SingleShapePhysicser(space, shape)
     47        self.angle = 0
    4048
    4149    def _get_image(self, name, *transforms):
     
    6371        return pymunk.Body.update_velocity(body, gravity, self.damping, dt)
    6472
     73    def get_render_angle(self):
     74        return self.angle
     75
    6576    def go_werewolf(self):
     77        self._physicsers[self.form].remove_from_space()
    6678        self.form = self.WOLF_FORM
     79        self._physicsers[self.form].add_to_space()
    6780        self._body.mass = 100
    68         self._body.moment = 10000
    6981        self._body.velocity_limit = 1000
    7082        self.impulse_factor = 4000
     
    7385
    7486    def go_human(self):
     87        self._physicsers[self.form].remove_from_space()
    7588        self.form = self.HUMAN_FORM
     89        self._physicsers[self.form].add_to_space()
    7690        self._body.mass = 10
    77         self._body.moment = 1000
    7891        self._body.velocity_limit = 1000
    7992        self.impulse_factor = 500
     
    8497        if (dx, dy) == (0, 0):
    8598            return
    86         vec = pymunk.Vec2d((dx, dy))
    87         self._body.angle = vec.angle
     99        self.angle = pymunk.Vec2d((dx, dy)).angle
    88100        self._body.apply_impulse(
    89101            (dx * self.impulse_factor, dy * self.impulse_factor))
     
    119131            return
    120132        self._body.apply_impulse((dx, dy))
     133
     134    def render(self, surface):
     135        pymunk.pygame_util.draw(surface, self._shapes[self.form])
     136        super(Protagonist, self).render(surface)
  • nagslang/screens/area.py

    r92 r93  
    5454
    5555    def add_protagonist(self):
    56         self.protagonist = Protagonist((350, 300))
    57         self.protagonist.add_to_space(self.space)
    58         self.toy_switch = FloorSwitch((300, 400))
    59         self.toy_switch.add_to_space(self.space)
     56        self.protagonist = Protagonist(self.space, (350, 300))
     57        self.toy_switch = FloorSwitch(self.space, (300, 400))
    6058
    6159    def handle_event(self, ev):
     
    7068        level_size = level_surface.get_size()
    7169        display_size = display_surface.get_size()
    72         protagnist_pos = self.protagonist.physicser.render_position(
     70        protagnist_pos = self.protagonist.physicser.get_render_position(
    7371                level_surface)
    7472        x_wide = display_size[0] // 2
Note: See TracChangeset for help on using the changeset viewer.