changeset 434:18e4b121646b

Move the common parts of EoSDGame outside, to not reallocate them at each stage.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 11 Aug 2013 14:30:52 +0200
parents 1222341ea22c
children 878273a984c4
files eosd pytouhou/games/eosd.py
diffstat 2 files changed, 80 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/eosd
+++ b/eosd
@@ -55,7 +55,7 @@ import logging
 from pytouhou.ui.window import Window
 from pytouhou.resource.loader import Loader
 from pytouhou.ui.gamerunner import GameRunner
-from pytouhou.games.eosd import EoSDGame
+from pytouhou.games.eosd import EoSDCommon, EoSDGame
 from pytouhou.game.game import GameOver
 from pytouhou.game.player import PlayerState
 from pytouhou.formats.t6rp import T6RP, Level
@@ -145,6 +145,7 @@ def main(path, data, stage_num, rank, ch
 
     game_class = EoSDGameBossRush if boss_rush else EoSDGame
 
+    common = EoSDCommon(resource_loader)
     runner = GameRunner(window, resource_loader, skip=skip_replay)
     while True:
         if replay:
@@ -182,7 +183,7 @@ def main(path, data, stage_num, rank, ch
 
         hints_stage = hints.stages[stage_num - 1] if hints else None
 
-        game = game_class(resource_loader, states, stage_num, rank, difficulty, prng=prng, continues=continues, hints=hints_stage)
+        game = game_class(resource_loader, states, stage_num, rank, difficulty, common, prng=prng, continues=continues, hints=hints_stage)
 
         if not enable_particles:
             def new_particle(pos, anim, amp, number=1, reverse=False, duration=24):
--- a/pytouhou/games/eosd.py
+++ b/pytouhou/games/eosd.py
@@ -27,52 +27,64 @@ from pytouhou.game.background import Bac
 from pytouhou.vm.eclrunner import ECLMainRunner
 
 
+class EoSDCommon(object):
+    def __init__(self, resource_loader):
+        self.etama = resource_loader.get_multi_anm(('etama3.anm', 'etama4.anm'))
+        self.bullet_types = [BulletType(self.etama[0], 0, 11, 14, 15, 16, hitbox_size=2,
+                                        type_id=0),
+                             BulletType(self.etama[0], 1, 12, 17, 18, 19, hitbox_size=3,
+                                        type_id=1),
+                             BulletType(self.etama[0], 2, 12, 17, 18, 19, hitbox_size=2,
+                                        type_id=2),
+                             BulletType(self.etama[0], 3, 12, 17, 18, 19, hitbox_size=3,
+                                        type_id=3),
+                             BulletType(self.etama[0], 4, 12, 17, 18, 19, hitbox_size=2.5,
+                                        type_id=4),
+                             BulletType(self.etama[0], 5, 12, 17, 18, 19, hitbox_size=2,
+                                        type_id=5),
+                             BulletType(self.etama[0], 6, 13, 20, 20, 20, hitbox_size=8,
+                                        launch_anim_offsets=(0, 1, 1, 2, 2, 3, 4, 0),
+                                        type_id=6),
+                             BulletType(self.etama[0], 7, 13, 20, 20, 20, hitbox_size=5.5,
+                                        launch_anim_offsets=(1,)*28,
+                                        type_id=7),
+                             BulletType(self.etama[0], 8, 13, 20, 20, 20, hitbox_size=4.5,
+                                        launch_anim_offsets=(0, 1, 1, 2, 2, 3, 4, 0),
+                                        type_id=8),
+                             BulletType(self.etama[1], 0, 1, 2, 2, 2, hitbox_size=16,
+                                        launch_anim_offsets=(0, 1, 2, 3, 4, 5, 6, 7, 8),
+                                        type_id=9)]
+
+        self.laser_types = [LaserType(self.etama[0], 9),
+                            LaserType(self.etama[0], 10)]
+
+        self.item_types = [ItemType(self.etama[0], 0, 7), #Power
+                           ItemType(self.etama[0], 1, 8), #Point
+                           ItemType(self.etama[0], 2, 9), #Big power
+                           ItemType(self.etama[0], 3, 10), #Bomb
+                           ItemType(self.etama[0], 4, 11), #Full power
+                           ItemType(self.etama[0], 5, 12), #1up
+                           ItemType(self.etama[0], 6, 13)] #Star
+
+        self.enemy_face = [('face03a.anm', 'face03b.anm'),
+                           ('face05a.anm',),
+                           ('face06a.anm', 'face06b.anm'),
+                           ('face08a.anm', 'face08b.anm'),
+                           ('face09a.anm', 'face09b.anm'),
+                           ('face09b.anm', 'face10a.anm', 'face10b.anm'),
+                           ('face08a.anm', 'face12a.anm', 'face12b.anm', 'face12c.anm')]
+
+        self.characters = resource_loader.get_eosd_characters()
+        self.interface = EoSDInterface(resource_loader)
+
+
+
 class EoSDGame(Game):
     def __init__(self, resource_loader, player_states, stage, rank, difficulty,
-                 bullet_types=None, laser_types=None, item_types=None,
-                 nb_bullets_max=640, width=384, height=448, prng=None,
+                 common, nb_bullets_max=640, width=384, height=448, prng=None,
                  continues=0, hints=None):
 
-        if not bullet_types:
-            self.etama = resource_loader.get_multi_anm(('etama3.anm', 'etama4.anm'))
-            bullet_types = [BulletType(self.etama[0], 0, 11, 14, 15, 16, hitbox_size=2,
-                                       type_id=0),
-                            BulletType(self.etama[0], 1, 12, 17, 18, 19, hitbox_size=3,
-                                       type_id=1),
-                            BulletType(self.etama[0], 2, 12, 17, 18, 19, hitbox_size=2,
-                                       type_id=2),
-                            BulletType(self.etama[0], 3, 12, 17, 18, 19, hitbox_size=3,
-                                       type_id=3),
-                            BulletType(self.etama[0], 4, 12, 17, 18, 19, hitbox_size=2.5,
-                                       type_id=4),
-                            BulletType(self.etama[0], 5, 12, 17, 18, 19, hitbox_size=2,
-                                       type_id=5),
-                            BulletType(self.etama[0], 6, 13, 20, 20, 20, hitbox_size=8,
-                                       launch_anim_offsets=(0, 1, 1, 2, 2, 3, 4, 0),
-                                       type_id=6),
-                            BulletType(self.etama[0], 7, 13, 20, 20, 20, hitbox_size=5.5,
-                                       launch_anim_offsets=(1,)*28,
-                                       type_id=7),
-                            BulletType(self.etama[0], 8, 13, 20, 20, 20, hitbox_size=4.5,
-                                       launch_anim_offsets=(0, 1, 1, 2, 2, 3, 4, 0),
-                                       type_id=8),
-                            BulletType(self.etama[1], 0, 1, 2, 2, 2, hitbox_size=16,
-                                       launch_anim_offsets=(0, 1, 2, 3, 4, 5, 6, 7, 8),
-                                       type_id=9)]
-
-        if not laser_types:
-            laser_types = [LaserType(self.etama[0], 9),
-                           LaserType(self.etama[0], 10)]
-
-        if not item_types:
-            item_types = [ItemType(self.etama[0], 0, 7), #Power
-                          ItemType(self.etama[0], 1, 8), #Point
-                          ItemType(self.etama[0], 2, 9), #Big power
-                          ItemType(self.etama[0], 3, 10), #Bomb
-                          ItemType(self.etama[0], 4, 11), #Full power
-                          ItemType(self.etama[0], 5, 12), #1up
-                          ItemType(self.etama[0], 6, 13)] #Star
-
+        self.etama = common.etama #XXX
         try:
             self.enm_anm = resource_loader.get_multi_anm(('stg%denm.anm' % stage,
                                                           'stg%denm2.anm' % stage))
@@ -84,18 +96,11 @@ class EoSDGame(Game):
         self.spellcard_effect_anm = resource_loader.get_single_anm('eff0%d.anm' % stage)
 
         player_face = player_states[0].character // 2
-        enemy_face = [('face03a.anm', 'face03b.anm'),
-                      ('face05a.anm',),
-                      ('face06a.anm', 'face06b.anm'),
-                      ('face08a.anm', 'face08b.anm'),
-                      ('face09a.anm', 'face09b.anm'),
-                      ('face09b.anm', 'face10a.anm', 'face10b.anm'),
-                      ('face08a.anm', 'face12a.anm', 'face12b.anm', 'face12c.anm')]
         self.msg = resource_loader.get_msg('msg%d.dat' % stage)
         msg_anm = [resource_loader.get_multi_anm(('face0%da.anm' % player_face,
                                                   'face0%db.anm' % player_face,
                                                   'face0%dc.anm' % player_face)),
-                   resource_loader.get_multi_anm(enemy_face[stage - 1])]
+                   resource_loader.get_multi_anm(common.enemy_face[stage - 1])]
 
         self.msg_anm = [[], []]
         for i, anms in enumerate(msg_anm):
@@ -103,11 +108,9 @@ class EoSDGame(Game):
                 for sprite in anm.sprites.values():
                     self.msg_anm[i].append((anm, sprite))
 
-        characters = resource_loader.get_eosd_characters()
-        players = [EoSDPlayer(state, self, resource_loader, characters[state.character]) for state in player_states]
+        players = [EoSDPlayer(state, self, resource_loader, common.characters[state.character]) for state in player_states]
 
-        self.stage = stage #XXX
-        interface = EoSDInterface(self, resource_loader)
+        common.interface.start_stage(self, stage)
 
         # Load stage data
         self.std = resource_loader.get_stage('stage%d.std' % stage)
@@ -118,14 +121,15 @@ class EoSDGame(Game):
         self.resource_loader = resource_loader #XXX: currently used for texture preload in pytouhou.ui.gamerunner. Wipe it!
 
         Game.__init__(self, players, stage, rank, difficulty,
-                      bullet_types, laser_types, item_types, nb_bullets_max,
-                      width, height, prng, interface, continues, hints)
+                      common.bullet_types, common.laser_types,
+                      common.item_types, nb_bullets_max, width, height, prng,
+                      common.interface, continues, hints)
 
 
 
 class EoSDInterface(object):
