Changeset 427:3ee839f227ad for nagslang


Ignore:
Timestamp:
Sep 7, 2013, 11:34:19 AM (7 years ago)
Author:
davidsharpe@…
Branch:
default
Message:

Special relativistic gravities.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r418 r427  
    9898    def get_shape(self):
    9999        return self._shape
     100
     101
     102class MultiShapePhysicser(Physicser):
     103    def __init__(self, space, shape, *extra_shapes):
     104        super(MultiShapePhysicser, self).__init__(space)
     105        self._shape = shape
     106        self._extra_shapes = extra_shapes
     107        shape.physicser = self
     108
     109    def get_shape(self):
     110        return self._shape
     111
     112    def add_to_space(self):
     113        shape = self.get_shape()
     114        self.get_space().add(shape)
     115        if not shape.body.is_static:
     116            self.get_space().add(shape.body)
     117        for s in self._extra_shapes:
     118            self.get_space().add(s)
     119
     120    def remove_from_space(self):
     121        shape = self.get_shape()
     122        self.get_space().remove(shape)
     123        if not shape.body.is_static:
     124            self.get_space().remove(shape.body)
     125        for s in self._extra_shapes:
     126            self.get_space().remove(s)
    100127
    101128
     
    212239        return True
    213240
    214     def environmental_movement(self, dx, dy):
    215         if (dx, dy) == (0, 0):
    216             return
    217         self.physicser.apply_impulse((dx, dy))
     241    def environmental_movement(self, vec):
     242        self.physicser.apply_impulse(vec)
    218243
    219244    @classmethod
     
    606631    rate = 5
    607632
    608     def __init__(self, space, position, size, force):
    609         body = make_body(10, pymunk.inf, position)
     633    def __init__(self, space, position, radius, force):
     634        body = make_body(None, None, position)
    610635        # Adjust shape relative to position
    611         self.shape = pymunk.Circle(body, size)
     636        self._radius = radius
     637        self.shape = pymunk.Circle(body, radius)
     638        self.centre = pymunk.Circle(body, 10)
     639        self.centre.friction = pymunk.inf
    612640        self._ticks = 0
    613641        self.force = force
     
    615643        self.shape.sensor = True
    616644        super(GravityWell, self).__init__(
    617             SingleShapePhysicser(space, self.shape),
     645            MultiShapePhysicser(space, self.shape, self.centre),
    618646            render.ImageRenderer(resources.get_image(
    619647                'objects', 'gravity_well.png')),
     
    625653        # off terrian, but as long as the rate is reasonably
    626654        # low, they shouldn't impact gameplay
    627         if self._ticks == 0:
    628             self.apply_effect(protagonist)
    629         self._ticks += 1
    630         if self._ticks > self.rate:
    631             self._ticks = 0
     655        self.apply_effect(protagonist)
    632656
    633657    def collide_with_furniture(self, furniture):
     
    636660        # off terrian, but as long as the rate is reasonably
    637661        # low, they shouldn't impact gameplay
    638         if self._ticks == 0:
    639             self.apply_effect(furniture)
    640         self._ticks += 1
    641         if self._ticks > self.rate:
    642             self._ticks = 0
     662        self.apply_effect(furniture)
    643663
    644664    def apply_effect(self, object_to_move):
    645         object_to_move.environmental_movement(self.force, self.force)
     665        movement = self.physicser.position - object_to_move.physicser.position
     666        force = self.force * (1 - (self._radius / movement.length))
     667        movement.length = force
     668        object_to_move.environmental_movement(-movement)
    646669
    647670    @classmethod
Note: See TracChangeset for help on using the changeset viewer.