Save game support
authorNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 21:19:52 +0000 (23:19 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 21:20:40 +0000 (23:20 +0200)
tabakrolletjie/gamestate.py
tabakrolletjie/scenes/day.py
tabakrolletjie/scenes/menu.py
tabakrolletjie/utils.py

index 6f6725b8e0bda36ef5a031946306194d0496b0e2..afe842dde40a2d01ba696c6ccf2e487ba6697751 100644 (file)
@@ -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"])
index 7f73ad288a17342946d91a928577220da0e86ddb..d19b0c6e526e68118be0fca16b005bd13b836f9b 100644 (file)
@@ -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):
index 67336112c6c3e7a65151bfb524d23117bf1c7369..89b531e2621d78f69ec75e7869fb880104c5e3b5 100644 (file)
@@ -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':
index 19364c2b013c46279e94d6eed8b3282002e8a45d..b237467ded10e499d5b8f4b69c76816d87a6c25e 100644 (file)
@@ -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):