Mercurial > touhou
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 |
rev | line source |
---|---|
15 | 1 from struct import unpack |
2 | |
3 from pytouhou.utils.matrix import Matrix | |
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 | 17 class Sprite(object): |
18 def __init__(self, anm, script_index): | |
19 self.anm = anm | |
20 self.script_index = script_index | |
21 self.texcoords = (0, 0, 0, 0) # x, y, width, height | |
22 self.mirrored = False | |
23 self.rescale = (1., 1.) | |
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 | 26 self.corner_relative_placement = False |
30 | 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 | 31 self._uvs = [] |
32 self._vertices = [] | |
33 | |
34 | |
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 | 40 |
41 tx, ty, tw, th = self.texcoords | |
42 sx, sy = self.rescale | |
43 width = override_width or (tw * sx) | |
44 height = override_height or (th * sy) | |
45 | |
31 | 46 vertmat.scale2d(width, height) |
15 | 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 | 49 if self.rotations_3d != (0., 0., 0.): |
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 | 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 | 59 |
30 | 60 x_1 = 1. / self.anm.size[0] |
61 y_1 = 1. / self.anm.size[1] | |
62 uvs = [(tx * x_1, 1. - (ty * y_1)), | |
63 ((tx + tw) * x_1, 1. - (ty * y_1)), | |
64 ((tx + tw) * x_1, 1. - ((ty + th) * y_1)), | |
65 (tx * x_1, 1. - ((ty + th) * y_1))] | |
15 | 66 |
30 | 67 d = vertmat.data |
68 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.) | |
69 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2]) | |
15 | 70 |
71 | |
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 | 77 changed = False |
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 | 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 | 113 ax, ay, az = self.rotations_3d |
114 sax, say, saz = self.rotations_speed_3d | |
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 | 119 |
30 | 120 return changed |
121 |