Changeset 293:47226c661ae2


Ignore:
Timestamp:
Sep 5, 2013, 9:26:13 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
rebase_source:
f0870a31cda9c73d2cdad490c23342d04ff63d55
Message:

Bullets that mostly die when they hit things

Location:
nagslang
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • nagslang/events.py

    r276 r293  
    4949class FireEvent(UserEvent):
    5050    @classmethod
    51     def post(cls, source, impulse):
    52         super(FireEvent, cls).post(source=source, impulse=impulse)
     51    def post(cls, source, impulse, source_collision_type):
     52        super(FireEvent, cls).post(source=source, impulse=impulse,
     53                                   source_collision_type=source_collision_type)
  • nagslang/game_object.py

    r286 r293  
    123123        if interactible is not None:
    124124            self.interactible.set_game_object(self)
     125        self.remove = False  # If true, will be removed from drawables
    125126
    126127    def get_space(self):
     
    333334
    334335class Bullet(GameObject):
    335     def __init__(self, space, position, impulse):
     336    def __init__(self, space, position, impulse, source_collision_type):
    336337        body = make_body(1, pymunk.inf, position)
     338        self.last_position = position
    337339        self.shape = pymunk.Circle(body, 2)
     340        self.shape.sensor = True
    338341        self.shape.collision_type = COLLISION_TYPE_PROJECTILE
     342        self.source_collision_type = source_collision_type
    339343        super(Bullet, self).__init__(
    340344            SingleShapePhysicser(space, self.shape),
     
    342346        )
    343347        self.physicser.apply_impulse(impulse)
     348
     349    def animate(self):
     350        super(Bullet, self).animate()
     351        position = self.physicser.position
     352        r = self.get_space().segment_query(self.last_position, position)
     353        self.last_position = position
     354        for collision in r:
     355            if collision.shape.collision_type == self.source_collision_type:
     356                continue
     357            if collision.shape == self.physicser.get_shape():
     358                continue
     359            print "Hit", collision.shape.collision_type
     360            self.physicser.remove_from_space()
     361            self.remove = True
     362            break
    344363
    345364
  • nagslang/protagonist.py

    r291 r293  
    262262        vec.angle = self.angle
    263263        vec.length = 1000
    264         FireEvent.post(self.physicser.position, vec)
     264        FireEvent.post(self.physicser.position, vec, COLLISION_TYPE_PLAYER)
    265265
    266266    def in_wolf_form(self):
  • nagslang/screens/area.py

    r283 r293  
    146146            # position change is enough
    147147        elif FireEvent.matches(ev):
    148             bullet = Bullet(self.space, ev.source, ev.impulse)
     148            bullet = Bullet(self.space, ev.source, ev.impulse,
     149                            ev.source_collision_type)
    149150            self._drawables.add(bullet)
    150151        self.keys.handle_event(ev)
     
    197198        for drawable in self._drawables:
    198199            drawable.animate()
     200            if drawable.remove:
     201                self._drawables.remove(drawable)
    199202
    200203        super(AreaScreen, self).tick(seconds)
Note: See TracChangeset for help on using the changeset viewer.