Mercurial > touhou
annotate pytouhou/game/sprite.py @ 21:bf225780973f
Small refactoring, and Rumia \o/
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Thu, 11 Aug 2011 12:39:12 +0200 |
parents | d940d004b840 |
children | f17122405121 |
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.) | |
25 self.corner_relative_placement = False | |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
26 self.frame = 0 |
15 | 27 self._uvs = [] |
28 self._vertices = [] | |
29 | |
30 | |
31 def update_uvs_vertices(self, override_width=0, override_height=0): | |
32 vertmat = Matrix() | |
33 vertmat.data[0][0] = -.5 | |
34 vertmat.data[1][0] = -.5 | |
35 | |
36 vertmat.data[0][1] = .5 | |
37 vertmat.data[1][1] = -.5 | |
38 | |
39 vertmat.data[0][2] = .5 | |
40 vertmat.data[1][2] = .5 | |
41 | |
42 vertmat.data[0][3] = -.5 | |
43 vertmat.data[1][3] = .5 | |
44 | |
45 for i in range(4): | |
46 vertmat.data[2][i] = 0. | |
47 vertmat.data[3][i] = 1. | |
48 | |
49 tx, ty, tw, th = self.texcoords | |
50 sx, sy = self.rescale | |
51 width = override_width or (tw * sx) | |
52 height = override_height or (th * sy) | |
53 | |
54 transform = Matrix.get_scaling_matrix(width, height, 1.) | |
55 if self.mirrored: | |
56 transform = Matrix.get_scaling_matrix(-1., 1., 1.).mult(transform) | |
57 if self.rotations_3d != (0., 0., 0.): | |
58 rx, ry, rz = self.rotations_3d | |
59 transform = Matrix.get_rotation_matrix(-rx, 'x').mult(transform) | |
60 transform = Matrix.get_rotation_matrix(ry, 'y').mult(transform) | |
61 transform = Matrix.get_rotation_matrix(-rz, 'z').mult(transform) #TODO: minus, really? | |
62 if self.corner_relative_placement: # Reposition | |
63 transform = Matrix.get_translation_matrix(width / 2., height / 2., 0.).mult(transform) | |
64 vertmat = transform.mult(vertmat) | |
65 | |
66 uvs = [(tx / self.anm.size[0], 1. - (ty / self.anm.size[1])), | |
67 ((tx + tw) / self.anm.size[0], 1. - (ty / self.anm.size[1])), | |
68 ((tx + tw) / self.anm.size[0], 1. - ((ty + th) / self.anm.size[1])), | |
69 (tx / self.anm.size[0], 1. - ((ty + th) / self.anm.size[1]))] | |
70 | |
71 vertices = [] | |
72 for i in xrange(4): | |
73 w = vertmat.data[3][i] | |
74 vertices.append((vertmat.data[0][i] / w, | |
75 vertmat.data[1][i] / w, | |
76 vertmat.data[2][i] / w)) | |
77 | |
78 self._uvs, self._vertices = uvs, vertices | |
79 | |
80 | |
81 | |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
82 def update(self, override_width=0, override_height=0): |
15 | 83 properties = {} |
84 for time, instr_type, data in self.anm.scripts[self.script_index]: | |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
85 if time == self.frame: |
15 | 86 if instr_type == 15: #Return |
87 break | |
88 else: | |
89 properties[instr_type] = data | |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
90 self.frame += 1 |
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
91 |
15 | 92 if properties: |
93 if 1 in properties: | |
94 self.texcoords = self.anm.sprites[unpack('<I', properties[1])[0]] | |
95 del properties[1] | |
96 if 2 in properties: | |
97 self.rescale = unpack('<ff', properties[2]) | |
98 del properties[2] | |
99 if 7 in properties: | |
100 self.mirrored = True #TODO | |
101 del properties[7] | |
102 if 9 in properties: | |
103 self.rotations_3d = unpack('<fff', properties[9]) | |
104 del properties[9] | |
105 if 23 in properties: | |
106 self.corner_relative_placement = True #TODO | |
107 del properties[23] | |
108 if properties: | |
109 print('Leftover properties: %r' % properties) #TODO | |
110 self.update_uvs_vertices(override_width, override_height) | |
111 return True | |
112 return False | |
113 |