Mercurial > touhou
comparison pytouhou/game/enemymanager.py @ 36:f46c18872796
Minor refactoring
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 14 Aug 2011 14:58:30 +0200 |
parents | 4d93d45ecb62 |
children | a10e3f44a883 |
comparison
equal
deleted
inserted
replaced
35:9027692abd79 | 36:f46c18872796 |
---|---|
13 self.anm_wrapper = anm_wrapper | 13 self.anm_wrapper = anm_wrapper |
14 self.anm = None | 14 self.anm = None |
15 self.ecl_runner = ecl_runner | 15 self.ecl_runner = ecl_runner |
16 self.x, self.y = pos | 16 self.x, self.y = pos |
17 self.life = life | 17 self.life = life |
18 self.max_life = life | |
18 self.type = _type | 19 self.type = _type |
19 self.frame = 0 | 20 self.frame = 0 |
20 self.sprite = None | 21 self.sprite = None |
22 self.pending_bullets = [] | |
23 self.bullet_attributes = [] | |
24 self.bullet_launch_offset = (0, 0) | |
21 | 25 |
22 self.death_sprite = None | 26 self.death_sprite = None |
23 self.movement_dependant_sprites = None | 27 self.movement_dependant_sprites = None |
24 self.direction = None | 28 self.direction = None |
25 self.interpolator = None #TODO | 29 self.interpolator = None #TODO |
28 self.rotation_speed = 0. | 32 self.rotation_speed = 0. |
29 self.acceleration = 0. | 33 self.acceleration = 0. |
30 | 34 |
31 self.hitbox = (0, 0) | 35 self.hitbox = (0, 0) |
32 | 36 |
33 self.ecl_runner.implementation.update({97: ('I', self.set_sprite), | 37 self.ecl_runner.implementation.update({#67: ('HHIIffffI', self.set_bullet_attributes), |
38 97: ('I', self.set_sprite), | |
34 98: ('HHHHHH', self.set_multiple_sprites), | 39 98: ('HHHHHH', self.set_multiple_sprites), |
35 45: ('ff', self.set_angle_speed), | 40 45: ('ff', self.set_angle_speed), |
36 43: ('fff', self.set_pos), | 41 43: ('fff', self.set_pos), |
37 46: ('f', self.set_rotation_speed), | 42 46: ('f', self.set_rotation_speed), |
38 47: ('f', self.set_speed), | 43 47: ('f', self.set_speed), |
41 57: ('Ifff', self.move_to), | 46 57: ('Ifff', self.move_to), |
42 100: ('I', self.set_death_sprite), | 47 100: ('I', self.set_death_sprite), |
43 103: ('fff', self.set_hitbox)}) #TODO | 48 103: ('fff', self.set_hitbox)}) #TODO |
44 | 49 |
45 | 50 |
51 | |
46 def set_death_sprite(self, sprite_index): | 52 def set_death_sprite(self, sprite_index): |
47 self.death_sprite = sprite_index % 256 #TODO | 53 self.death_sprite = sprite_index % 256 #TODO |
48 | 54 |
49 | 55 |
50 def set_hitbox(self, width, height, depth): | 56 def set_hitbox(self, width, height, depth): |
111 max_y <= -self.y)): | 117 max_y <= -self.y)): |
112 return False | 118 return False |
113 return True | 119 return True |
114 | 120 |
115 | 121 |
122 def get_objects_by_texture(self): | |
123 objects_by_texture = {} | |
124 key = self.anm.first_name, self.anm.secondary_name | |
125 if not key in objects_by_texture: | |
126 objects_by_texture[key] = (0, [], []) | |
127 vertices = tuple((x + self.x, y + self.y, z) for x, y, z in self.sprite._vertices) | |
128 objects_by_texture[key][1].extend(vertices) | |
129 objects_by_texture[key][2].extend(self.sprite._uvs) | |
130 #TODO: effects/bullet launch | |
131 return objects_by_texture | |
132 | |
133 | |
116 def update(self, frame): | 134 def update(self, frame): |
117 self.ecl_runner.update() | 135 self.ecl_runner.update() |
118 | 136 |
119 x, y = self.x, self.y | 137 x, y = self.x, self.y |
120 if self.interpolator: | 138 if self.interpolator: |
137 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[2]) | 155 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[2]) |
138 self.direction = -1 | 156 self.direction = -1 |
139 elif x > self.x: | 157 elif x > self.x: |
140 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[3]) | 158 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[3]) |
141 self.direction = +1 | 159 self.direction = +1 |
142 #TODO: end_animation | |
143 elif self.direction is not None: | 160 elif self.direction is not None: |
144 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[{-1: 0, +1:1}[self.direction]]) | 161 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[{-1: 0, +1:1}[self.direction]]) |
145 self.direction = None | 162 self.direction = None |
146 | 163 |
147 self.x, self.y = x, y | 164 self.x, self.y = x, y |
154 visible = False | 171 visible = False |
155 self.sprite = None | 172 self.sprite = None |
156 else: | 173 else: |
157 visible = False | 174 visible = False |
158 | 175 |
176 | |
159 self.frame += 1 | 177 self.frame += 1 |
160 return visible | 178 return visible |
161 | 179 |
162 | 180 |
163 | 181 |
189 for sub, instr_type, args in self.main.pop(0)[1]: | 207 for sub, instr_type, args in self.main.pop(0)[1]: |
190 if instr_type in (0, 2, 4, 6): # Normal/mirrored enemy | 208 if instr_type in (0, 2, 4, 6): # Normal/mirrored enemy |
191 x, y, z, life, unknown1, unknown2, unknown3 = args | 209 x, y, z, life, unknown1, unknown2, unknown3 = args |
192 ecl_runner = ECLRunner(self.ecl, sub) | 210 ecl_runner = ECLRunner(self.ecl, sub) |
193 enemy = Enemy((x, y), life, instr_type, ecl_runner, self.anm_wrapper) | 211 enemy = Enemy((x, y), life, instr_type, ecl_runner, self.anm_wrapper) |
194 | |
195 ecl_runner.implementation[1] = ('I', self.make_enemy_deleter(enemy)) | 212 ecl_runner.implementation[1] = ('I', self.make_enemy_deleter(enemy)) |
196 | 213 |
197 self.enemies.append(enemy) | 214 self.enemies.append(enemy) |
198 | 215 |
199 # Update enemies | 216 # Update enemies |
200 visible_enemies = [enemy for enemy in self.enemies if enemy.update(frame)] | 217 visible_enemies = [enemy for enemy in self.enemies if enemy.update(frame)] |
201 | 218 |
202 # Add enemies to vertices/uvs | 219 # Add enemies to vertices/uvs |
203 self.objects_by_texture = {} | 220 self.objects_by_texture = {} |
204 for enemy in visible_enemies: | 221 for enemy in visible_enemies: |
205 ox, oy = enemy.x, enemy.y | |
206 if enemy.is_visible(384, 448): #TODO | 222 if enemy.is_visible(384, 448): #TODO |
207 key = enemy.anm.first_name, enemy.anm.secondary_name | 223 for key, (count, vertices, uvs) in enemy.get_objects_by_texture().items(): |
208 if not key in self.objects_by_texture: | 224 if not key in self.objects_by_texture: |
209 self.objects_by_texture[key] = (0, [], []) | 225 self.objects_by_texture[key] = (0, [], []) |
210 vertices = tuple((x + ox, y + oy, z) for x, y, z in enemy.sprite._vertices) | 226 self.objects_by_texture[key][1].extend(vertices) |
211 self.objects_by_texture[key][2].extend(enemy.sprite._uvs) | 227 self.objects_by_texture[key][2].extend(uvs) |
212 self.objects_by_texture[key][1].extend(vertices) | |
213 for key, (nb_vertices, vertices, uvs) in self.objects_by_texture.items(): | 228 for key, (nb_vertices, vertices, uvs) in self.objects_by_texture.items(): |
214 nb_vertices = len(vertices) | 229 nb_vertices = len(vertices) |
215 vertices = pack('f' * (3 * nb_vertices), *chain(*vertices)) | 230 vertices = pack('f' * (3 * nb_vertices), *chain(*vertices)) |
216 uvs = pack('f' * (2 * nb_vertices), *chain(*uvs)) | 231 uvs = pack('f' * (2 * nb_vertices), *chain(*uvs)) |
217 self.objects_by_texture[key] = (nb_vertices, vertices, uvs) | 232 self.objects_by_texture[key] = (nb_vertices, vertices, uvs) |