-    def __init__(self, game, resource_loader):
-        self.game = game
+    def __init__(self, resource_loader):
+        self.game = None
         front = resource_loader.get_single_anm('front.anm')
         self.ascii_anm = resource_loader.get_single_anm('ascii.anm')
 
@@ -143,10 +147,7 @@ class EoSDInterface(object):
         for item in self.items:
             item.sprite.allow_dest_offset = True #XXX
 
-        self.level_start = [Text((176, 200), self.ascii_anm, text='STAGE %d' % game.stage)] #TODO: find the exact location.
-        self.level_start[0].set_timeout(240, effect='fadeout', duration=60, start=120)
-        self.level_start[0].set_color('yellow')
-        #TODO: use the system text for the stage name, and the song name.
+        self.level_start = []
 
         self.labels = {
             'highscore': Text((500, 58), self.ascii_anm, front, text='0'),
@@ -174,6 +175,20 @@ class EoSDInterface(object):
             item.sprite.allow_dest_offset = True #XXX
 
 
+    def start_stage(self, game, stage):
+        self.game = game
+        if stage < 6:
+            text = 'STAGE %d' % stage
+        elif stage == 6:
+            text = 'FINAL STAGE'
+        elif stage == 7:
+            text = 'EXTRA STAGE'
+        self.level_start = [Text((16+384/2, 200), self.ascii_anm, text=text, align='center')] #TODO: find the exact location.
+        self.level_start[0].set_timeout(240, effect='fadeout', duration=60, start=120)
+        self.level_start[0].set_color('yellow')
+        #TODO: use the system text for the stage name, and the song name.
+
+
     def set_boss_life(self):
         if not self.game.boss:
             return