comparison pytouhou/game/enemy.pyx @ 497:3da7395f39e3

Make enemy callbacks programmables.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 14 Oct 2013 12:20:55 +0200
parents 6be9c99a3a24
children 507d446dc6cf
comparison
equal deleted inserted replaced
496:104c737ce8b3 497:3da7395f39e3
19 from pytouhou.game.bullet cimport Bullet, LAUNCHED 19 from pytouhou.game.bullet cimport Bullet, LAUNCHED
20 from pytouhou.game.laser cimport Laser, PlayerLaser 20 from pytouhou.game.laser cimport Laser, PlayerLaser
21 from pytouhou.game.effect cimport Effect 21 from pytouhou.game.effect cimport Effect
22 22
23 23
24 cdef class Callback:
25 def __init__(self, function=None, args=()):
26 self.function = function
27 self.args = args
28
29 def __nonzero__(self):
30 return self.function is not None
31
32 cpdef enable(self, function, tuple args):
33 self.function = function
34 self.args = args
35
36 cpdef disable(self):
37 self.function = None
38
39 cpdef fire(self):
40 if self.function is not None:
41 self.function(*self.args)
42 self.function = None
43
44
24 cdef class Enemy(Element): 45 cdef class Enemy(Element):
25 def __init__(self, pos, long life, long _type, long bonus_dropped, long die_score, anms, Game game): 46 def __init__(self, pos, long life, long _type, long bonus_dropped, long die_score, anms, Game game):
26 Element.__init__(self) 47 Element.__init__(self)
27 48
28 self._game = game 49 self._game = game
48 self.extended_bullet_attributes = (0, 0, 0, 0, 0., 0., 0., 0.) 69 self.extended_bullet_attributes = (0, 0, 0, 0, 0., 0., 0., 0.)
49 self.current_laser_id = 0 70 self.current_laser_id = 0
50 self.laser_by_id = {} 71 self.laser_by_id = {}
51 self.bullet_attributes = None 72 self.bullet_attributes = None
52 self.bullet_launch_offset = (0, 0) 73 self.bullet_launch_offset = (0, 0)
53 self.death_callback = -1
54 self.boss_callback = -1
55 self.low_life_callback = -1
56 self.low_life_trigger = -1 74 self.low_life_trigger = -1
57 self.timeout = -1 75 self.timeout = -1
58 self.timeout_callback = -1
59 self.remaining_lives = 0 76 self.remaining_lives = 0
77
78 self.death_callback = Callback()
79 self.boss_callback = Callback()
80 self.low_life_callback = Callback()
81 self.timeout_callback = Callback()
60 82
61 self.automatic_orientation = False 83 self.automatic_orientation = False
62 84
63 self.bullet_launch_interval = 0 85 self.bullet_launch_interval = 0
64 self.bullet_launch_timer = 0 86 self.bullet_launch_timer = 0
380 402
381 cdef void handle_callbacks(self): 403 cdef void handle_callbacks(self):
382 #TODO: implement missing callbacks and clean up! 404 #TODO: implement missing callbacks and clean up!
383 if self.life <= 0 and self.touchable: 405 if self.life <= 0 and self.touchable:
384 self.timeout = -1 #TODO: not really true, the timeout is frozen 406 self.timeout = -1 #TODO: not really true, the timeout is frozen
385 self.timeout_callback = -1 407 self.timeout_callback.disable()
386 death_flags = self.death_flags & 7 408 death_flags = self.death_flags & 7
387 409
388 self.die_anim() 410 self.die_anim()
389 411
390 #TODO: verify if the score is added with all the different flags. 412 #TODO: verify if the score is added with all the different flags.
426 self._game.boss = None 448 self._game.boss = None
427 self.damageable = False 449 self.damageable = False
428 self.life = 1 450 self.life = 1
429 self.death_flags = 0 451 self.death_flags = 0
430 452
431 if death_flags != 0 and self.death_callback > -1: 453 if death_flags != 0:
432 self.process.switch_to_sub(self.death_callback) 454 self.death_callback.fire()
433 self.death_callback = -1 455 elif self.life <= self.low_life_trigger and self.low_life_callback:
434 elif self.life <= self.low_life_trigger and self.low_life_callback > -1: 456 self.low_life_callback.fire()
435 self.process.switch_to_sub(self.low_life_callback)
436 self.low_life_callback = -1
437 self.low_life_trigger = -1 457 self.low_life_trigger = -1
438 self.timeout_callback = -1 458 self.timeout_callback.disable()
439 elif self.timeout != -1 and self.frame == self.timeout: 459 elif self.timeout != -1 and self.frame == self.timeout:
440 self.frame = 0 460 self.frame = 0
441 self.timeout = -1 461 self.timeout = -1
442 self._game.kill_enemies() 462 self._game.kill_enemies()
443 self._game.cancel_bullets() 463 self._game.cancel_bullets()
444 464
445 if self.low_life_trigger > 0: 465 if self.low_life_trigger > 0:
446 self.life = self.low_life_trigger 466 self.life = self.low_life_trigger
447 self.low_life_trigger = -1 467 self.low_life_trigger = -1
448 468
449 if self.timeout_callback > -1: 469 if self.timeout_callback:
450 self.process.switch_to_sub(self.timeout_callback) 470 self.timeout_callback.fire()
451 self.timeout_callback = -1
452 #TODO: this is only done under certain (unknown) conditions! 471 #TODO: this is only done under certain (unknown) conditions!
453 # but it shouldn't hurt anyway, as the only option left is to crash! 472 # but it shouldn't hurt anyway, as the only option left is to crash!
454 elif self.death_callback > -1: 473 elif self.death_callback:
455 self.life = 0 #TODO: do this next frame? Bypass self.touchable? 474 self.life = 0 #TODO: do this next frame? Bypass self.touchable?
456 else: 475 else:
457 raise Exception('What the hell, man!') 476 raise Exception('What the hell, man!')
458 477
459 478