changeset 38:cb5b27011044

Small refactoring and proper anm0's instruction 5 handling
author Thibaut Girka <thib@sitedethib.com>
date Sun, 14 Aug 2011 18:08:45 +0200
parents a10e3f44a883
children 493b503c81e0
files pytouhou/formats/anm0.py pytouhou/game/sprite.py
diffstat 2 files changed, 29 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/formats/anm0.py
+++ b/pytouhou/formats/anm0.py
@@ -49,15 +49,32 @@ class Animations(object):
 
 
         # Scripts
-        anm.scripts = {}#[None] * nb_scripts
+        anm.scripts = {}
         for i, offset in script_offsets:
             anm.scripts[i] = []
+            instruction_offsets = []
             file.seek(offset)
             while True:
                 #TODO
+                instruction_offsets.append(file.tell() - offset)
                 time, instr_type, length = unpack('<HBB', file.read(4))
                 data = file.read(length)
-                anm.scripts[i].append((time, instr_type, data))
+                if instr_type == 1: # set_sprite
+                    args = unpack('<I', data)
+                elif instr_type == 2: # set_scale
+                    args = unpack('<ff', data)
+                elif instr_type == 3: # set_alpha
+                    args = unpack('<I', data)
+                elif instr_type == 5: # jump
+                    # Translate offset to instruction index
+                    args = (instruction_offsets.index(unpack('<I', data)[0]),)
+                elif instr_type == 9: # set_3d_rotation
+                    args = unpack('<fff', data)
+                elif instr_type == 10: # set_3d_rotation_speed
+                    args = unpack('<fff', data)
+                else:
+                    args = (data,)
+                anm.scripts[i].append((time, instr_type, args))
                 if instr_type == 0:
                     break
         #TODO
--- a/pytouhou/game/sprite.py
+++ b/pytouhou/game/sprite.py
@@ -83,34 +83,35 @@ class Sprite(object):
             script = self.anm.scripts[self.script_index]
             try:
                 while frame <= self.frame:
-                    frame, instr_type, data = script[self.instruction_pointer]
+                    frame, instr_type, args = script[self.instruction_pointer]
                     if frame == self.frame:
                         changed = True
                         if instr_type == 0:
                             self.playing = False
                             return False
                         if instr_type == 1:
-                            self.texcoords = self.anm.sprites[unpack('<I', data)[0]]
+                            self.texcoords = self.anm.sprites[args[0]]
                         elif instr_type == 2:
-                            self.rescale = unpack('<ff', data)
+                            self.rescale = args
                         elif instr_type == 3:
-                            self.alpha = unpack('<I', data)[0] % 256 #TODO
+                            self.alpha = args[0] % 256 #TODO
                         elif instr_type == 5:
-                            self.frame, = unpack('<I', data)
-                            self.instruction_pointer = 0
+                            self.instruction_pointer, = args
+                            self.frame = script[self.instruction_pointer][0]
+                            continue
                         elif instr_type == 7:
                             self.mirrored = True
                         elif instr_type == 9:
-                            self.rotations_3d = unpack('<fff', data)
+                            self.rotations_3d = args
                         elif instr_type == 10:
-                            self.rotations_speed_3d = unpack('<fff', data)
+                            self.rotations_speed_3d = args
                         elif instr_type == 23:
                             self.corner_relative_placement = True #TODO
                         elif instr_type == 15:
                             self.keep_still = True
                             break
                         else:
-                            print('unhandled opcode: %d, %r' % (instr_type, data)) #TODO
+                            print('unhandled opcode: %d, %r' % (instr_type, args)) #TODO
                     if frame <= self.frame:
                         self.instruction_pointer += 1
             except IndexError: