diff pytouhou/game/game.py @ 97:ac2e5e1c2c3c

Refactor \o/
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 23:50:00 +0200
parents fc0294c745b6
children d141c851c598
line wrap: on
line diff
--- a/pytouhou/game/game.py
+++ b/pytouhou/game/game.py
@@ -15,10 +15,16 @@
 
 from pytouhou.utils.random import Random
 
+from pytouhou.vm.eclrunner import ECLMainRunner
+
+from pytouhou.game.enemymanager import Enemy #TODO: enemymanager -> enemy
+
+
 class GameState(object):
-    __slots__ = ('resources', 'players', 'rank', 'difficulty', 'frame', 'stage', 'boss', 'prng')
-    def __init__(self, resources, players, stage, rank, difficulty):
-        self.resources = resources
+    __slots__ = ('resource_loader', 'players', 'rank', 'difficulty', 'frame',
+                 'stage', 'boss', 'prng')
+    def __init__(self, resource_loader, players, stage, rank, difficulty):
+        self.resource_loader = resource_loader
 
         self.stage = stage
         self.players = players
@@ -29,9 +35,78 @@ class GameState(object):
         self.frame = 0
 
 
-class Resources(object):
-    def __init__(self, etama_anm_wrappers, players_anm_wrappers, effects_anm_wrapper):
-        self.etama_anm_wrappers = etama_anm_wrappers
-        self.players_anm_wrappers = players_anm_wrappers
-        self.effects_anm_wrapper = effects_anm_wrapper
+
+class Game(object):
+    def __init__(self, resource_loader, players, stage, rank, difficulty):
+        self.game_state = GameState(resource_loader, players, stage, rank, difficulty)
+
+        self.enemies = []
+
+        self.bullets = []
+        self.bonuses = []
+
+        self.enm_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage,
+                                                                 'stg%denm2.anm' % stage))
+        ecl = resource_loader.get_ecl('ecldata%d.ecl' % stage)
+        self.ecl_runner = ECLMainRunner(ecl, self.new_enemy, self.game_state)
+
+
+    def get_objects_by_texture(self, objects_by_texture):
+        #TODO: move elsewhere
+        for enemy in self.enemies:
+            enemy.get_objects_by_texture(objects_by_texture)
+
+        for bullet in self.bullets:
+            bullet.get_objects_by_texture(objects_by_texture)
+
+
+    def new_enemy(self, pos, life, instr_type):
+        enemy = Enemy(pos, life, instr_type, self.enm_anm_wrapper, self.game_state)
+        self.enemies.append(enemy)
+        return enemy
+
+
+    def run_iter(self, keystate):
+        # 1. VMs.
+        self.ecl_runner.run_iter()
+
+        # 2. Filter out destroyed enemies
+        self.enemies[:] = (enemy for enemy in self.enemies if not enemy._removed)
 
+        # 3. Let's play!
+        for enemy in self.enemies:
+            enemy.update()
+            for bullet in tuple(enemy.bullets):
+                if bullet._launched:
+                    enemy.bullets.remove(bullet)
+                self.bullets.append(bullet)
+        for bullet in self.bullets:
+            bullet.update()
+
+
+        # 4. Cleaning
+        self.cleanup()
+
+        self.game_state.frame += 1
+
+
+    def cleanup(self):
+        # Filter out non-visible enemies
+        for enemy in tuple(self.enemies):
+            if enemy.is_visible(384, 448): #TODO
+                enemy._was_visible = True
+            elif enemy._was_visible:
+                # Filter out-of-screen enemy
+                enemy._removed = True
+                self.enemies.remove(enemy)
+
+        # Filter out-of-scren bullets
+        # TODO: was_visible thing
+        for bullet in tuple(self.bullets):
+            if not bullet.is_visible(384, 448):
+                self.bullets.remove(bullet)
+
+        # Disable boss mode if it is dead/it has timeout
+        if self.game_state.boss and self.game_state.boss._removed:
+            self.game_state.boss = None
+