Mercurial > touhou
annotate pytouhou/game/sprite.py @ 53:e72166f73e30
Oops
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 22 Aug 2011 23:20:17 +0200 |
parents | ab826bc29aa2 |
children | b383b09bc735 |
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] |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
100 try: |
41
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
101 frame = self.frame |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
102 while frame <= self.frame: |
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] |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
104 if frame == self.frame: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
105 changed = True |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
106 if instr_type == 0: |
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
107 self.playing = False |
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
108 return False |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
109 if instr_type == 1: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
110 self.texcoords = self.anm.sprites[args[0]] |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
111 elif instr_type == 2: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
112 self.rescale = args |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
113 elif instr_type == 3: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
114 self.alpha = args[0] % 256 #TODO |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
115 elif instr_type == 5: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
116 self.instruction_pointer, = args |
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
117 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
|
118 continue |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
119 elif instr_type == 7: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
120 self.mirrored = True |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
121 elif instr_type == 9: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
122 self.rotations_3d = args |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
123 elif instr_type == 10: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
124 self.rotations_speed_3d = args |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
125 elif instr_type == 23: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
126 self.corner_relative_placement = True #TODO |
41
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
127 elif instr_type == 27: |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
128 tox, toy = self.texoffsets |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
129 self.texoffsets = tox + args[0], toy |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
130 elif instr_type == 28: |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
131 tox, toy = self.texoffsets |
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
132 self.texoffsets = tox, toy + args[0] |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
133 elif instr_type == 15: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
134 self.keep_still = True |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
135 break |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
136 else: |
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
137 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
|
138 if frame <= self.frame: |
30 | 139 self.instruction_pointer += 1 |
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
140 except IndexError: |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
141 self.playing = False |
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
142 pass |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
143 self.frame += 1 |
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
144 |
30 | 145 ax, ay, az = self.rotations_3d |
146 sax, say, saz = self.rotations_speed_3d | |
147 self.rotations_3d = ax + sax, ay + say, az + saz | |
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
148 |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
149 if self.rotations_speed_3d != (0., 0., 0.): |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
150 return True |
15 | 151 |
30 | 152 return changed |
153 |