comparison pytouhou/game/sprite.py @ 90:630e9045e851

Minor refactoring
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 10:12:15 +0200
parents 3804f07d3b0e
children 85f3b8ba3f24
comparison
equal deleted inserted replaced
89:1513f5626656 90:630e9045e851
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 from math import pi
15 16
16 from pytouhou.utils.matrix import Matrix 17 from pytouhou.utils.matrix import Matrix
17 from pytouhou.utils.interpolator import Interpolator 18 from pytouhou.utils.interpolator import Interpolator
18 19
19 20
40 class Sprite(object): 41 class Sprite(object):
41 def __init__(self): 42 def __init__(self):
42 self.anm = None 43 self.anm = None
43 self._removed = False 44 self._removed = False
44 self._changed = False 45 self._changed = False
46
47 self.width_override = 0
48 self.height_override = 0
49 self.angle = 0
50 self.force_rotation = False
45 51
46 self.scale_interpolator = None 52 self.scale_interpolator = None
47 self.fade_interpolator = None 53 self.fade_interpolator = None
48 self.offset_interpolator = None 54 self.offset_interpolator = None
49 55
88 self.offset_interpolator = Interpolator(self.dest_offset, formula) 94 self.offset_interpolator = Interpolator(self.dest_offset, formula)
89 self.offset_interpolator.set_interpolation_start(self.frame, self.dest_offset) 95 self.offset_interpolator.set_interpolation_start(self.frame, self.dest_offset)
90 self.offset_interpolator.set_interpolation_end(self.frame + duration - 1, (x, y, z)) 96 self.offset_interpolator.set_interpolation_end(self.frame + duration - 1, (x, y, z))
91 97
92 98
93 def update_vertices_uvs_colors(self, override_width=0, override_height=0, angle_base=0.): 99 def update_vertices_uvs_colors(self):
100 if not self._changed:
101 return
102
94 if self.fade_interpolator: 103 if self.fade_interpolator:
95 self.fade_interpolator.update(self.frame) 104 self.fade_interpolator.update(self.frame)
96 self.alpha = int(self.fade_interpolator.values[0]) 105 self.alpha = int(self.fade_interpolator.values[0])
97 106
98 if self.scale_interpolator: 107 if self.scale_interpolator:
101 110
102 if self.offset_interpolator: 111 if self.offset_interpolator:
103 self.offset_interpolator.update(self.frame) 112 self.offset_interpolator.update(self.frame)
104 self.dest_offset = self.offset_interpolator.values 113 self.dest_offset = self.offset_interpolator.values
105 114
106
107 vertmat = Matrix([[-.5, .5, .5, -.5], 115 vertmat = Matrix([[-.5, .5, .5, -.5],
108 [-.5, -.5, .5, .5], 116 [-.5, -.5, .5, .5],
109 [ .0, .0, .0, .0], 117 [ .0, .0, .0, .0],
110 [ 1., 1., 1., 1.]]) 118 [ 1., 1., 1., 1.]])
111 119
112 tx, ty, tw, th = self.texcoords 120 tx, ty, tw, th = self.texcoords
113 sx, sy = self.rescale 121 sx, sy = self.rescale
114 width = override_width or (tw * sx) 122 width = self.width_override or (tw * sx)
115 height = override_height or (th * sy) 123 height = self.height_override or (th * sy)
116 124
117 vertmat.scale2d(width, height) 125 vertmat.scale2d(width, height)
118 if self.mirrored: 126 if self.mirrored:
119 vertmat.flip() 127 vertmat.flip()
120 128
121 rx, ry, rz = self.rotations_3d 129 rx, ry, rz = self.rotations_3d
122 rz += angle_base 130 if self.automatic_orientation:
131 rz += pi/2. - self.angle
132 elif self.force_rotation:
133 rz += self.angle
123 134
124 if (rx, ry, rz) != (0., 0., 0.): 135 if (rx, ry, rz) != (0., 0., 0.):
125 if rx: 136 if rx:
126 vertmat.rotate_x(-rx) 137 vertmat.rotate_x(-rx)
127 if ry: 138 if ry:
143 154
144 d = vertmat.data 155 d = vertmat.data
145 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.) 156 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.)
146 self._colors = [(self.color[0], self.color[1], self.color[2], self.alpha)] * 4 157 self._colors = [(self.color[0], self.color[1], self.color[2], self.alpha)] * 4
147 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2]) 158 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2])
148 159 self._changed = False
149 self._changed = any((self.scale_interpolator, self.fade_interpolator, self.offset_interpolator))
150 160
151 161
152 def update(self): 162 def update(self, override_width=0, override_height=0, angle_base=0., force_rotation=False):
163 self._changed = (self._changed
164 or override_width != self.width_override
165 or override_height != self.height_override
166 or self.angle != angle_base
167 or self.force_rotation != force_rotation
168 or self.scale_interpolator
169 or self.fade_interpolator
170 or self.offset_interpolator)
171
172 self.width_override = override_width
173 self.height_override = override_height
174 self.angle = angle_base
175 self.force_rotation = force_rotation
176
153 if self.rotations_speed_3d != (0., 0., 0.) or self.scale_speed != (0., 0.): 177 if self.rotations_speed_3d != (0., 0., 0.) or self.scale_speed != (0., 0.):
154 ax, ay, az = self.rotations_3d 178 ax, ay, az = self.rotations_3d
155 sax, say, saz = self.rotations_speed_3d 179 sax, say, saz = self.rotations_speed_3d
156 self.rotations_3d = ax + sax, ay + say, az + saz 180 self.rotations_3d = ax + sax, ay + say, az + saz
157 self.rescale = self.rescale[0] + self.scale_speed[0], self.rescale[1] + self.scale_speed[1] 181 self.rescale = self.rescale[0] + self.scale_speed[0], self.rescale[1] + self.scale_speed[1]