Ignore:
Timestamp:
09/06/13 09:39:48 (9 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Phase:
public
Message:

Physics-related cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/protagonist.py

    r333 r334  
    11import pymunk
    22import pymunk.pygame_util
    3 from pymunk.vec2d import Vec2d
    43
    54from nagslang import render
     
    1312from nagslang.resources import resources
    1413from nagslang.events import ScreenChange
     14from nagslang.utils import vec_from_angle, vec_with_length
    1515
    1616
     
    171171
    172172    def get_facing_direction(self):
    173         # It's easier to work with a vector than an angle here.
    174         vec = Vec2d.unit()
    175         vec.angle = self.angle
    176         # We probably don't have exactly -1, 0, or 1 here.
     173        # Our angle is quantised to 45 degree intervals, so possible values for
     174        # x and y in a unit vector are +/-(0, sqrt(2)/2, 1) with some floating
     175        # point imprecision. Rounding will normalise these to (-1.0, 0.0, 1.0)
     176        # which we can safely turn into integers and use as dict keys.
     177        vec = vec_from_angle(self.angle)
    177178        x = int(round(vec.x))
    178179        y = int(round(vec.y))
     
    204205            return
    205206        self.is_moving = True
    206         self.angle = pymunk.Vec2d((dx, dy)).angle
    207         self.physicser.apply_impulse(
    208             (dx * self.impulse_factor, dy * self.impulse_factor))
     207        vec = vec_with_length((dx, dy), self.impulse_factor)
     208        self.angle = vec.angle
     209        self.physicser.apply_impulse(vec)
    209210
    210211    def set_position(self, position):
     
    259260        if not self.has_item('gun'):
    260261            return
    261         vec = Vec2d.unit()
    262         vec.angle = self.angle
    263         vec.length = 1000
     262        vec = vec_from_angle(self.angle, 1000)
    264263        FireEvent.post(
    265264            self.physicser.position, vec, BULLET_DAMAGE, COLLISION_TYPE_PLAYER)
    266265
    267266    def claw(self):
    268         vec = Vec2d.unit()
    269         vec.angle = self.angle
    270         vec.length = 30
     267        vec = vec_from_angle(self.angle, 30)
    271268        ClawEvent.post(self.physicser.position, vec, CLAW_DAMAGE)
    272269
Note: See TracChangeset for help on using the changeset viewer.