changeset 293:47226c661ae2

Bullets that mostly die when they hit things
author Stefano Rivera <stefano@rivera.za.net>
date Thu, 05 Sep 2013 23:26:13 +0200
parents 7b121ed73b95
children a2120ba50eb6
files nagslang/events.py nagslang/game_object.py nagslang/protagonist.py nagslang/screens/area.py
diffstat 4 files changed, 28 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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
--- 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
--- 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)