changeset 258:620134bc51f4

Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
author Thibaut Girka <thib@sitedethib.com>
date Sun, 22 Jan 2012 22:03:57 +0100
parents 9b699e8de4a7
children 3631abb6e95b
files pytouhou/game/bullet.pyx pytouhou/game/game.py
diffstat 2 files changed, 16 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/game/bullet.pyx
+++ b/pytouhou/game/bullet.pyx
@@ -90,7 +90,7 @@ cdef class Bullet(object):
             self._sprite.angle = angle
 
 
-    def is_visible(self, screen_width, screen_height):
+    cpdef is_visible(Bullet self, screen_width, screen_height):
         tx, ty, tw, th = self._sprite.texcoords
         x, y = self.x, self.y
 
@@ -105,7 +105,7 @@ cdef class Bullet(object):
         return True
 
 
-    def set_anim(self, sprite_idx_offset=None):
+    def set_anim(Bullet self, sprite_idx_offset=None):
         if sprite_idx_offset is not None:
             self.sprite_idx_offset = sprite_idx_offset
 
@@ -120,7 +120,7 @@ cdef class Bullet(object):
         self._anmrunner.run_frame()
 
 
-    def launch(self):
+    def launch(Bullet self):
         self._state = LAUNCHED
         self.frame = 0
         self.set_anim()
@@ -130,11 +130,11 @@ cdef class Bullet(object):
                                                    (self.speed,), 16)
 
 
-    def collide(self):
+    def collide(Bullet self):
         self.cancel()
 
 
-    def cancel(self):
+    def cancel(Bullet self):
         # Cancel animation
         bt = self._bullet_type
         self._sprite = Sprite()
@@ -158,7 +158,7 @@ cdef class Bullet(object):
         self._game.cancelled_bullets.append(self)
 
 
-    def update(self):
+    def update(Bullet self):
         if self._anmrunner is not None and not self._anmrunner.run_frame():
             if self._state == LAUNCHING:
                 #TODO: check if it doesn't skip a frame
@@ -248,3 +248,13 @@ cdef class Bullet(object):
 
         self.frame += 1
 
+        # Filter out-of-screen bullets and handle special flags
+        #TODO: flags 1024 and 2048
+        if self.flags & 448:
+            self._was_visible = False
+        elif self.is_visible(self._game.width, self._game.height):
+            self._was_visible = True
+        elif self._was_visible:
+            # Filter out-of-screen bullets
+            self._removed = True
+
--- a/pytouhou/game/game.py
+++ b/pytouhou/game/game.py
@@ -266,16 +266,6 @@ class Game(object):
         self.enemies = [enemy for enemy in self.enemies if not enemy._removed]
 
         # Filter out-of-scren bullets
-        # TODO: move to Bullet?
-        for bullet in chain(self.bullets, self.players_bullets):
-            if bullet.flags & 448:
-                bullet._was_visible = False
-            elif bullet.is_visible(self.width, self.height):
-                bullet._was_visible = True
-            elif bullet._was_visible:
-                # Filter out-of-screen bullets
-                bullet._removed = True
-
         self.bullets = [bullet for bullet in self.bullets
                             if not bullet._removed]
         self.players_bullets = [bullet for bullet in self.players_bullets