comparison pytouhou/game/enemy.py @ 166:dcf32488a2c9

Better enemy death, with animation and (hopefully) correct flags handling.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 18 Oct 2011 13:17:23 -0700
parents 4684d311a32d
children e483b5a7e84b
comparison
equal deleted inserted replaced
165:c8c60291c56f 166:dcf32488a2c9
17 from pytouhou.vm.anmrunner import ANMRunner 17 from pytouhou.vm.anmrunner import ANMRunner
18 from pytouhou.game.sprite import Sprite 18 from pytouhou.game.sprite import Sprite
19 from pytouhou.game.bullet import Bullet 19 from pytouhou.game.bullet import Bullet
20 from pytouhou.game.item import Item 20 from pytouhou.game.item import Item
21 from math import cos, sin, atan2, pi 21 from math import cos, sin, atan2, pi
22
23
24 class Effect(object):
25 def __init__(self, pos, index, anm_wrapper):
26 self._sprite = Sprite()
27 self._anmrunner = ANMRunner(anm_wrapper, index, self._sprite)
28 self._anmrunner.run_frame()
29 self._removed = False
30
31 self.x, self.y = pos
32
33 def update(self):
34 if self._anmrunner and not self._anmrunner.run_frame():
35 self._anmrunner = None
36
37 if self._sprite:
38 if self._sprite._removed:
39 self._sprite = None
22 40
23 41
24 class Enemy(object): 42 class Enemy(object):
25 def __init__(self, pos, life, _type, bonus_dropped, anm_wrapper, game): 43 def __init__(self, pos, life, _type, bonus_dropped, anm_wrapper, game):
26 self._game = game 44 self._game = game
142 self._sprite = Sprite() 160 self._sprite = Sprite()
143 self._anmrunner = ANMRunner(self._anm_wrapper, index, self._sprite) 161 self._anmrunner = ANMRunner(self._anm_wrapper, index, self._sprite)
144 self._anmrunner.run_frame() 162 self._anmrunner.run_frame()
145 163
146 164
147 def collide(self): 165 def on_attack(self, bullet):
148 #TODO: animation 166 if self.damageable:
149 #TODO: doesn’t always kill herself (a boss for example), search how 167 self.life -= bullet._bullet_type.damage
150 self._removed = True 168
151 169
152 170 def on_collide(self):
153 def killed(self): 171 self.life -= 80 # found experimentally
154 if self.touchable: 172
155 if 0 <= self._bonus_dropped < 256: 173
156 self._game.drop_bonus(self.x, self.y, 0) 174 def die_anim(self):
157 elif -256 <= self._bonus_dropped < 0: 175 eff00 = self._game.resource_loader.get_anm_wrapper(('eff00.anm',))
158 pass #TODO: should be random, search how it is done. 176 self._game.effects.append(Effect((self.x, self.y), self.death_anim, eff00))
159
160 #TODO: use self.death_flags
161 self._removed = True
162 177
163 178
164 def set_pos(self, x, y, z): 179 def set_pos(self, x, y, z):
165 self.x, self.y = x, y 180 self.x, self.y = x, y
166 self.interpolator = Interpolator((x, y)) 181 self.interpolator = Interpolator((x, y))
269 if self.bullet_launch_interval != 0: 284 if self.bullet_launch_interval != 0:
270 self.bullet_launch_timer += 1 285 self.bullet_launch_timer += 1
271 if self.bullet_launch_timer == self.bullet_launch_interval: 286 if self.bullet_launch_timer == self.bullet_launch_interval:
272 self.fire() 287 self.fire()
273 288
274 if self.life <= 0:
275 self.killed()
276
277 self.frame += 1 289 self.frame += 1
278 290