changeset 597:244c99c568c8

Don’t hardcode the available games and interfaces, and relocation them.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 25 Oct 2014 18:52:16 +0200
parents ab131d04987d
children a7286a0facf9
files pytouhou/games/eosd.py pytouhou/games/eosd/__init__.py pytouhou/games/eosd/game.py pytouhou/games/eosd/interface.py pytouhou/games/sample/__init__.py pytouhou/games/sample/interface.py pytouhou/interfaces/__init__.py pytouhou/interfaces/eosd.py pytouhou/interfaces/sample.py pytouhou/options.py scripts/pytouhou
diffstat 8 files changed, 34 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
rename from pytouhou/games/eosd.py
rename to pytouhou/games/eosd/game.py
--- a/pytouhou/games/eosd.py
+++ b/pytouhou/games/eosd/game.py
@@ -14,18 +14,18 @@
 
 from pytouhou.utils.interpolator import Interpolator
 
-from pytouhou.game.game import Game
+from pytouhou.game.game import Game as GameBase
 from pytouhou.game.bullettype import BulletType
 from pytouhou.game.lasertype import LaserType
 from pytouhou.game.itemtype import ItemType
-from pytouhou.game.player import Player
+from pytouhou.game.player import Player as PlayerBase
 from pytouhou.game.orb import Orb
 from pytouhou.game.background import Background
 
 from pytouhou.vm import ECLMainRunner
 
 
-class EoSDCommon(object):
+class Common(object):
     def __init__(self, resource_loader, player_characters, continues, stage,
                  width=384, height=448):
         self.width, self.height = width, height
@@ -90,13 +90,13 @@ class EoSDCommon(object):
                 anm = resource_loader.get_single_anm('player0%d.anm' % character)
                 self.player_anms[character] = (anm, face)
 
-            self.players[i] = EoSDPlayer(i, self.player_anms[character][0],
-                                         eosd_characters[player_character],
-                                         character, default_power, continues)
+            self.players[i] = Player(i, self.player_anms[character][0],
+                                     eosd_characters[player_character],
+                                     character, default_power, continues)
 
 
 
-class EoSDGame(Game):
+class Game(GameBase):
     def __init__(self, resource_loader, stage, rank, difficulty,
                  common, prng, hints=None, friendly_fire=True,
                  nb_bullets_max=640):
@@ -133,11 +133,11 @@ class EoSDGame(Game):
 
         common.interface.start_stage(self, stage)
 
-        Game.__init__(self, common.players, stage, rank, difficulty,
-                      common.bullet_types, common.laser_types,
-                      common.item_types, nb_bullets_max, common.width,
-                      common.height, prng, common.interface, hints,
-                      friendly_fire)
+        GameBase.__init__(self, common.players, stage, rank, difficulty,
+                          common.bullet_types, common.laser_types,
+                          common.item_types, nb_bullets_max, common.width,
+                          common.height, prng, common.interface, hints,
+                          friendly_fire)
 
         try:
             self.texts['stage_name'] = common.interface.stage_name
@@ -151,12 +151,12 @@ class EoSDGame(Game):
 
 
 
-class EoSDPlayer(Player):
+class Player(PlayerBase):
     def __init__(self, number, anm, shts, character, power, continues):
         self.sht = shts[0]
         self.focused_sht = shts[1]
 
-        Player.__init__(self, number, anm, character, power, continues)
+        PlayerBase.__init__(self, number, anm, character, power, continues)
 
         self.orbs = [Orb(anm, 128, self),
                      Orb(anm, 129, self)]
@@ -192,7 +192,7 @@ class EoSDPlayer(Player):
 
 
     def update(self, keystate):
-        Player.update(self, keystate)
+        PlayerBase.update(self, keystate)
 
         if self.death_time == 0 or self._game.frame - self.death_time > 60:
             if self.orb_dx_interpolator:
