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"])
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
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):
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):
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())
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':
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):