comparison pytouhou/game/enemymanager.py @ 85:3804f07d3b0e

Various optimizations
author Thibaut Girka <thib@sitedethib.com>
date Sat, 03 Sep 2011 23:34:12 +0200
parents fc0294c745b6
children 630e9045e851
comparison
equal deleted inserted replaced
84:1a0c78e5a941 85:3804f07d3b0e
145 145
146 def stop_in(self, duration, formula): 146 def stop_in(self, duration, formula):
147 if not self.speed_interpolator: 147 if not self.speed_interpolator:
148 self.speed_interpolator = Interpolator((self.speed,), formula) 148 self.speed_interpolator = Interpolator((self.speed,), formula)
149 self.speed_interpolator.set_interpolation_start(self.frame, (self.speed,)) 149 self.speed_interpolator.set_interpolation_start(self.frame, (self.speed,))
150 self.speed_interpolator.set_interpolation_end(self.frame + duration, (0.,)) 150 self.speed_interpolator.set_interpolation_end(self.frame + duration - 1, (0.,))
151 151
152 self.speed = 0. 152 self.speed = 0.
153 153
154 154
155 def is_visible(self, screen_width, screen_height): 155 def is_visible(self, screen_width, screen_height):
171 max_y < -self.y)): 171 max_y < -self.y)):
172 return False 172 return False
173 return True 173 return True
174 174
175 175
176 def get_objects_by_texture(self): 176 def get_objects_by_texture(self, objects_by_texture):
177 objects_by_texture = {} 177 for bullet in self.bullets:
178 178 bullet.get_objects_by_texture(objects_by_texture)
179 for bullet in self.bullets:
180 objects_by_texture.update(bullet.get_objects_by_texture())
181 179
182 if not self._sprite: 180 if not self._sprite:
183 return objects_by_texture 181 return
184 182
185 key = self._sprite.anm.first_name, self._sprite.anm.secondary_name 183 key = self._sprite.anm.first_name, self._sprite.anm.secondary_name
186 key = (key, self._sprite.blendfunc) 184 key = (key, self._sprite.blendfunc)
187 if not key in objects_by_texture: 185 if not key in objects_by_texture:
188 objects_by_texture[key] = (0, [], [], []) 186 objects_by_texture[key] = (0, [], [], [])
189 vertices = tuple((x + self.x, y + self.y, z) for x, y, z in self._sprite._vertices) 187 vertices = tuple((x + self.x, y + self.y, z) for x, y, z in self._sprite._vertices)
190 objects_by_texture[key][1].extend(vertices) 188 objects_by_texture[key][1].extend(vertices)
191 objects_by_texture[key][2].extend(self._sprite._uvs) 189 objects_by_texture[key][2].extend(self._sprite._uvs)
192 objects_by_texture[key][3].extend(self._sprite._colors) 190 objects_by_texture[key][3].extend(self._sprite._colors)
193 #TODO: effects/bullet launch
194 return objects_by_texture
195 191
196 192
197 def update(self): 193 def update(self):
198 x, y = self.x, self.y 194 x, y = self.x, self.y
199 if self.interpolator: 195 if self.interpolator:
284 for frame, sub, instr_type, args in ecl.main: 280 for frame, sub, instr_type, args in ecl.main:
285 if not self.main or self.main[-1][0] < frame: 281 if not self.main or self.main[-1][0] < frame:
286 self.main.append((frame, [(sub, instr_type, args)])) 282 self.main.append((frame, [(sub, instr_type, args)]))
287 elif self.main[-1][0] == frame: 283 elif self.main[-1][0] == frame:
288 self.main[-1][1].append((sub, instr_type, args)) 284 self.main[-1][1].append((sub, instr_type, args))
285
286
287 def get_objects_by_texture(self, objects_by_texture):
288 # Add enemies to vertices/uvs
289 for enemy in self.enemies:
290 enemy.get_objects_by_texture(objects_by_texture)
291
292 # Add bullets to vertices/uvs
293 for bullet in self.bullets:
294 bullet.get_objects_by_texture(objects_by_texture)
289 295
290 296
291 def update(self, frame): 297 def update(self, frame):
292 if self.main and self.main[0][0] == frame: 298 if self.main and self.main[0][0] == frame:
293 for sub, instr_type, args in self.main.pop(0)[1]: 299 for sub, instr_type, args in self.main.pop(0)[1]:
342 348
343 #TODO: disable boss mode if it is dead/it has timeout 349 #TODO: disable boss mode if it is dead/it has timeout
344 if self._game_state.boss and self._game_state.boss._removed: 350 if self._game_state.boss and self._game_state.boss._removed:
345 self._game_state.boss = None 351 self._game_state.boss = None
346 352
347 # Add enemies to vertices/uvs 353 #TODO
348 self.objects_by_texture = {} 354 self.objects_by_texture = {}
349 for enemy in visible_enemies: 355 self.get_objects_by_texture(self.objects_by_texture)
350 for key, (count, vertices, uvs, colors) in enemy.get_objects_by_texture().items():
351 if not key in self.objects_by_texture:
352 self.objects_by_texture[key] = (0, [], [], [])
353 self.objects_by_texture[key][1].extend(vertices)
354 self.objects_by_texture[key][2].extend(uvs)
355 self.objects_by_texture[key][3].extend(colors)
356
357 # Add bullets to vertices/uvs
358 for bullet in self.bullets:
359 for key, (count, vertices, uvs, colors) in bullet.get_objects_by_texture().items():
360 if not key in self.objects_by_texture:
361 self.objects_by_texture[key] = (0, [], [], [])
362 self.objects_by_texture[key][1].extend(vertices)
363 self.objects_by_texture[key][2].extend(uvs)
364 self.objects_by_texture[key][3].extend(colors)
365
366 for key, (nb_vertices, vertices, uvs, colors) in self.objects_by_texture.items(): 356 for key, (nb_vertices, vertices, uvs, colors) in self.objects_by_texture.items():
367 nb_vertices = len(vertices) 357 nb_vertices = len(vertices)
368 vertices = pack('f' * (3 * nb_vertices), *chain(*vertices)) 358 vertices = pack('f' * (3 * nb_vertices), *chain(*vertices))
369 uvs = pack('f' * (2 * nb_vertices), *chain(*uvs)) 359 uvs = pack('f' * (2 * nb_vertices), *chain(*uvs))
370 colors = pack('B' * (4 * nb_vertices), *chain(*colors)) 360 colors = pack('B' * (4 * nb_vertices), *chain(*colors))