Mercurial > touhou
annotate pytouhou/game/sprite.py @ 54:b383b09bc735
Fix anim cycling
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 22 Aug 2011 23:43:40 +0200 |
parents | ab826bc29aa2 |
children | 694f25881d0f |
rev | line source |
---|---|
52
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
2 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
4 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
8 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
12 ## GNU General Public License for more details. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
13 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
14 |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
41
diff
changeset
|
15 |
15 | 16 from struct import unpack |
17 | |
18 from pytouhou.utils.matrix import Matrix | |
19 | |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
20 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
21 class AnmWrapper(object): |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
22 def __init__(self, anm_files): |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
23 self.anm_files = list(anm_files) |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
24 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
25 def get_sprite(self, script_index): |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
26 for anm in self.anm_files: |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
27 if script_index in anm.scripts: |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
28 return anm, Sprite(anm, script_index) |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
29 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
30 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
31 |
15 | 32 class Sprite(object): |
33 def __init__(self, anm, script_index): | |
34 self.anm = anm | |
35 self.script_index = script_index | |
36 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
|
37 self.texoffsets = (0., 0.) |
15 | 38 self.mirrored = False |
39 self.rescale = (1., 1.) | |
40 self.rotations_3d = (0., 0., 0.) | |
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
41 self.rotations_speed_3d = (0., 0., 0.) |
15 | 42 self.corner_relative_placement = False |
30 | 43 self.instruction_pointer = 0 |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
44 self.keep_still = False |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
45 self.playing = True |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
46 self.frame = 0 |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
47 self.alpha = 255 |
15 | 48 self._uvs = [] |
49 self._vertices = [] | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
50 self._colors = [] |
15 | 51 |
52 | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
53 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
|
54 vertmat = Matrix([[-.5, .5, .5, -.5], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
55 [-.5, -.5, .5, .5], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
56 [ .0, .0, .0, .0], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
57 [ 1., 1., 1., 1.]]) |
15 | 58 |
59 tx, ty, tw, th = self.texcoords | |
60 sx, sy = self.rescale | |
61 width = override_width or (tw * sx) | |
62 height = override_height or (th * sy) | |
63 | |
31 | 64 vertmat.scale2d(width, height) |
15 | 65 if self.mirrored: |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
66 vertmat.flip() |
15 | 67 if self.rotations_3d != (0., 0., 0.): |
68 rx, ry, rz = self.rotations_3d | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
69 if rx: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
70 vertmat.rotate_x(-rx) |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
71 if ry: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
72 vertmat.rotate_y(ry) |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
73 if rz: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
74 vertmat.rotate_z(-rz) #TODO: minus, really? |
15 | 75 if self.corner_relative_placement: # Reposition |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
76 vertmat.translate(width / 2., height / 2., 0.) |
15 | 77 |
30 | 78 x_1 = 1. / self.anm.size[0] |
79 y_1 = 1. / self.anm.size[1] | |
41
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
80 tox, toy = self.texoffsets |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
81 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
|
82 ((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
|
83 ((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
|
84 (tx * x_1 + tox, 1. - ((ty + th) * y_1 + toy))] |
15 | 85 |
30 | 86 d = vertmat.data |
87 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
|
88 self._colors = [(255, 255, 255, self.alpha)] * 4 |
30 | 89 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2]) |
15 | 90 |
91 | |
92 | |
29
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
28
diff
changeset
|
93 def update(self): |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
94 if not self.playing: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
95 return False |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
96 |
30 | 97 changed = False |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
98 if not self.keep_still: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
99 script = self.anm.scripts[self.script_index] |
54 | 100 frame = self.frame |
101 while True: | |
102 try: | |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
103 frame, instr_type, args = script[self.instruction_pointer] |
54 | 104 except IndexError: |
105 self.playing = False | |
106 return False | |
107 | |
108 if frame > self.frame: | |
109 break | |
110 else: | |
111 self.instruction_pointer += 1 | |
112 if frame == self.frame: | |
113 changed = True | |
114 if instr_type == 0: | |
115 self.playing = False | |
116 return False | |
117 if instr_type == 1: | |
118 self.texcoords = self.anm.sprites[args[0]] | |
119 elif instr_type == 2: | |
120 self.rescale = args | |
121 elif instr_type == 3: | |
122 self.alpha = args[0] % 256 #TODO | |
123 elif instr_type == 5: | |
124 self.instruction_pointer, = args | |
125 self.frame = script[self.instruction_pointer][0] | |
126 elif instr_type == 7: | |
127 self.mirrored = True | |
128 elif instr_type == 9: | |
129 self.rotations_3d = args | |
130 elif instr_type == 10: | |
131 self.rotations_speed_3d = args | |
132 elif instr_type == 23: | |
133 self.corner_relative_placement = True #TODO | |
134 elif instr_type == 27: | |
135 tox, toy = self.texoffsets | |
136 self.texoffsets = tox + args[0], toy | |
137 elif instr_type == 28: | |
138 tox, toy = self.texoffsets | |
139 self.texoffsets = tox, toy + args[0] | |
140 elif instr_type == 15: | |
141 self.keep_still = True | |
142 break | |
143 else: | |
144 print('unhandled opcode: %d, %r' % (instr_type, args)) #TODO | |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
145 self.frame += 1 |
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
146 |
30 | 147 ax, ay, az = self.rotations_3d |
148 sax, say, saz = self.rotations_speed_3d | |
149 self.rotations_3d = ax + sax, ay + say, az + saz | |
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
150 |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
151 if self.rotations_speed_3d != (0., 0., 0.): |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
152 return True |
15 | 153 |
30 | 154 return changed |
155 |