comparison pytouhou/game/enemymanager.py @ 21:bf225780973f

Small refactoring, and Rumia \o/
author Thibaut Girka <thib@sitedethib.com>
date Thu, 11 Aug 2011 12:39:12 +0200
parents 6ebf9539c077
children fa87db09fc3a
comparison
equal deleted inserted replaced
20:6ebf9539c077 21:bf225780973f
6 from pytouhou.game.sprite import Sprite 6 from pytouhou.game.sprite import Sprite
7 from math import cos, sin, atan2 7 from math import cos, sin, atan2
8 8
9 9
10 class Enemy(object): 10 class Enemy(object):
11 def __init__(self, pos, life, _type, script, anms): 11 def __init__(self, pos, life, _type, script, anm_wrapper):
12 self.anms = tuple(anms) 12 self.anm_wrapper = anm_wrapper
13 self.anm = None 13 self.anm = None
14 self.script = list(script) 14 self.script = list(script)
15 self.x, self.y = pos 15 self.x, self.y = pos
16 self.life = life 16 self.life = life
17 self.type = _type 17 self.type = _type
18 self.frame = 0 18 self.frame = 0
19 self.sprite = None 19 self.sprite = None
20 20
21 self.movement_dependant_sprites = None
21 self.interpolator = None #TODO 22 self.interpolator = None #TODO
22 self.angle = 0. 23 self.angle = 0.
23 self.speed = 0. 24 self.speed = 0.
24 self.rotation_speed = 0. 25 self.rotation_speed = 0.
25 self.acceleration = 0. 26 self.acceleration = 0.
32 for instr_type, rank_mask, param_mask, args in self.script.pop(0)[1]: 33 for instr_type, rank_mask, param_mask, args in self.script.pop(0)[1]:
33 if instr_type == 1: # delete 34 if instr_type == 1: # delete
34 return False 35 return False
35 elif instr_type == 97: # set_enemy_sprite 36 elif instr_type == 97: # set_enemy_sprite
36 script_index, = unpack('<I', args) 37 script_index, = unpack('<I', args)
37 if script_index in self.anms[0].scripts: 38 self.anm, self.sprite = self.anm_wrapper.get_sprite(script_index)
38 self.sprite = Sprite(self.anms[0], script_index) 39 elif instr_type == 98: #TODO
39 self.anm = self.anms[0] 40 self.movement_dependant_sprites = unpack('<HHHHI', args)
40 else:
41 self.sprite = Sprite(self.anms[1], script_index)
42 self.anm = self.anms[1]
43 elif instr_type == 43: # set_pos 41 elif instr_type == 43: # set_pos
44 self.x, self.y, z = unpack('<fff', args) 42 self.x, self.y, z = unpack('<fff', args)
45 self.interpolator = Interpolator((self.x, self.y)) #TODO: better interpolation 43 self.interpolator = Interpolator((self.x, self.y)) #TODO: better interpolation
46 self.interpolator.set_interpolation_start(self.frame, (self.x, self.y)) 44 self.interpolator.set_interpolation_start(self.frame, (self.x, self.y))
47 elif instr_type == 45: # set_angle_speed 45 elif instr_type == 45: # set_angle_speed
57 player_x, player_y = 192., 400.#TODO 55 player_x, player_y = 192., 400.#TODO
58 self.angle = atan2(player_y - self.y, player_x - self.x) 56 self.angle = atan2(player_y - self.y, player_x - self.x)
59 elif instr_type == 57: 57 elif instr_type == 57:
60 duration, x, y, z = unpack('<Ifff', args) 58 duration, x, y, z = unpack('<Ifff', args)
61 self.interpolator.set_interpolation_end(self.frame + duration, (x, y)) 59 self.interpolator.set_interpolation_end(self.frame + duration, (x, y))
62 if self.sprite:
63 self.sprite.update()
64 60
61 x, y = self.x, self.y
65 if self.interpolator: 62 if self.interpolator:
66 self.interpolator.update(self.frame) 63 self.interpolator.update(self.frame)
67 x, y = self.interpolator.values 64 x, y = self.interpolator.values
68 dx, dy = x - self.x, y - self.y 65
69 #TODO: animations
70 if abs(dx) > abs(dy):
71 pass #TODO
72 else:
73 pass #TODO
74 self.x, self.y = x, y
75 self.speed += self.acceleration #TODO: units? Execution order? 66 self.speed += self.acceleration #TODO: units? Execution order?
76 self.angle += self.rotation_speed #TODO: units? Execution order? 67 self.angle += self.rotation_speed #TODO: units? Execution order?
77 68
78 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed 69 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
79 if self.type & 2: 70 if self.type & 2:
80 self.x -= dx 71 x -= dx
81 else: 72 else:
82 self.x += dx 73 x += dx
83 self.y += dy 74 y += dy
75
76 if self.movement_dependant_sprites:
77 #TODO: is that really how it works?
78 dx, dy = self.x - x, self.y - y
79 if (dx, dy) == (0, 0):
80 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[0])
81 elif abs(dx) > abs(dy):
82 if dx < 0:
83 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[2])
84 else:
85 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[3])
86 else:
87 if dy < 0:
88 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[1])
89 else:
90 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[2])
91
92 self.x, self.y = x, y
93 if self.sprite:
94 self.sprite.update()
84 95
85 self.frame += 1 96 self.frame += 1
86 return True 97 return True
87 98
88 99
89 100
90 class EnemyManager(object): 101 class EnemyManager(object):
91 def __init__(self, stage, anims, ecl): 102 def __init__(self, stage, anm_wrapper, ecl):
92 self.stage = stage 103 self.stage = stage
93 self.anims = tuple(anims) 104 self.anm_wrapper = anm_wrapper
94 self.main = [] 105 self.main = []
95 self.subs = {} 106 self.subs = {}
96 self.objects_by_texture = {} 107 self.objects_by_texture = {}
97 self.enemies = [] 108 self.enemies = []
98 109
118 def update(self, frame): 129 def update(self, frame):
119 if self.main and self.main[0][0] == frame: 130 if self.main and self.main[0][0] == frame:
120 for sub, instr_type, args in self.main.pop(0)[1]: 131 for sub, instr_type, args in self.main.pop(0)[1]:
121 if instr_type in (0, 2, 4, 6): # Normal/mirrored enemy 132 if instr_type in (0, 2, 4, 6): # Normal/mirrored enemy
122 x, y, z, life, unknown1, unknown2, unknown3 = args 133 x, y, z, life, unknown1, unknown2, unknown3 = args
123 self.enemies.append(Enemy((x, y), life, instr_type, self.subs[sub], self.anims)) 134 self.enemies.append(Enemy((x, y), life, instr_type, self.subs[sub], self.anm_wrapper))
124 135
125 # Update enemies 136 # Update enemies
126 for enemy in tuple(self.enemies): 137 for enemy in tuple(self.enemies):
127 if not enemy.update(frame): 138 if not enemy.update(frame):
128 self.enemies.remove(enemy) 139 self.enemies.remove(enemy)