comparison pytouhou/game/sprite.py @ 71:a03d7a94b997

Add support for a few ANM instructions
author Thibaut Girka <thib@sitedethib.com>
date Sat, 27 Aug 2011 20:42:16 +0200
parents a142e57218a0
children 6a08f44fa01b
comparison
equal deleted inserted replaced
70:7c1f20407b3e 71:a03d7a94b997
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 15
16 from pytouhou.utils.matrix import Matrix 16 from pytouhou.utils.matrix import Matrix
17 from pytouhou.utils.interpolator import Interpolator
17 18
18 19
19 class AnmWrapper(object): 20 class AnmWrapper(object):
20 def __init__(self, anm_files): 21 def __init__(self, anm_files):
21 self.anm_files = list(anm_files) 22 self.anm_files = list(anm_files)
40 def __init__(self): 41 def __init__(self):
41 self.anm = None 42 self.anm = None
42 self._removed = False 43 self._removed = False
43 self._changed = False 44 self._changed = False
44 45
46 self.scale_interpolator = None
47 self.fade_interpolator = None
48 self.offset_interpolator = None
49
45 self.texcoords = (0, 0, 0, 0) # x, y, width, height 50 self.texcoords = (0, 0, 0, 0) # x, y, width, height
51 self.dest_offset = (0., 0., 0.)
52 self.allow_dest_offset = False
46 self.texoffsets = (0., 0.) 53 self.texoffsets = (0., 0.)
47 self.mirrored = False 54 self.mirrored = False
48 self.rescale = (1., 1.) 55 self.rescale = (1., 1.)
49 self.scale_speed = (0., 0.) 56 self.scale_speed = (0., 0.)
50 self.rotations_3d = (0., 0., 0.) 57 self.rotations_3d = (0., 0., 0.)
56 self._uvs = [] 63 self._uvs = []
57 self._vertices = [] 64 self._vertices = []
58 self._colors = [] 65 self._colors = []
59 66
60 67
68 def fade(self, duration, alpha, formula):
69 if not self.fade_interpolator:
70 self.fade_interpolator = Interpolator((self.alpha,), formula)
71 self.fade_interpolator.set_interpolation_start(self.frame, (self.alpha,))
72 self.fade_interpolator.set_interpolation_end(self.frame + duration - 1, (alpha,))
73
74
75 def scale_in(self, duration, sx, sy, formula):
76 if not self.scale_interpolator:
77 self.scale_interpolator = Interpolator(self.rescale, formula)
78 self.scale_interpolator.set_interpolation_start(self.frame, self.rescale)
79 self.scale_interpolator.set_interpolation_end(self.frame + duration - 1, (sx, sy))
80
81
82 def move_in(self, duration, x, y, z, formula):
83 if not self.offset_interpolator:
84 self.offset_interpolator = Interpolator(self.dest_offset, formula)
85 self.offset_interpolator.set_interpolation_start(self.frame, self.dest_offset)
86 self.offset_interpolator.set_interpolation_end(self.frame + duration - 1, (x, y, z))
87
88
61 def update_vertices_uvs_colors(self, override_width=0, override_height=0): 89 def update_vertices_uvs_colors(self, override_width=0, override_height=0):
90 if self.fade_interpolator:
91 self.fade_interpolator.update(self.frame)
92 self.alpha = int(self.fade_interpolator.values[0])
93
94 if self.scale_interpolator:
95 self.scale_interpolator.update(self.frame)
96 self.rescale = self.scale_interpolator.values
97
98 if self.offset_interpolator:
99 self.offset_interpolator.update(self.frame)
100 self.dest_offset = self.offset_interpolator.values
101
102
62 vertmat = Matrix([[-.5, .5, .5, -.5], 103 vertmat = Matrix([[-.5, .5, .5, -.5],
63 [-.5, -.5, .5, .5], 104 [-.5, -.5, .5, .5],
64 [ .0, .0, .0, .0], 105 [ .0, .0, .0, .0],
65 [ 1., 1., 1., 1.]]) 106 [ 1., 1., 1., 1.]])
66 107
80 vertmat.rotate_y(ry) 121 vertmat.rotate_y(ry)
81 if rz: 122 if rz:
82 vertmat.rotate_z(-rz) #TODO: minus, really? 123 vertmat.rotate_z(-rz) #TODO: minus, really?
83 if self.corner_relative_placement: # Reposition 124 if self.corner_relative_placement: # Reposition
84 vertmat.translate(width / 2., height / 2., 0.) 125 vertmat.translate(width / 2., height / 2., 0.)
126 if self.allow_dest_offset:
127 vertmat.translate(*self.dest_offset)
85 128
86 x_1 = 1. / self.anm.size[0] 129 x_1 = 1. / self.anm.size[0]
87 y_1 = 1. / self.anm.size[1] 130 y_1 = 1. / self.anm.size[1]
88 tox, toy = self.texoffsets 131 tox, toy = self.texoffsets
89 uvs = [(tx * x_1 + tox, 1. - (ty * y_1) + toy), 132 uvs = [(tx * x_1 + tox, 1. - (ty * y_1) + toy),
102 ax, ay, az = self.rotations_3d 145 ax, ay, az = self.rotations_3d
103 sax, say, saz = self.rotations_speed_3d 146 sax, say, saz = self.rotations_speed_3d
104 self.rotations_3d = ax + sax, ay + say, az + saz 147 self.rotations_3d = ax + sax, ay + say, az + saz
105 self.rescale = self.rescale[0] + self.scale_speed[0], self.rescale[1] + self.scale_speed[1] 148 self.rescale = self.rescale[0] + self.scale_speed[0], self.rescale[1] + self.scale_speed[1]
106 self._changed = True 149 self._changed = True
150 self.frame += 1
107 151