Mercurial > rinkhals
changeset 512:b112bcf4d435
Add restore game button to main menu.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Thu, 26 Nov 2009 23:36:20 +0000 |
parents | 57f9077fb7fb |
children | 399bb3cb4761 |
files | gamelib/engine.py gamelib/gameboard.py gamelib/mainmenu.py gamelib/savegame.py gamelib/toolbar.py |
diffstat | 5 files changed, 42 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/gamelib/engine.py Thu Nov 26 23:33:49 2009 +0000 +++ b/gamelib/engine.py Thu Nov 26 23:36:20 2009 +0000 @@ -103,6 +103,10 @@ return DayState(self.game) elif e.key == K_i: return HelpScreenState(self.game) + elif e.type is DO_LOAD_SAVEGAME: + self.game.switch_gameboard(e.gameboard) + e.gameboard.skip_next_start_day() + return DayState(self.game) elif e.type is not QUIT: self.game.main_app.event(e)
--- a/gamelib/gameboard.py Thu Nov 26 23:33:49 2009 +0000 +++ b/gamelib/gameboard.py Thu Nov 26 23:36:20 2009 +0000 @@ -297,6 +297,9 @@ self._cache_animal_positions() def start_day(self): + if hasattr(self, '_skip_start_day'): + del self._skip_start_day + return self.day, self.night = True, False self.tv.sun(True) self.reset_states() @@ -308,6 +311,11 @@ chicken.start_day() self.redraw_counters() + def skip_next_start_day(self): + # used to skip the start of the day triggered after + # reloading a save game + self._skip_start_day = True + def in_bounds(self, pos): """Check if a position is within the game boundaries""" if pos.x < 0 or pos.y < 0: @@ -1059,14 +1067,10 @@ self.reset_states() return serializer.simplify(self) - def restore_game(self, data): - if 'refid' not in data or 'class' not in data or data['class'] != self.__class__.__name__: - raise ValueError("Invalid save game.") - - new_gameboard = serializer.unsimplify(data) - + @staticmethod + def restore_game(gameboard): import engine - pygame.event.post(pygame.event.Event(engine.DO_LOAD_SAVEGAME, gameboard=new_gameboard)) + pygame.event.post(pygame.event.Event(engine.DO_LOAD_SAVEGAME, gameboard=gameboard)) class TextDialog(gui.Dialog):
--- a/gamelib/mainmenu.py Thu Nov 26 23:33:49 2009 +0000 +++ b/gamelib/mainmenu.py Thu Nov 26 23:36:20 2009 +0000 @@ -5,6 +5,8 @@ import constants import engine import imagecache +import gameboard +import savegame def make_main_menu(level): """Create a main menu""" @@ -42,6 +44,9 @@ def choose_level(): pygame.event.post(engine.GO_LEVEL_SCREEN) + def load_game(): + savegame.RestoreDialog(gameboard.GameBoard.restore_game).open() + def help_pressed(): pygame.event.post(engine.GO_HELP_SCREEN) @@ -63,6 +68,11 @@ self.tr() self.td(start_button, **td_kwargs) + loadgame_button = gui.Button('Restore Game') + loadgame_button.connect(gui.CLICK, load_game) + self.tr() + self.td(loadgame_button, **td_kwargs) + quit_button = gui.Button("Quit") quit_button.connect(gui.CLICK, quit_pressed)
--- a/gamelib/savegame.py Thu Nov 26 23:33:49 2009 +0000 +++ b/gamelib/savegame.py Thu Nov 26 23:36:20 2009 +0000 @@ -12,6 +12,8 @@ import config import version +import gameboard +import serializer TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S" @@ -239,11 +241,11 @@ class RestoreDialog(BaseSaveRestoreDialog): """Restore game dialog.""" - def __init__(self, gameboard): + def __init__(self, restore_func): BaseSaveRestoreDialog.__init__(self, "Load Game ...", "Load", allow_new=False) - self.connect(gui.CHANGE, self._restore, gameboard) + self.connect(gui.CHANGE, self._restore, restore_func) - def _restore(self, gameboard): + def _restore(self, restore_func): filename = self.get_fullpath() if filename is None: return @@ -254,4 +256,14 @@ print "Failed to load game: %s" % (e,) return - gameboard.restore_game(data) + if 'refid' not in data or 'class' not in data or data['class'] != gameboard.GameBoard.__name__: + print "Failed to load game: Invalid game data." + return + + try: + new_gameboard = serializer.unsimplify(data) + except Exception, e: + print "Failed to load game: %s" % (e,) + return + + restore_func(new_gameboard)
--- a/gamelib/toolbar.py Thu Nov 26 23:33:49 2009 +0000 +++ b/gamelib/toolbar.py Thu Nov 26 23:36:20 2009 +0000 @@ -181,7 +181,7 @@ def load_game(self): """Load game 'dialog'.""" - savegame.RestoreDialog(self.gameboard).open() + savegame.RestoreDialog(self.gameboard.restore_game).open() update_cash_counter = mkcountupdate('cash_counter') update_wood_counter = mkcountupdate('wood_counter')