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