comparison pytouhou/vm/anmrunner.py @ 236:741860192b56

Implement ANM0 interrupts “Instruction” 22 is used as a label for interrupts. If the normal animation is interrupted, it goes straight to the matched instruction. Interrupt -1 matches all interrupts.
author Thibaut Girka <thib@sitedethib.com>
date Sun, 01 Jan 2012 19:47:34 +0100
parents e7902309305c
children 0e1762b1ab9f
comparison
equal deleted inserted replaced
235:e59bd7979ddc 236:741860192b56
23 23
24 class ANMRunner(object): 24 class ANMRunner(object):
25 __metaclass__ = MetaRegistry 25 __metaclass__ = MetaRegistry
26 __slots__ = ('_anm_wrapper', '_sprite', '_running', 26 __slots__ = ('_anm_wrapper', '_sprite', '_running',
27 'sprite_index_offset', 27 'sprite_index_offset',
28 'script', 'instruction_pointer', 'frame') 28 'script', 'instruction_pointer', 'frame',
29 'waiting')
29 30
30 31
31 def __init__(self, anm_wrapper, script_id, sprite, sprite_index_offset=0): 32 def __init__(self, anm_wrapper, script_id, sprite, sprite_index_offset=0):
32 self._anm_wrapper = anm_wrapper 33 self._anm_wrapper = anm_wrapper
33 self._sprite = sprite 34 self._sprite = sprite
34 self._running = True 35 self._running = True
36 self.waiting = False
35 37
36 anm, self.script = anm_wrapper.get_script(script_id) 38 anm, self.script = anm_wrapper.get_script(script_id)
37 self.frame = 0 39 self.frame = 0
38 self.instruction_pointer = 0 40 self.instruction_pointer = 0
39 41
40 self.sprite_index_offset = sprite_index_offset 42 self.sprite_index_offset = sprite_index_offset
41 43
42 44
45 def interrupt(self, interrupt):
46 new_ip = self.script.interrupts.get(interrupt, None)
47 if new_ip is None:
48 new_ip = self.script.interrupts.get(-1, None)
49 if new_ip is None:
50 return False
51 else:
52 self.instruction_pointer = new_ip
53 self.frame, opcode, args = self.script[self.instruction_pointer]
54 return True
55
56
43 def run_frame(self): 57 def run_frame(self):
44 if not self._running: 58 if not self._running:
45 return False 59 return False
60
61 if self.waiting:
62 return True
46 63
47 sprite = self._sprite 64 sprite = self._sprite
48 65
49 while self._running: 66 while self._running:
50 frame, opcode, args = self.script[self.instruction_pointer] 67 frame, opcode, args = self.script[self.instruction_pointer]
164 def set_blendfunc_add(self): 181 def set_blendfunc_add(self):
165 self._sprite.blendfunc = 0 #TODO 182 self._sprite.blendfunc = 0 #TODO
166 183
167 184
168 @instruction(15) 185 @instruction(15)
169 @instruction(21) #TODO
170 def keep_still(self): 186 def keep_still(self):
171 self._running = False 187 self._running = False
172 188
173 @instruction(16) 189 @instruction(16)
174 def load_random_sprite(self, min_idx, amp): 190 def load_random_sprite(self, min_idx, amp):
192 208
193 209
194 @instruction(20) 210 @instruction(20)
195 def move_in_accel(self, x, y, z, duration): 211 def move_in_accel(self, x, y, z, duration):
196 self._sprite.move_in(duration, x, y, z, lambda x: x ** 2) 212 self._sprite.move_in(duration, x, y, z, lambda x: x ** 2)
213
214
215 @instruction(21)
216 def wait(self):
217 """Wait for an interrupt.
218 """
219 self.waiting = True
220
221
222 @instruction(22)
223 def interrupt_label(self, interrupt):
224 """Noop"""
225 pass
197 226
198 227
199 @instruction(23) 228 @instruction(23)
200 def set_corner_relative_placement(self): 229 def set_corner_relative_placement(self):
201 self._sprite.corner_relative_placement = True #TODO 230 self._sprite.corner_relative_placement = True #TODO