Mercurial > touhou
annotate pytouhou/game/sprite.py @ 41:93c8dc2de923
Add (hopefully) "texture shifting" in animations
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 15 Aug 2011 00:41:51 +0200 |
parents | cb5b27011044 |
children | ab826bc29aa2 |
rev | line source |
---|---|
15 | 1 from struct import unpack |
2 | |
3 from pytouhou.utils.matrix import Matrix | |
4 | |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
5 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
6 class AnmWrapper(object): |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
7 def __init__(self, anm_files): |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
8 self.anm_files = list(anm_files) |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
9 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
10 def get_sprite(self, script_index): |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
11 for anm in self.anm_files: |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
12 if script_index in anm.scripts: |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
13 return anm, Sprite(anm, script_index) |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
14 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
15 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
16 |
15 | 17 class Sprite(object): |
18 def __init__(self, anm, script_index): | |
19 self.anm = anm | |
20 self.script_index = script_index | |
21 self.texcoords = (0, 0, 0, 0) # x, y, width, height | |
41
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
22 self.texoffsets = (0., 0.) |
15 | 23 self.mirrored = False |
24 self.rescale = (1., 1.) | |
25 self.rotations_3d = (0., 0., 0.) | |
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
26 self.rotations_speed_3d = (0., 0., 0.) |
15 | 27 self.corner_relative_placement = False |
30 | 28 self.instruction_pointer = 0 |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
29 self.keep_still = False |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
30 self.playing = True |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
31 self.frame = 0 |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
32 self.alpha = 255 |
15 | 33 self._uvs = [] |
34 self._vertices = [] | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
35 self._colors = [] |
15 | 36 |
37 | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
38 def update_vertices_uvs_colors(self, override_width=0, override_height=0): |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
39 vertmat = Matrix([[-.5, .5, .5, -.5], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
40 [-.5, -.5, .5, .5], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
41 [ .0, .0, .0, .0], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
42 [ 1., 1., 1., 1.]]) |
15 | 43 |
44 tx, ty, tw, th = self.texcoords | |
45 sx, sy = self.rescale | |
46 width = override_width or (tw * sx) | |
47 height = override_height or (th * sy) | |
48 | |
31 | 49 vertmat.scale2d(width, height) |
15 | 50 if self.mirrored: |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
51 vertmat.flip() |
15 | 52 if self.rotations_3d != (0., 0., 0.): |
53 rx, ry, rz = self.rotations_3d | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
54 if rx: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
55 vertmat.rotate_x(-rx) |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
56 if ry: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
57 vertmat.rotate_y(ry) |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
58 if rz: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
59 vertmat.rotate_z(-rz) #TODO: minus, really? |
15 | 60 if self.corner_relative_placement: # Reposition |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
61 vertmat.translate(width / 2., height / 2., 0.) |
15 | 62 |
30 | 63 x_1 = 1. / self.anm.size[0] |
64 y_1 = 1. / self.anm.size[1] | |
41
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
65 tox, toy = self.texoffsets |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
66 uvs = [(tx * x_1 + tox, 1. - (ty * y_1) + toy), |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
67 ((tx + tw) * x_1 + tox, 1. - (ty * y_1) + toy), |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
68 ((tx + tw) * x_1 + tox, 1. - ((ty + th) * y_1 + toy)), |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
69 (tx * x_1 + tox, 1. - ((ty + th) * y_1 + toy))] |
15 | 70 |
30 | 71 d = vertmat.data |
72 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.) | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
73 self._colors = [(255, 255, 255, self.alpha)] * 4 |
30 | 74 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2]) |
15 | 75 |
76 | |
77 | |
29
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
28
diff
changeset
|
78 def update(self): |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
79 if not self.playing: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
80 return False |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
81 |
30 | 82 changed = False |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
83 if not self.keep_still: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
84 script = self.anm.scripts[self.script_index] |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
85 try: |
41
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
86 frame = self.frame |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
87 while frame <= self.frame: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
88 frame, instr_type, args = script[self.instruction_pointer] |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
89 if frame == self.frame: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
90 changed = True |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
91 if instr_type == 0: |
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
92 self.playing = False |
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
93 return False |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
94 if instr_type == 1: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
95 self.texcoords = self.anm.sprites[args[0]] |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
96 elif instr_type == 2: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
97 self.rescale = args |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
98 elif instr_type == 3: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
99 self.alpha = args[0] % 256 #TODO |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
100 elif instr_type == 5: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
101 self.instruction_pointer, = args |
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
102 self.frame = script[self.instruction_pointer][0] |
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
103 continue |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
104 elif instr_type == 7: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
105 self.mirrored = True |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
106 elif instr_type == 9: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
107 self.rotations_3d = args |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
108 elif instr_type == 10: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
109 self.rotations_speed_3d = args |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
110 elif instr_type == 23: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
111 self.corner_relative_placement = True #TODO |
41
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
112 elif instr_type == 27: |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
113 tox, toy = self.texoffsets |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
114 self.texoffsets = tox + args[0], toy |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
115 elif instr_type == 28: |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
116 tox, toy = self.texoffsets |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
117 self.texoffsets = tox, toy + args[0] |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
118 elif instr_type == 15: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
119 self.keep_still = True |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
120 break |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
121 else: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
122 print('unhandled opcode: %d, %r' % (instr_type, args)) #TODO |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
123 if frame <= self.frame: |
30 | 124 self.instruction_pointer += 1 |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
125 except IndexError: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
126 self.playing = False |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
127 pass |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
128 self.frame += 1 |
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
129 |
30 | 130 ax, ay, az = self.rotations_3d |
131 sax, say, saz = self.rotations_speed_3d | |
132 self.rotations_3d = ax + sax, ay + say, az + saz | |
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
133 |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
134 if self.rotations_speed_3d != (0., 0., 0.): |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
135 return True |
15 | 136 |
30 | 137 return changed |
138 |