annotate pytouhou/game/sprite.py @ 34:4d93d45ecb62

Fix animation script flow handling
author Thibaut Girka <thib@sitedethib.com>
date Sun, 14 Aug 2011 13:34:58 +0200
parents 55973a3f1222
children a10e3f44a883
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
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 self._uvs = []
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 self._vertices = []
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 def update_uvs_vertices(self, override_width=0, override_height=0):
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
36 vertmat = Matrix([[-.5, .5, .5, -.5],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
37 [-.5, -.5, .5, .5],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
38 [ .0, .0, .0, .0],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
39 [ 1., 1., 1., 1.]])
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 tx, ty, tw, th = self.texcoords
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 sx, sy = self.rescale
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 width = override_width or (tw * sx)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 height = override_height or (th * sy)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45
31
55973a3f1222 Some more optimization!
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
46 vertmat.scale2d(width, height)
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 if self.mirrored:
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
48 vertmat.flip()
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 if self.rotations_3d != (0., 0., 0.):
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 rx, ry, rz = self.rotations_3d
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
51 if rx:
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
52 vertmat.rotate_x(-rx)
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
53 if ry:
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
54 vertmat.rotate_y(ry)
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
55 if rz:
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
56 vertmat.rotate_z(-rz) #TODO: minus, really?
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 if self.corner_relative_placement: # Reposition
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 27
diff changeset
58 vertmat.translate(width / 2., height / 2., 0.)
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
60 x_1 = 1. / self.anm.size[0]
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
61 y_1 = 1. / self.anm.size[1]
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
62 uvs = [(tx * x_1, 1. - (ty * y_1)),
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
63 ((tx + tw) * x_1, 1. - (ty * y_1)),
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
64 ((tx + tw) * x_1, 1. - ((ty + th) * y_1)),
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
65 (tx * x_1, 1. - ((ty + th) * y_1))]
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
67 d = vertmat.data
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
68 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.)
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
69 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2])
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72
29
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 28
diff changeset
73 def update(self):
34
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
74 if not self.playing:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
75 return False
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
76
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
77 changed = False
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
78 frame = self.frame
34
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
79 if not self.keep_still:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
80 script = self.anm.scripts[self.script_index]
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
81 try:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
82 while frame <= self.frame:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
83 frame, instr_type, data = script[self.instruction_pointer]
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
84 if frame == self.frame:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
85 changed = True
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
86 if instr_type == 1:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
87 self.texcoords = self.anm.sprites[unpack('<I', data)[0]]
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
88 elif instr_type == 2:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
89 self.rescale = unpack('<ff', data)
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
90 elif instr_type == 5:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
91 self.frame, = unpack('<I', data)
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
92 self.instruction_pointer = 0
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
93 elif instr_type == 7:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
94 self.mirrored = True
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
95 elif instr_type == 9:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
96 self.rotations_3d = unpack('<fff', data)
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
97 elif instr_type == 10:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
98 self.rotations_speed_3d = unpack('<fff', data)
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
99 elif instr_type == 23:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
100 self.corner_relative_placement = True #TODO
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
101 elif instr_type == 15:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
102 self.keep_still = True
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
103 break
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
104 else:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
105 print('unhandled opcode: %d, %r' % (instr_type, data)) #TODO
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
106 if frame <= self.frame:
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
107 self.instruction_pointer += 1
34
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
108 except IndexError:
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
109 self.playing = False
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 31
diff changeset
110 pass
17
d940d004b840 Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
111 self.frame += 1
d940d004b840 Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
112
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
113 ax, ay, az = self.rotations_3d
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
114 sax, say, saz = self.rotations_speed_3d
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
115 self.rotations_3d = ax + sax, ay + say, az + saz
27
b65d6bc55793 Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents: 26
diff changeset
116
b65d6bc55793 Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents: 26
diff changeset
117 if self.rotations_speed_3d != (0., 0., 0.):
b65d6bc55793 Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents: 26
diff changeset
118 return True
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
119
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
120 return changed
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
121