From: Simon Cross Date: Sat, 17 May 2014 11:26:31 +0000 (+0200) Subject: Add save-game loading. X-Git-Tag: 0.1~122 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=a97bdc61b9c4049e2aa20278c814b8bb8b8273cc;p=naja.git Add save-game loading. --- diff --git a/naja/__main__.py b/naja/__main__.py index 02f49a3..5b76b2d 100644 --- a/naja/__main__.py +++ b/naja/__main__.py @@ -6,9 +6,10 @@ import pygame.locals as pgl from naja.constants import SCREEN from naja.engine import Engine from naja.sound import sound -from naja.options import parse_args +from naja.options import parse_args, options from naja.resources.loader import Loader from naja.scenes.menu import MenuScene +from naja.utils import warp_to_game_state def main(): @@ -34,6 +35,10 @@ def main(): screen = pygame.display.get_surface() scene = MenuScene(None) engine = Engine(screen, scene, None) + + if options.game_state is not None: + warp_to_game_state(options.game_state) + engine.run() diff --git a/naja/constants.py b/naja/constants.py index ae812f7..1205b0b 100644 --- a/naja/constants.py +++ b/naja/constants.py @@ -18,6 +18,7 @@ DEFAULTS = dict( # Debug flags: initial_bits=None, cheat_enabled=False, + game_state=None, ) # Sound constants diff --git a/naja/options.py b/naja/options.py index 6fe7566..d37f1be 100644 --- a/naja/options.py +++ b/naja/options.py @@ -9,6 +9,41 @@ from naja.constants import DEFAULTS options = AttrDict() +def check_min_max(option, value, min_value, max_value): + ''' + Check value lies between min and max and raise OptionValueError if it does + not. + ''' + if not (min_value <= value <= max_value): + raise optparse.OptionValueError( + "Value of %s should be between %s and %s but got: %r" + % (option.dest, min, max, value)) + + +def load_game(option, opt_str, value, parser): + ''' + Load a save game and store it in parser.values.game_state. + ''' + check_min_max(option, value, 0, 7) + # madness takes its toll ... + options.save_location = parser.values.save_location + from naja.scenes.load_save import SaveGameSlot + state = SaveGameSlot(value).load() + if state is None: + raise optparse.OptionValueError( + "Could not load game from slot %s" % (value,)) + parser.values.game_state = state + + +def load_deck(option, opt_str, value, parser): + ''' + Create a new game for a specific deck and store it in + parser.values.game_state. + ''' + raise optparse.OptionalValueError( + "Deck loading not implemented.") + + def parse_args(args): ''' Parse arguments and store them in the options dictionary. @@ -38,10 +73,12 @@ def parse_args(args): parser.add_option('--cheat-enabled', default=False, action='store_true', help='For those too lazy to type the KONAMI code') - parser.add_option('--deck', default=None, + parser.add_option('--deck', default=None, action="callback", + callback=load_deck, help='Start with a new game for a specific deck') - parser.add_option('--load', default=None, - help='Start with a specific save game loaded') + parser.add_option('--load', default=None, type=int, action="callback", + callback=load_game, + help='Start with a specific save game loaded (0-7)') opts, _ = parser.parse_args(args) diff --git a/naja/utils.py b/naja/utils.py index d69cd4a..b15302a 100644 --- a/naja/utils.py +++ b/naja/utils.py @@ -25,3 +25,10 @@ def move_glyph(move_name): 'BISHOP': u'\u265d', 'KNIGHT': u'\u265e', }.get(move_name, move_name.lower()) + + +def warp_to_game_state(game_state): + from naja.events import LoadGameEvent, SceneChangeEvent + from naja.scenes.game import GameScene + LoadGameEvent.post(game_state) + SceneChangeEvent.post(GameScene)