changeset 90:630e9045e851

Minor refactoring
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 10:12:15 +0200
parents 1513f5626656
children f7525fa66bb0
files pytouhou/game/background.py pytouhou/game/bullet.py pytouhou/game/enemymanager.py pytouhou/game/sprite.py
diffstat 4 files changed, 40 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/game/background.py
+++ b/pytouhou/game/background.py
@@ -76,9 +76,9 @@ class Background(object):
                 sprite = Sprite()
                 anm_runner = ANMRunner(self.anm_wrapper, script_index, sprite)
                 anm_runner.run_frame()
-                sprite.update()
+                sprite.update(width_override, height_override)
                 if sprite._changed:
-                    sprite.update_vertices_uvs_colors(width_override, height_override)
+                    sprite.update_vertices_uvs_colors()
                 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices)
                 colors = sprite._colors
                 faces.append((vertices, uvs, colors))
--- a/pytouhou/game/bullet.py
+++ b/pytouhou/game/bullet.py
@@ -94,6 +94,7 @@ class Bullet(object):
 
     def get_objects_by_texture(self, objects_by_texture):
         sprite = self._sprite
+        sprite.update_vertices_uvs_colors()
         key = sprite.anm.first_name, sprite.anm.secondary_name
         key = (key, sprite.blendfunc)
         if not key in objects_by_texture:
@@ -114,10 +115,7 @@ class Bullet(object):
         if self._anmrunner and not self._anmrunner.run_frame():
             self._anmrunner = None
 
-        self._sprite.update()
-        if self._sprite._changed: #TODO
-            angle = pi/2.-self.angle if self._sprite.automatic_orientation else 0.
-            self._sprite.update_vertices_uvs_colors(angle_base=angle)
+        self._sprite.update(angle_base=self.angle)
 
         #TODO: flags
         x, y = self.x, self.y
--- a/pytouhou/game/enemymanager.py
+++ b/pytouhou/game/enemymanager.py
@@ -180,6 +180,8 @@ class Enemy(object):
         if not self._sprite:
             return
 
+        self._sprite.update_vertices_uvs_colors()
+
         key = self._sprite.anm.first_name, self._sprite.anm.secondary_name
         key = (key, self._sprite.blendfunc)
         if not key in objects_by_texture:
@@ -244,10 +246,8 @@ class Enemy(object):
             if self._sprite._removed:
                 self._sprite = None
             else:
-                self._sprite.update()
-                if self._sprite._changed or self.automatic_orientation:
-                    angle_base = self.angle if self.automatic_orientation else 0.
-                    self._sprite.update_vertices_uvs_colors(angle_base=angle_base)
+                self._sprite.update(angle_base=self.angle,
+                                    force_rotation=self.automatic_orientation)
 
 
         if self.bullet_launch_interval != 0:
--- a/pytouhou/game/sprite.py
+++ b/pytouhou/game/sprite.py
@@ -12,6 +12,7 @@
 ## GNU General Public License for more details.
 ##
 
+from math import pi
 
 from pytouhou.utils.matrix import Matrix
 from pytouhou.utils.interpolator import Interpolator
@@ -43,6 +44,11 @@ class Sprite(object):
         self._removed = False
         self._changed = False
 
+        self.width_override = 0
+        self.height_override = 0
+        self.angle = 0
+        self.force_rotation = False
+
         self.scale_interpolator = None
         self.fade_interpolator = None
         self.offset_interpolator = None
@@ -90,7 +96,10 @@ class Sprite(object):
             self.offset_interpolator.set_interpolation_end(self.frame + duration - 1, (x, y, z))
 
 
-    def update_vertices_uvs_colors(self, override_width=0, override_height=0, angle_base=0.):
+    def update_vertices_uvs_colors(self):
+        if not self._changed:
+            return
+
         if self.fade_interpolator:
             self.fade_interpolator.update(self.frame)
             self.alpha = int(self.fade_interpolator.values[0])
@@ -103,7 +112,6 @@ class Sprite(object):
             self.offset_interpolator.update(self.frame)
             self.dest_offset = self.offset_interpolator.values
 
-
         vertmat = Matrix([[-.5,     .5,     .5,    -.5],
                           [-.5,    -.5,     .5,     .5],
                           [ .0,     .0,     .0,     .0],
@@ -111,15 +119,18 @@ class Sprite(object):
 
         tx, ty, tw, th = self.texcoords
         sx, sy = self.rescale
-        width = override_width or (tw * sx)
-        height = override_height or (th * sy)
+        width = self.width_override or (tw * sx)
+        height = self.height_override or (th * sy)
 
         vertmat.scale2d(width, height)
         if self.mirrored:
             vertmat.flip()
 
         rx, ry, rz = self.rotations_3d
-        rz += angle_base
+        if self.automatic_orientation:
+            rz += pi/2. - self.angle
+        elif self.force_rotation:
+            rz += self.angle
 
         if (rx, ry, rz) != (0., 0., 0.):
             if rx:
@@ -145,11 +156,24 @@ class Sprite(object):
         assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.)
         self._colors = [(self.color[0], self.color[1], self.color[2], self.alpha)] * 4
         self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2])
-
-        self._changed = any((self.scale_interpolator, self.fade_interpolator, self.offset_interpolator))
+        self._changed = False
 
 
-    def update(self):
+    def update(self, override_width=0, override_height=0, angle_base=0., force_rotation=False):
+        self._changed = (self._changed
+                         or override_width != self.width_override
+                         or override_height != self.height_override
+                         or self.angle != angle_base
+                         or self.force_rotation != force_rotation
+                         or self.scale_interpolator
+                         or self.fade_interpolator
+                         or self.offset_interpolator)
+
+        self.width_override = override_width
+        self.height_override = override_height
+        self.angle = angle_base
+        self.force_rotation = force_rotation
+
         if self.rotations_speed_3d != (0., 0., 0.) or self.scale_speed != (0., 0.):
             ax, ay, az = self.rotations_3d
             sax, say, saz = self.rotations_speed_3d