Mercurial > touhou
comparison pytouhou/game/sprite.py @ 69:a142e57218a0
Refactor. Move VMs to pytouhou.vm.
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sat, 27 Aug 2011 10:58:54 +0200 |
parents | 0886994029e4 |
children | a03d7a94b997 |
comparison
equal
deleted
inserted
replaced
68:a2459defd4b6 | 69:a142e57218a0 |
---|---|
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 | |
17 from struct import unpack | |
18 | |
19 from pytouhou.utils.matrix import Matrix | 16 from pytouhou.utils.matrix import Matrix |
20 from pytouhou.utils.helpers import get_logger | |
21 | |
22 logger = get_logger(__name__) | |
23 | 17 |
24 | 18 |
25 class AnmWrapper(object): | 19 class AnmWrapper(object): |
26 def __init__(self, anm_files): | 20 def __init__(self, anm_files): |
27 self.anm_files = list(anm_files) | 21 self.anm_files = list(anm_files) |
28 | 22 |
29 def get_sprite(self, script_index): | 23 |
24 def get_sprite(self, sprite_index): | |
25 for anm in self.anm_files: | |
26 if sprite_index in anm.sprites: | |
27 return anm, anm.sprites[sprite_index] | |
28 raise IndexError | |
29 | |
30 | |
31 def get_script(self, script_index): | |
30 for anm in self.anm_files: | 32 for anm in self.anm_files: |
31 if script_index in anm.scripts: | 33 if script_index in anm.scripts: |
32 return anm, Sprite(anm, script_index) | 34 return anm, anm.scripts[script_index] |
35 raise IndexError | |
33 | 36 |
34 | 37 |
35 | 38 |
36 class Sprite(object): | 39 class Sprite(object): |
37 def __init__(self, anm, script_index): | 40 def __init__(self): |
38 self.anm = anm | 41 self.anm = None |
39 self.script_index = script_index | 42 self._removed = False |
43 self._changed = False | |
44 | |
40 self.texcoords = (0, 0, 0, 0) # x, y, width, height | 45 self.texcoords = (0, 0, 0, 0) # x, y, width, height |
41 self.texoffsets = (0., 0.) | 46 self.texoffsets = (0., 0.) |
42 self.mirrored = False | 47 self.mirrored = False |
43 self.rescale = (1., 1.) | 48 self.rescale = (1., 1.) |
44 self.scale_speed = (0., 0.) | 49 self.scale_speed = (0., 0.) |
45 self.rotations_3d = (0., 0., 0.) | 50 self.rotations_3d = (0., 0., 0.) |
46 self.rotations_speed_3d = (0., 0., 0.) | 51 self.rotations_speed_3d = (0., 0., 0.) |
47 self.corner_relative_placement = False | 52 self.corner_relative_placement = False |
48 self.instruction_pointer = 0 | |
49 self.keep_still = False | |
50 self.playing = True | |
51 self.frame = 0 | 53 self.frame = 0 |
52 self.color = (255, 255, 255) | 54 self.color = (255, 255, 255) |
53 self.alpha = 255 | 55 self.alpha = 255 |
54 self._uvs = [] | 56 self._uvs = [] |
55 self._vertices = [] | 57 self._vertices = [] |
93 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.) | 95 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.) |
94 self._colors = [(self.color[0], self.color[1], self.color[2], self.alpha)] * 4 | 96 self._colors = [(self.color[0], self.color[1], self.color[2], self.alpha)] * 4 |
95 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2]) | 97 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2]) |
96 | 98 |
97 | 99 |
100 def update(self): | |
101 if self.rotations_speed_3d != (0., 0., 0.) or self.scale_speed != (0., 0.): | |
102 ax, ay, az = self.rotations_3d | |
103 sax, say, saz = self.rotations_speed_3d | |
104 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] | |
106 self._changed = True | |
98 | 107 |
99 def update(self): | |
100 if not self.playing: | |
101 return False | |
102 | |
103 changed = False | |
104 if not self.keep_still: | |
105 script = self.anm.scripts[self.script_index] | |
106 frame = self.frame | |
107 while True: | |
108 try: | |
109 frame, instr_type, args = script[self.instruction_pointer] | |
110 except IndexError: | |
111 self.playing = False | |
112 return False | |
113 | |
114 if frame > self.frame: | |
115 break | |
116 else: | |
117 self.instruction_pointer += 1 | |
118 if frame == self.frame: | |
119 changed = True | |
120 if instr_type == 0: | |
121 self.playing = False | |
122 return False | |
123 if instr_type == 1: | |
124 #TODO: handle out-of-anm sprites | |
125 self.texcoords = self.anm.sprites[args[0]] | |
126 elif instr_type == 2: | |
127 self.rescale = args | |
128 elif instr_type == 3: | |
129 self.alpha = args[0] % 256 #TODO | |
130 elif instr_type == 4: | |
131 b, g, r = args | |
132 self.color = (r, g, b) | |
133 elif instr_type == 5: | |
134 self.instruction_pointer, = args | |
135 self.frame = script[self.instruction_pointer][0] | |
136 elif instr_type == 7: | |
137 self.mirrored = not self.mirrored | |
138 elif instr_type == 9: | |
139 self.rotations_3d = args | |
140 elif instr_type == 10: | |
141 self.rotations_speed_3d = args | |
142 elif instr_type == 11: | |
143 self.scale_speed = args | |
144 elif instr_type == 16: | |
145 #TODO: handle out-of-anm sprites | |
146 #TODO: use the game's PRNG? | |
147 self.texcoords = self.anm.sprites[args[0] + randrange(args[1])] | |
148 elif instr_type == 23: | |
149 self.corner_relative_placement = True #TODO | |
150 elif instr_type == 27: | |
151 tox, toy = self.texoffsets | |
152 self.texoffsets = tox + args[0], toy | |
153 elif instr_type == 28: | |
154 tox, toy = self.texoffsets | |
155 self.texoffsets = tox, toy + args[0] | |
156 elif instr_type in (15, 21): | |
157 self.keep_still = True | |
158 break | |
159 else: | |
160 logger.warn('unhandled instruction %d (args: %r)', instr_type, args) | |
161 self.frame += 1 | |
162 | |
163 ax, ay, az = self.rotations_3d | |
164 sax, say, saz = self.rotations_speed_3d | |
165 self.rotations_3d = ax + sax, ay + say, az + saz | |
166 self.rescale = self.rescale[0] + self.scale_speed[0], self.rescale[1] + self.scale_speed[1] | |
167 | |
168 if self.rotations_speed_3d != (0., 0., 0.) or self.scale_speed != (0., 0.): | |
169 return True | |
170 | |
171 return changed | |
172 |