comparison pytouhou/game/enemymanager.py @ 97:ac2e5e1c2c3c

Refactor \o/
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 23:50:00 +0200
parents ca571697ec83
children
comparison
equal deleted inserted replaced
96:54929d495654 97:ac2e5e1c2c3c
260 bullet.update() 260 bullet.update()
261 261
262 262
263 self.frame += 1 263 self.frame += 1
264 264
265
266
267 class EnemyManager(object):
268 def __init__(self, stage, anm_wrapper, ecl, game_state):
269 self._game_state = game_state
270 self.stage = stage
271 self.anm_wrapper = anm_wrapper
272 self.main = []
273 self.ecl = ecl
274 self.enemies = []
275 self.processes = []
276 self.bullets = []
277
278 # Populate main
279 for frame, sub, instr_type, args in ecl.main:
280 if not self.main or self.main[-1][0] < frame:
281 self.main.append((frame, [(sub, instr_type, args)]))
282 elif self.main[-1][0] == frame:
283 self.main[-1][1].append((sub, instr_type, args))
284
285
286 def get_objects_by_texture(self, objects_by_texture):
287 # Add enemies to vertices/uvs
288 for enemy in self.enemies:
289 enemy.get_objects_by_texture(objects_by_texture)
290
291 # Add bullets to vertices/uvs
292 for bullet in self.bullets:
293 bullet.get_objects_by_texture(objects_by_texture)
294
295
296 def update(self, frame):
297 if self.main and self.main[0][0] == frame:
298 for sub, instr_type, args in self.main.pop(0)[1]:
299 if instr_type in (0, 2, 4, 6) and not self._game_state.boss:
300 x, y, z, life, unknown1, unknown2, unknown3 = args
301 if instr_type & 4:
302 if x < -990: #102h.exe@0x411820
303 x = self._game_state.prng.rand_double() * 368
304 if y < -990: #102h.exe@0x41184b
305 y = self._game_state.prng.rand_double() * 416
306 if z < -990: #102h.exe@0x411881
307 y = self._game_state.prng.rand_double() * 800
308 enemy = Enemy((x, y), life, instr_type, self.anm_wrapper, self._game_state)
309 self.enemies.append(enemy)
310 self.processes.append(ECLRunner(self.ecl, sub, enemy, self._game_state))
311
312
313 # Run processes
314 self.processes[:] = (process for process in self.processes if process.run_iteration())
315
316 # Filter of destroyed enemies
317 self.enemies[:] = (enemy for enemy in self.enemies if not enemy._removed)
318
319 # Update enemies
320 for enemy in self.enemies:
321 enemy.update()
322 for bullet in tuple(enemy.bullets):
323 if bullet._launched:
324 enemy.bullets.remove(bullet)
325 self.bullets.append(bullet)
326
327 # Update bullets
328 for bullet in self.bullets:
329 bullet.update()
330
331 # Filter out non-visible enemies
332 visible_enemies = [enemy for enemy in self.enemies if enemy.is_visible(384, 448)] #TODO
333 for enemy in visible_enemies:
334 enemy._was_visible = True
335
336 # Filter out-of-screen enemies
337 for enemy in tuple(self.enemies):
338 if enemy._was_visible and not enemy in visible_enemies:
339 enemy._removed = True
340 self.enemies.remove(enemy)
341
342 # Filter out-of-scren bullets
343 for bullet in tuple(self.bullets):
344 if not bullet.is_visible(384, 448):
345 self.bullets.remove(bullet)
346
347
348 #TODO: disable boss mode if it is dead/it has timeout
349 if self._game_state.boss and self._game_state.boss._removed:
350 self._game_state.boss = None
351