changeset 161:7e7368356445

Add bullet cancel anim support
author Thibaut Girka <thib@sitedethib.com>
date Tue, 11 Oct 2011 11:29:33 +0200
parents 606468ab4f7b
children 959c8b312918
files pytouhou/game/bullet.py pytouhou/game/game.py pytouhou/opengl/gamerenderer.pyx
diffstat 3 files changed, 34 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/game/bullet.py
+++ b/pytouhou/game/bullet.py
@@ -108,8 +108,24 @@ class Bullet(object):
 
 
     def collide(self):
-        #TODO: animation
-        self._removed = True
+        self.cancel()
+
+
+    def cancel(self):
+        # Cancel animation
+        bt = self._bullet_type
+        self._sprite = Sprite()
+        self._sprite.angle = self.angle
+        self._anmrunner = ANMRunner(bt.anm_wrapper, bt.cancel_anim_index,
+                                    self._sprite, bt.launch_anim_offsets[self.sprite_idx_offset])
+        self._anmrunner.run_frame()
+
+        # Change update method
+        self.update = self.update_cancel
+
+        # Do not use this one for collisions anymore
+        self._game.bullets.remove(self)
+        self._game.cancelled_bullets.append(self)
 
 
     def update(self):
@@ -121,6 +137,15 @@ class Bullet(object):
             self.launch()
 
 
+    def update_cancel(self):
+        dx, dy = self.delta
+        self.x += dx
+        self.y += dy
+
+        if not self._anmrunner.run_frame():
+            self._removed = True
+
+
     def update_simple(self):
         dx, dy = self.delta
         self.x += dx
--- a/pytouhou/game/game.py
+++ b/pytouhou/game/game.py
@@ -36,6 +36,7 @@ class Game(object):
         self.players = [Player(player_state, characters[player_state.character]) for player_state in player_states]
         self.enemies = []
         self.bullets = []
+        self.cancelled_bullets = []
         self.items = []
 
         self.stage = stage
@@ -78,6 +79,7 @@ class Game(object):
         # 2. Filter out destroyed enemies
         self.enemies = [enemy for enemy in self.enemies if not enemy._removed]
         self.bullets = [bullet for bullet in self.bullets if not bullet._removed]
+        self.cancelled_bullets = [bullet for bullet in self.cancelled_bullets if not bullet._removed]
         self.items = [item for item in self.items if not item._removed]
 
         # 3. Let's play!
@@ -99,6 +101,9 @@ class Game(object):
         for bullet in self.bullets:
             bullet.update()
 
+        for bullet in self.cancelled_bullets:
+            bullet.update()
+
         for item in self.items:
             item.update()
 
@@ -161,6 +166,7 @@ class Game(object):
         # Filter out-of-scren bullets
         # TODO: was_visible thing
         self.bullets = [bullet for bullet in self.bullets if bullet.is_visible(384, 448)]
+        self.cancelled_bullets = [bullet for bullet in self.cancelled_bullets if bullet.is_visible(384, 448)]
 
         # Disable boss mode if it is dead/it has timeout
         if self.boss and self.boss._removed:
--- a/pytouhou/opengl/gamerenderer.pyx
+++ b/pytouhou/opengl/gamerenderer.pyx
@@ -152,6 +152,7 @@ cdef class GameRenderer:
             self.render_elements(game.enemies)
             self.render_elements(game.players)
             self.render_elements(game.bullets)
+            self.render_elements(game.cancelled_bullets)
             self.render_elements(game.items)
             #TODO: display item indicators
             glEnable(GL_FOG)