# HG changeset patch # User Stefano Rivera # Date 1378416373 -7200 # Node ID 47226c661ae2bdbfe4cdc85df0c821aafb7ade76 # Parent 7b121ed73b956a93d3d68c44c5841809fd4b28bf Bullets that mostly die when they hit things diff -r 7b121ed73b95 -r 47226c661ae2 nagslang/events.py --- a/nagslang/events.py Thu Sep 05 23:17:02 2013 +0200 +++ b/nagslang/events.py Thu Sep 05 23:26:13 2013 +0200 @@ -48,5 +48,6 @@ class FireEvent(UserEvent): @classmethod - def post(cls, source, impulse): - super(FireEvent, cls).post(source=source, impulse=impulse) + def post(cls, source, impulse, source_collision_type): + super(FireEvent, cls).post(source=source, impulse=impulse, + source_collision_type=source_collision_type) diff -r 7b121ed73b95 -r 47226c661ae2 nagslang/game_object.py --- a/nagslang/game_object.py Thu Sep 05 23:17:02 2013 +0200 +++ b/nagslang/game_object.py Thu Sep 05 23:26:13 2013 +0200 @@ -122,6 +122,7 @@ self.interactible = interactible if interactible is not None: self.interactible.set_game_object(self) + self.remove = False # If true, will be removed from drawables def get_space(self): return self.physicser.get_space() @@ -332,16 +333,34 @@ class Bullet(GameObject): - def __init__(self, space, position, impulse): + def __init__(self, space, position, impulse, source_collision_type): body = make_body(1, pymunk.inf, position) + self.last_position = position self.shape = pymunk.Circle(body, 2) + self.shape.sensor = True self.shape.collision_type = COLLISION_TYPE_PROJECTILE + self.source_collision_type = source_collision_type super(Bullet, self).__init__( SingleShapePhysicser(space, self.shape), render.ImageRenderer(resources.get_image('objects', 'bullet.png')), ) self.physicser.apply_impulse(impulse) + def animate(self): + super(Bullet, self).animate() + position = self.physicser.position + r = self.get_space().segment_query(self.last_position, position) + self.last_position = position + for collision in r: + if collision.shape.collision_type == self.source_collision_type: + continue + if collision.shape == self.physicser.get_shape(): + continue + print "Hit", collision.shape.collision_type + self.physicser.remove_from_space() + self.remove = True + break + class CollectibleGameObject(GameObject): zorder = ZORDER_LOW diff -r 7b121ed73b95 -r 47226c661ae2 nagslang/protagonist.py --- a/nagslang/protagonist.py Thu Sep 05 23:17:02 2013 +0200 +++ b/nagslang/protagonist.py Thu Sep 05 23:26:13 2013 +0200 @@ -261,7 +261,7 @@ vec = Vec2d.unit() vec.angle = self.angle vec.length = 1000 - FireEvent.post(self.physicser.position, vec) + FireEvent.post(self.physicser.position, vec, COLLISION_TYPE_PLAYER) def in_wolf_form(self): return self.form == self.WOLF_FORM diff -r 7b121ed73b95 -r 47226c661ae2 nagslang/screens/area.py --- a/nagslang/screens/area.py Thu Sep 05 23:17:02 2013 +0200 +++ b/nagslang/screens/area.py Thu Sep 05 23:26:13 2013 +0200 @@ -145,7 +145,8 @@ # else we're teleporting within the screen, and just the # position change is enough elif FireEvent.matches(ev): - bullet = Bullet(self.space, ev.source, ev.impulse) + bullet = Bullet(self.space, ev.source, ev.impulse, + ev.source_collision_type) self._drawables.add(bullet) self.keys.handle_event(ev) @@ -196,6 +197,8 @@ self.tick_protagonist() for drawable in self._drawables: drawable.animate() + if drawable.remove: + self._drawables.remove(drawable) super(AreaScreen, self).tick(seconds)