Changeset 133:79e1888573d3


Ignore:
Timestamp:
Sep 2, 2013, 4:05:03 PM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

A box.

Location:
nagslang
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r126 r133  
    66
    77from nagslang.constants import (
    8     SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, ZORDER_LOW)
     8    SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW)
    99from nagslang.options import options
    1010
     
    175175
    176176
    177 class ShapeStateRenderer(Renderer):
     177class ShapeRenderer(Renderer):
     178    def render(self, surface):
     179        self._render_shape(surface)
     180        super(ShapeRenderer, self).render(surface)
     181
     182
     183class ShapeStateRenderer(ShapeRenderer):
    178184    """Renders the shape in a different colour depending on the state.
    179185
     
    187193
    188194        self.game_object.get_shape().color = color
    189         self._render_shape(surface)
    190195        super(ShapeStateRenderer, self).render(surface)
     196
     197
     198def damping_velocity_func(body, gravity, damping, dt):
     199    """Apply custom damping to this body's velocity.
     200    """
     201    damping = getattr(body, 'damping', damping)
     202    return pymunk.Body.update_velocity(body, gravity, damping, dt)
     203
     204
     205def make_body(mass, moment, position, damping=None):
     206    body = pymunk.Body(mass, moment)
     207    body.position = position
     208    if damping is not None:
     209        body.damping = damping
     210        body.velocity_func = damping_velocity_func
     211    return body
    191212
    192213
     
    250271            StateProxyPuzzler(state_source),
    251272        )
     273
     274
     275class Box(GameObject):
     276    def __init__(self, space, position):
     277        body = make_body(10, 10000, position, damping=0.5)
     278        self.shape = pymunk.Poly(
     279            body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
     280        self.shape.collision_type = COLLISION_TYPE_BOX
     281        super(Box, self).__init__(
     282            SingleShapePhysicser(space, self.shape),
     283            ShapeRenderer(),
     284        )
  • nagslang/protagonist.py

    r123 r133  
    44from nagslang.constants import COLLISION_TYPE_PLAYER, ZORDER_MID
    55from nagslang.game_object import (
    6     GameObject, SingleShapePhysicser, FacingImageRenderer)
     6    GameObject, SingleShapePhysicser, FacingImageRenderer, make_body)
    77from nagslang.mutators import FLIP_H
    88from nagslang.resources import resources
     
    3131
    3232    def _setup_physics(self, space, position):
    33         self._body = pymunk.Body(10, pymunk.inf)
    34         self._body.position = position
    35         self._body.velocity_func = self.velocity_func
     33        self._body = make_body(10, pymunk.inf, position, 0.8)
    3634
    3735        self._shapes = {
     
    7169        return obj
    7270
    73     def velocity_func(self, body, gravity, damping, dt):
    74         return pymunk.Body.update_velocity(body, gravity, self.damping, dt)
    75 
    7671    def get_render_angle(self):
    7772        return self.angle
     
    8479        self._body.velocity_limit = 1000
    8580        self.impulse_factor = 4000
    86         self.damping = 0.9
     81        self._body.damping = 0.9
    8782        self.renderer = self._renderers[self.form]
    8883
     
    9489        self._body.velocity_limit = 1000
    9590        self.impulse_factor = 500
    96         self.damping = 0.8
     91        self._body.damping = 0.8
    9792        self.renderer = self._renderers[self.form]
    9893
  • nagslang/screens/area.py

    r127 r133  
    77from nagslang.constants import ZORDER_HIGH
    88from nagslang.events import ScreenChange
    9 from nagslang.game_object import FloorSwitch, FloorLight, PuzzleGlue
     9from nagslang.game_object import FloorSwitch, FloorLight, PuzzleGlue, Box
    1010from nagslang.level import Level
    1111from nagslang.protagonist import Protagonist
     
    7272        self.add_walls()
    7373        self.add_protagonist()
    74         # TODO: Put this in a level instead:
     74        self._setup_demo_objects()
     75
     76    def _setup_demo_objects(self):
     77        # TODO: Put this in a level instead
    7578        glue = PuzzleGlue()
    7679        switch = FloorSwitch(self.space, (300, 400))
     
    8487        self._drawables.add(light)
    8588        self._drawables.add(light2)
     89        box = Box(self.space, (250, 350))
     90        self._drawables.add(box)
    8691
    8792    def add_walls(self):
Note: See TracChangeset for help on using the changeset viewer.