Mercurial > touhou
annotate 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 |
| 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 |
|
57
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
16 from random import randrange |
| 15 | 17 from struct import unpack |
| 18 | |
| 19 from pytouhou.utils.matrix import Matrix | |
| 20 | |
|
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
21 |
|
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
22 class AnmWrapper(object): |
|
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
23 def __init__(self, anm_files): |
|
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
24 self.anm_files = list(anm_files) |
|
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
25 |
|
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
26 def get_sprite(self, script_index): |
|
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
27 for anm in self.anm_files: |
|
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
28 if script_index in anm.scripts: |
|
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
29 return anm, Sprite(anm, script_index) |
|
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 |
|
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
32 |
| 15 | 33 class Sprite(object): |
| 34 def __init__(self, anm, script_index): | |
| 35 self.anm = anm | |
| 36 self.script_index = script_index | |
| 37 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
|
38 self.texoffsets = (0., 0.) |
| 15 | 39 self.mirrored = False |
| 40 self.rescale = (1., 1.) | |
|
57
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
41 self.scale_speed = (0., 0.) |
| 15 | 42 self.rotations_3d = (0., 0., 0.) |
|
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
43 self.rotations_speed_3d = (0., 0., 0.) |
| 15 | 44 self.corner_relative_placement = False |
| 30 | 45 self.instruction_pointer = 0 |
|
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
46 self.keep_still = False |
|
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
47 self.playing = True |
|
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
48 self.frame = 0 |
|
57
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
49 self.color = (255, 255, 255) |
|
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
50 self.alpha = 255 |
| 15 | 51 self._uvs = [] |
| 52 self._vertices = [] | |
|
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
53 self._colors = [] |
| 15 | 54 |
| 55 | |
|
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
34
diff
changeset
|
56 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
|
57 vertmat = Matrix([[-.5, .5, .5, -.5], |
|
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
58 [-.5, -.5, .5, .5], |
|
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
59 [ .0, .0, .0, .0], |
|
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
60 [ 1., 1., 1., 1.]]) |
| 15 | 61 |
| 62 tx, ty, tw, th = self.texcoords | |
| 63 sx, sy = self.rescale | |
| 64 width = override_width or (tw * sx) | |
| 65 height = override_height or (th * sy) | |
| 66 | |
| 31 | 67 vertmat.scale2d(width, height) |
| 15 | 68 if self.mirrored: |
|
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
69 vertmat.flip() |
| 15 | 70 if self.rotations_3d != (0., 0., 0.): |
| 71 rx, ry, rz = self.rotations_3d | |
|
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
72 if rx: |
|
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
73 vertmat.rotate_x(-rx) |
|
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
74 if ry: |
|
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
75 vertmat.rotate_y(ry) |
|
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
76 if rz: |
|
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
77 vertmat.rotate_z(-rz) #TODO: minus, really? |
| 15 | 78 if self.corner_relative_placement: # Reposition |
|
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
79 vertmat.translate(width / 2., height / 2., 0.) |
| 15 | 80 |
| 30 | 81 x_1 = 1. / self.anm.size[0] |
| 82 y_1 = 1. / self.anm.size[1] | |
|
41
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
83 tox, toy = self.texoffsets |
|
93c8dc2de923
Add (hopefully) "texture shifting" in animations
Thibaut Girka <thib@sitedethib.com>
parents:
38
diff
changeset
|
84 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
|
85 ((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
|
86 ((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
|
87 (tx * x_1 + tox, 1. - ((ty + th) * y_1 + toy))] |
| 15 | 88 |
| 30 | 89 d = vertmat.data |
| 90 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.) | |
|
57
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
91 self._colors = [(self.color[0], self.color[1], self.color[2], self.alpha)] * 4 |
| 30 | 92 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2]) |
| 15 | 93 |
| 94 | |
| 95 | |
|
29
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
28
diff
changeset
|
96 def update(self): |
|
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
97 if not self.playing: |
|
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
98 return False |
|
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
99 |
| 30 | 100 changed = False |
|
34
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
101 if not self.keep_still: |
|
4d93d45ecb62
Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents:
31
diff
changeset
|
102 script = self.anm.scripts[self.script_index] |
| 54 | 103 frame = self.frame |
| 104 while True: | |
| 105 try: | |
|
38
cb5b27011044
Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents:
37
diff
changeset
|
106 frame, instr_type, args = script[self.instruction_pointer] |
| 54 | 107 except IndexError: |
| 108 self.playing = False | |
| 109 return False | |
| 110 | |
| 111 if frame > self.frame: | |
| 112 break | |
| 113 else: | |
| 114 self.instruction_pointer += 1 | |
| 115 if frame == self.frame: | |
| 116 changed = True | |
| 117 if instr_type == 0: | |
| 118 self.playing = False | |
| 119 return False | |
| 120 if instr_type == 1: | |
|
57
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
121 #TODO: handle out-of-anm sprites |
| 54 | 122 self.texcoords = self.anm.sprites[args[0]] |
| 123 elif instr_type == 2: | |
| 124 self.rescale = args | |
| 125 elif instr_type == 3: | |
| 126 self.alpha = args[0] % 256 #TODO | |
|
57
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
127 elif instr_type == 4: |
|
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
128 b, g, r = args |
|
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
129 self.color = (r, g, b) |
| 54 | 130 elif instr_type == 5: |
| 131 self.instruction_pointer, = args | |
| 132 self.frame = script[self.instruction_pointer][0] | |
| 133 elif instr_type == 7: | |
| 134 self.mirrored = True | |
| 135 elif instr_type == 9: | |
| 136 self.rotations_3d = args | |
| 137 elif instr_type == 10: | |
| 138 self.rotations_speed_3d = args | |
|
57
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
139 elif instr_type == 11: |
|
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
140 self.scale_speed = args |
|
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
141 elif instr_type == 16: |
|
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
142 #TODO: handle out-of-anm sprites |
|
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
143 #TODO: use the game's PRNG? |
|
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
144 self.texcoords = self.anm.sprites[args[0] + randrange(args[1])] |
| 54 | 145 elif instr_type == 23: |
| 146 self.corner_relative_placement = True #TODO | |
| 147 elif instr_type == 27: | |
| 148 tox, toy = self.texoffsets | |
| 149 self.texoffsets = tox + args[0], toy | |
| 150 elif instr_type == 28: | |
| 151 tox, toy = self.texoffsets | |
| 152 self.texoffsets = tox, toy + args[0] | |
|
57
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
153 elif instr_type in (15, 21): |
| 54 | 154 self.keep_still = True |
| 155 break | |
| 156 else: | |
| 157 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
|
158 self.frame += 1 |
|
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
159 |
| 30 | 160 ax, ay, az = self.rotations_3d |
| 161 sax, say, saz = self.rotations_speed_3d | |
| 162 self.rotations_3d = ax + sax, ay + say, az + saz | |
|
57
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
163 self.rescale = self.rescale[0] + self.scale_speed[0], self.rescale[1] + self.scale_speed[1] |
|
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
164 |
|
57
694f25881d0f
Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents:
54
diff
changeset
|
165 if self.rotations_speed_3d != (0., 0., 0.) or self.scale_speed != (0., 0.): |
|
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
166 return True |
| 15 | 167 |
| 30 | 168 return changed |
| 169 |
