comparison pytouhou/game/sprite.py @ 41:93c8dc2de923

Add (hopefully) "texture shifting" in animations
author Thibaut Girka <thib@sitedethib.com>
date Mon, 15 Aug 2011 00:41:51 +0200
parents cb5b27011044
children ab826bc29aa2
comparison
equal deleted inserted replaced
40:ce662b372ee0 41:93c8dc2de923
17 class Sprite(object): 17 class Sprite(object):
18 def __init__(self, anm, script_index): 18 def __init__(self, anm, script_index):
19 self.anm = anm 19 self.anm = anm
20 self.script_index = script_index 20 self.script_index = script_index
21 self.texcoords = (0, 0, 0, 0) # x, y, width, height 21 self.texcoords = (0, 0, 0, 0) # x, y, width, height
22 self.texoffsets = (0., 0.)
22 self.mirrored = False 23 self.mirrored = False
23 self.rescale = (1., 1.) 24 self.rescale = (1., 1.)
24 self.rotations_3d = (0., 0., 0.) 25 self.rotations_3d = (0., 0., 0.)
25 self.rotations_speed_3d = (0., 0., 0.) 26 self.rotations_speed_3d = (0., 0., 0.)
26 self.corner_relative_placement = False 27 self.corner_relative_placement = False
59 if self.corner_relative_placement: # Reposition 60 if self.corner_relative_placement: # Reposition
60 vertmat.translate(width / 2., height / 2., 0.) 61 vertmat.translate(width / 2., height / 2., 0.)
61 62
62 x_1 = 1. / self.anm.size[0] 63 x_1 = 1. / self.anm.size[0]
63 y_1 = 1. / self.anm.size[1] 64 y_1 = 1. / self.anm.size[1]
64 uvs = [(tx * x_1, 1. - (ty * y_1)), 65 tox, toy = self.texoffsets
65 ((tx + tw) * x_1, 1. - (ty * y_1)), 66 uvs = [(tx * x_1 + tox, 1. - (ty * y_1) + toy),
66 ((tx + tw) * x_1, 1. - ((ty + th) * y_1)), 67 ((tx + tw) * x_1 + tox, 1. - (ty * y_1) + toy),
67 (tx * x_1, 1. - ((ty + th) * y_1))] 68 ((tx + tw) * x_1 + tox, 1. - ((ty + th) * y_1 + toy)),
69 (tx * x_1 + tox, 1. - ((ty + th) * y_1 + toy))]
68 70
69 d = vertmat.data 71 d = vertmat.data
70 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.) 72 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.)
71 self._colors = [(255, 255, 255, self.alpha)] * 4 73 self._colors = [(255, 255, 255, self.alpha)] * 4
72 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2]) 74 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2])
76 def update(self): 78 def update(self):
77 if not self.playing: 79 if not self.playing:
78 return False 80 return False
79 81
80 changed = False 82 changed = False
81 frame = self.frame
82 if not self.keep_still: 83 if not self.keep_still:
83 script = self.anm.scripts[self.script_index] 84 script = self.anm.scripts[self.script_index]
84 try: 85 try:
86 frame = self.frame
85 while frame <= self.frame: 87 while frame <= self.frame:
86 frame, instr_type, args = script[self.instruction_pointer] 88 frame, instr_type, args = script[self.instruction_pointer]
87 if frame == self.frame: 89 if frame == self.frame:
88 changed = True 90 changed = True
89 if instr_type == 0: 91 if instr_type == 0:
105 self.rotations_3d = args 107 self.rotations_3d = args
106 elif instr_type == 10: 108 elif instr_type == 10:
107 self.rotations_speed_3d = args 109 self.rotations_speed_3d = args
108 elif instr_type == 23: 110 elif instr_type == 23:
109 self.corner_relative_placement = True #TODO 111 self.corner_relative_placement = True #TODO
112 elif instr_type == 27:
113 tox, toy = self.texoffsets
114 self.texoffsets = tox + args[0], toy
115 elif instr_type == 28:
116 tox, toy = self.texoffsets
117 self.texoffsets = tox, toy + args[0]
110 elif instr_type == 15: 118 elif instr_type == 15:
111 self.keep_still = True 119 self.keep_still = True
112 break 120 break
113 else: 121 else:
114 print('unhandled opcode: %d, %r' % (instr_type, args)) #TODO 122 print('unhandled opcode: %d, %r' % (instr_type, args)) #TODO