changeset 29:afa91be769ae

Don't lose time updating off-screen enemies' sprites
author Thibaut Girka <thib@sitedethib.com>
date Fri, 12 Aug 2011 22:03:34 +0200
parents f405b947624d
children e3ba2fa966f6
files pytouhou/game/background.py pytouhou/game/enemymanager.py pytouhou/game/sprite.py
diffstat 3 files changed, 19 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/game/background.py
+++ b/pytouhou/game/background.py
@@ -55,7 +55,8 @@ class Background(object):
             for script_index, ox, oy, oz, width_override, height_override in obj.quads:
                 #TODO: per-texture rendering
                 anm, sprite = self.anm_wrapper.get_sprite(script_index)
-                sprite.update(width_override, height_override)
+                if sprite.update():
+                    sprite.update_uvs_vertices(width_override, height_override)
                 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices)
                 faces.append((vertices, uvs))
             self.objects.append(faces)
--- a/pytouhou/game/enemymanager.py
+++ b/pytouhou/game/enemymanager.py
@@ -95,13 +95,20 @@ class Enemy(object):
     def is_visible(self, screen_width, screen_height):
         if not self.sprite:
             return False
-        if min(x for x, y, z in self.sprite._vertices) >= screen_width - self.x:
-            return False
-        if max(x for x, y, z in self.sprite._vertices) <= -self.x:
-            return False
-        if min(y for x, y, z in self.sprite._vertices) >= screen_height - self.y:
-            return False
-        if max(y for x, y, z in self.sprite._vertices) <= -self.y:
+
+        tx, ty, tw, th = self.sprite.texcoords
+        if self.sprite.corner_relative_placement:
+            raise Exception #TODO
+        else:
+            max_x = tw / 2.
+            max_y = th / 2.
+            min_x = -max_x
+            min_y = -max_y
+
+        if any((min_x >= screen_width - self.x,
+                max_x <= -self.x,
+                min_y >= screen_height - self.y,
+                max_y <= -self.y)):
             return False
         return True
 
@@ -146,7 +153,8 @@ class Enemy(object):
 
         self.x, self.y = x, y
         if self.sprite:
-            self.sprite.update()
+            if self.sprite.update() and self.is_visible(384, 448): #TODO: screen size
+                self.sprite.update_uvs_vertices()
 
         self.frame += 1
         return True
@@ -179,7 +187,6 @@ class EnemyManager(object):
                     enemy = Enemy((x, y), life, instr_type, ecl_runner, self.anm_wrapper)
 
                     def _enemy_deleter(unknown): #TOOD: unknown
-                        print('youhou!')
                         self.enemies.remove(enemy)
 
                     ecl_runner.implementation[1] = ('I', _enemy_deleter)
--- a/pytouhou/game/sprite.py
+++ b/pytouhou/game/sprite.py
@@ -70,7 +70,7 @@ class Sprite(object):
 
 
 
-    def update(self, override_width=0, override_height=0):
+    def update(self):
         properties = {}
         for time, instr_type, data in self.anm.scripts[self.script_index]:
             if time == self.frame:
@@ -106,10 +106,8 @@ class Sprite(object):
                 del properties[23]
             if properties:
                 print('Leftover properties: %r' % properties) #TODO
-            self.update_uvs_vertices(override_width, override_height)
             return True
         if self.rotations_speed_3d != (0., 0., 0.):
-            self.update_uvs_vertices(override_width, override_height)
             return True
         return False