From b11c77fa61109312d3414b176a11f051fcc59d74 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 10 Sep 2016 23:19:52 +0200 Subject: [PATCH] Save game support --- tabakrolletjie/gamestate.py | 21 +++++++++++++++++++++ tabakrolletjie/scenes/day.py | 4 +++- tabakrolletjie/scenes/menu.py | 13 +++++++------ tabakrolletjie/utils.py | 7 +++++++ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/tabakrolletjie/gamestate.py b/tabakrolletjie/gamestate.py index 6f6725b..afe842d 100644 --- a/tabakrolletjie/gamestate.py +++ b/tabakrolletjie/gamestate.py @@ -55,3 +55,24 @@ class GameState(object): def update_lights(self, lights): self._state["station"]["lights"] = lights.serialize_lights() + + def serialize(self): + result = { + "station": self._state["station"], + "turnips": self._state["turnips"], + "seeds": self._state['seeds'], + "harvested": self.harvested, + "eaten": self.eaten, + "days": self.days, + "resistances": self.resistances, + } + return result + + def load_from_json(self, json_data): + self._state["station"] = dict(**json_data["station"]) + self._state["turnips"] = json_data["turnips"] + self._state["seeds"] = json_data["seeds"] + self.harvested = json_data["harvested"] + self.eaten = json_data["harvested"] + self.days = json_data["days"] + self.resistances = dict(**json_data["resistances"]) diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index 7f73ad2..d19b0c6 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -15,7 +15,7 @@ from ..lights import LightManager, light_fitting_by_type, check_space_for_light from ..infobar import InfoBar from ..obstacles import ObstacleManager from ..events import SceneChangeEvent -from ..utils import debug_timer, shadowed_text +from ..utils import debug_timer, shadowed_text, write_save_file from ..loader import loader from ..transforms import Overlay, Alpha, ColourWedges @@ -53,6 +53,8 @@ class DayScene(BaseScene): self._draw_you_lose(gamestate) elif gamestate.harvested >= gamestate.turnip_target: self._draw_you_win(gamestate) + else: + write_save_file(gamestate.serialize()) self._ending = False def _draw_you_lose(self, gamestate): diff --git a/tabakrolletjie/scenes/menu.py b/tabakrolletjie/scenes/menu.py index 6733611..89b531e 100644 --- a/tabakrolletjie/scenes/menu.py +++ b/tabakrolletjie/scenes/menu.py @@ -7,7 +7,7 @@ from ..events import QuitEvent, SceneChangeEvent from ..loader import loader from ..constants import FONTS from ..widgets import TextButton, SpacerButton -from ..utils import save_file_exists +from ..utils import save_file_exists, get_save_data class MenuScene(BaseScene): @@ -50,11 +50,12 @@ class MenuScene(BaseScene): return button.name return None - def _do_day(self): + def _do_load_save(self, gamestate): + gamestate.load_from_json(get_save_data()) from .day import DayScene SceneChangeEvent.post(scene=DayScene()) - def _do_load_level(self): + def _start_new_game(self): from .load_level import LoadLevelScene SceneChangeEvent.post(scene=LoadLevelScene()) @@ -72,15 +73,15 @@ class MenuScene(BaseScene): elif ev.key == pgl.K_d: self._do_day() elif ev.key == pgl.K_l: - self._do_load_level() + self._start_new_game() elif ev.key == pgl.K_h: self._do_help() elif ev.type == pgl.MOUSEBUTTONDOWN: pressed = self._get_pressed(ev) if pressed == 'new game': - self._do_load_level() + self._start_new_game() elif pressed == 'continue': - self._do_day() + self._do_load_save(gamestate) elif pressed == 'help': self._do_help() elif pressed == 'quit': diff --git a/tabakrolletjie/utils.py b/tabakrolletjie/utils.py index 19364c2..b237467 100644 --- a/tabakrolletjie/utils.py +++ b/tabakrolletjie/utils.py @@ -95,6 +95,13 @@ def save_file_exists(): return os.path.isfile(savefile) +def get_save_data(): + savefile = get_save_file_name() + with open(savefile, 'rb') as f: + data = json.load(f) + return data + + def write_save_file(json_data): save_dir = save_location() if not os.path.exists(save_dir): -- 2.34.1