annotate pytouhou/game/sprite.py @ 39:493b503c81e0

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