comparison pytouhou/game/sprite.py @ 57:694f25881d0f

Fix move_to interpolation, add support for a few ANM and ECL instructions
author Thibaut Girka <thib@sitedethib.com>
date Tue, 23 Aug 2011 19:27:24 +0200
parents b383b09bc735
children 3da4de9decd0
comparison
equal deleted inserted replaced
56:299de3a9b69f 57:694f25881d0f
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 15
16 from random import randrange
16 from struct import unpack 17 from struct import unpack
17 18
18 from pytouhou.utils.matrix import Matrix 19 from pytouhou.utils.matrix import Matrix
19 20
20 21
35 self.script_index = script_index 36 self.script_index = script_index
36 self.texcoords = (0, 0, 0, 0) # x, y, width, height 37 self.texcoords = (0, 0, 0, 0) # x, y, width, height
37 self.texoffsets = (0., 0.) 38 self.texoffsets = (0., 0.)
38 self.mirrored = False 39 self.mirrored = False
39 self.rescale = (1., 1.) 40 self.rescale = (1., 1.)
41 self.scale_speed = (0., 0.)
40 self.rotations_3d = (0., 0., 0.) 42 self.rotations_3d = (0., 0., 0.)
41 self.rotations_speed_3d = (0., 0., 0.) 43 self.rotations_speed_3d = (0., 0., 0.)
42 self.corner_relative_placement = False 44 self.corner_relative_placement = False
43 self.instruction_pointer = 0 45 self.instruction_pointer = 0
44 self.keep_still = False 46 self.keep_still = False
45 self.playing = True 47 self.playing = True
46 self.frame = 0 48 self.frame = 0
49 self.color = (255, 255, 255)
47 self.alpha = 255 50 self.alpha = 255
48 self._uvs = [] 51 self._uvs = []
49 self._vertices = [] 52 self._vertices = []
50 self._colors = [] 53 self._colors = []
51 54
83 ((tx + tw) * x_1 + tox, 1. - ((ty + th) * y_1 + toy)), 86 ((tx + tw) * x_1 + tox, 1. - ((ty + th) * y_1 + toy)),
84 (tx * x_1 + tox, 1. - ((ty + th) * y_1 + toy))] 87 (tx * x_1 + tox, 1. - ((ty + th) * y_1 + toy))]
85 88
86 d = vertmat.data 89 d = vertmat.data
87 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.) 90 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.)
88 self._colors = [(255, 255, 255, self.alpha)] * 4 91 self._colors = [(self.color[0], self.color[1], self.color[2], self.alpha)] * 4
89 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2]) 92 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2])
90 93
91 94
92 95
93 def update(self): 96 def update(self):
113 changed = True 116 changed = True
114 if instr_type == 0: 117 if instr_type == 0:
115 self.playing = False 118 self.playing = False
116 return False 119 return False
117 if instr_type == 1: 120 if instr_type == 1:
121 #TODO: handle out-of-anm sprites
118 self.texcoords = self.anm.sprites[args[0]] 122 self.texcoords = self.anm.sprites[args[0]]
119 elif instr_type == 2: 123 elif instr_type == 2:
120 self.rescale = args 124 self.rescale = args
121 elif instr_type == 3: 125 elif instr_type == 3:
122 self.alpha = args[0] % 256 #TODO 126 self.alpha = args[0] % 256 #TODO
127 elif instr_type == 4:
128 b, g, r = args
129 self.color = (r, g, b)
123 elif instr_type == 5: 130 elif instr_type == 5:
124 self.instruction_pointer, = args 131 self.instruction_pointer, = args
125 self.frame = script[self.instruction_pointer][0] 132 self.frame = script[self.instruction_pointer][0]
126 elif instr_type == 7: 133 elif instr_type == 7:
127 self.mirrored = True 134 self.mirrored = True
128 elif instr_type == 9: 135 elif instr_type == 9:
129 self.rotations_3d = args 136 self.rotations_3d = args
130 elif instr_type == 10: 137 elif instr_type == 10:
131 self.rotations_speed_3d = args 138 self.rotations_speed_3d = args
139 elif instr_type == 11:
140 self.scale_speed = args
141 elif instr_type == 16:
142 #TODO: handle out-of-anm sprites
143 #TODO: use the game's PRNG?
144 self.texcoords = self.anm.sprites[args[0] + randrange(args[1])]
132 elif instr_type == 23: 145 elif instr_type == 23:
133 self.corner_relative_placement = True #TODO 146 self.corner_relative_placement = True #TODO
134 elif instr_type == 27: 147 elif instr_type == 27:
135 tox, toy = self.texoffsets 148 tox, toy = self.texoffsets
136 self.texoffsets = tox + args[0], toy 149 self.texoffsets = tox + args[0], toy
137 elif instr_type == 28: 150 elif instr_type == 28:
138 tox, toy = self.texoffsets 151 tox, toy = self.texoffsets
139 self.texoffsets = tox, toy + args[0] 152 self.texoffsets = tox, toy + args[0]
140 elif instr_type == 15: 153 elif instr_type in (15, 21):
141 self.keep_still = True 154 self.keep_still = True
142 break 155 break
143 else: 156 else:
144 print('unhandled opcode: %d, %r' % (instr_type, args)) #TODO 157 print('unhandled opcode: %d, %r' % (instr_type, args)) #TODO
145 self.frame += 1 158 self.frame += 1
146 159
147 ax, ay, az = self.rotations_3d 160 ax, ay, az = self.rotations_3d
148 sax, say, saz = self.rotations_speed_3d 161 sax, say, saz = self.rotations_speed_3d
149 self.rotations_3d = ax + sax, ay + say, az + saz 162 self.rotations_3d = ax + sax, ay + say, az + saz
163 self.rescale = self.rescale[0] + self.scale_speed[0], self.rescale[1] + self.scale_speed[1]
150 164
151 if self.rotations_speed_3d != (0., 0., 0.): 165 if self.rotations_speed_3d != (0., 0., 0.) or self.scale_speed != (0., 0.):
152 return True 166 return True
153 167
154 return changed 168 return changed
155 169