rename from pytouhou/interfaces/eosd.py
rename to pytouhou/games/eosd/interface.py
--- a/pytouhou/interfaces/eosd.py
+++ b/pytouhou/games/eosd/interface.py
@@ -16,7 +16,7 @@ from pytouhou.game.effect import Effect
 from pytouhou.game.text import Text, Counter, Gauge, NativeText
 
 
-class EoSDInterface(object):
+class Interface(object):
     width = 640
     height = 480
     game_pos = (32, 16)
new file mode 100644
rename from pytouhou/interfaces/sample.py
rename to pytouhou/games/sample/interface.py
--- a/pytouhou/interfaces/sample.py
+++ b/pytouhou/games/sample/interface.py
@@ -12,7 +12,7 @@
 ## GNU General Public License for more details.
 ##
 
-class SampleInterface(object):
+class Interface(object):
     width = 384
     height = 448
     game_pos = (0, 0)
deleted file mode 100644
--- a/pytouhou/options.py
+++ b/pytouhou/options.py
@@ -125,8 +125,8 @@ def parse_arguments(defaults):
     game_group.add_argument('-r', '--rank', metavar='RANK', type=int, help='Rank, from 0 (Easy, default) to 3 (Lunatic).')
     game_group.add_argument('-c', '--character', metavar='CHARACTER', type=int, help='Select the character to use, from 0 (ReimuA, default) to 3 (MarisaB).')
     game_group.add_argument('-b', '--boss-rush', action='store_true', help='Fight only bosses.')
-    game_group.add_argument('--game', metavar='GAME', choices=['EoSD'], help='Select the game engine to use.')
-    game_group.add_argument('--interface', metavar='INTERFACE', choices=['EoSD', 'Sample'], help='Select the interface to use.')
+    game_group.add_argument('--game', metavar='GAME', help='Select the game engine to use.')
+    game_group.add_argument('--interface', metavar='INTERFACE', help='Select the interface to use.')
     game_group.add_argument('--hints', metavar='HINTS', help='Hints file, to display text while playing.')
 
     replay_group = parser.add_argument_group('Replay options')
--- a/scripts/pytouhou
+++ b/scripts/pytouhou
@@ -24,8 +24,8 @@ defaults = {'data': default_data,
             'path': '.',
             'rank': 0,
             'character': 0,
-            'game': 'EoSD',
-            'interface': 'EoSD',
+            'game': 'eosd',
+            'interface': 'eosd',
             'port': 0,
             'backend': ['opengl', 'sdl'],
             'gl-flavor': 'compatibility',
@@ -57,14 +57,21 @@ if not args.no_menu:
     else:
         menu(options, args)
 
-if args.game == 'EoSD':
-    from pytouhou.games.eosd import EoSDCommon as Common, EoSDGame as Game
+import sys
+from importlib import import_module
 
-if args.interface == 'EoSD':
-    from pytouhou.interfaces.eosd import EoSDInterface as Interface
-elif args.interface == 'Sample':
-    from pytouhou.interfaces.sample import SampleInterface as Interface
+def load_module(type_, name, items=None):
+    try:
+        module = import_module('pytouhou.games.%s.%s' % (name, type_))
+    except ImportError:
+        logger.critical('Module “%s” doesn’t contain %s data, aborting.', name, type_)
+        sys.exit(1)
+    if items is None:
+        return module
+    return (getattr(module, item) for item in items)
 
+Game, Common = load_module('game', args.game, ['Game', 'Common'])
+Interface = load_module('interface', args.interface).Interface
 
 from pytouhou.lib.sdl import SDL, show_simple_message_box
 from pytouhou.ui.window import Window
@@ -77,7 +84,6 @@ from pytouhou.formats.hint import Hint
 from pytouhou.network import Network
 
 
-from importlib import import_module
 for backend_name in args.backend:
     if backend_name == 'opengl':
         options = {
@@ -146,7 +152,6 @@ def main(window, path, data, stage_num, 
         resource_loader.scan_archives(data)
     except IOError:
         show_simple_message_box(u'Some data files were not found, did you forget the -p option?')
-        import sys
         sys.exit(1)
 
     if stage_num is None: