Saved games can now be loaded as well.
authorJeremy Thurgood <firxen@gmail.com>
Fri, 16 May 2014 17:23:24 +0000 (19:23 +0200)
committerJeremy Thurgood <firxen@gmail.com>
Fri, 16 May 2014 17:23:24 +0000 (19:23 +0200)
naja/engine.py
naja/events.py
naja/scenes/load_save.py
naja/scenes/menu.py

index cbe27ce0b9ffe7d56da3bc9814b111815a46d84b..d7e0caf62539de966abae6746adc66bb9ad7f4ff 100644 (file)
@@ -2,7 +2,7 @@ import pygame
 import pygame.locals as pgl
 
 from naja.constants import FPS
-from naja.events import SceneChangeEvent, QuitGameEvent
+from naja.events import SceneChangeEvent, QuitGameEvent, LoadGameEvent
 
 
 class Engine(object):
@@ -21,6 +21,8 @@ class Engine(object):
                     return
                 elif SceneChangeEvent.matches(ev):
                     self.change_scene(ev.scene_cls)
+                elif LoadGameEvent.matches(ev):
+                    self._state = ev.state
                 else:
                     self._scene.handle_event(ev)
             self._scene.render(self._surface)
index 1d7609f5937cc61d81b89de4a0c805e7398f72b0..dbf7a03711fa1a4dd87a9ef15544332e4eddb948 100644 (file)
@@ -46,3 +46,11 @@ class InvalidateTheWorld(NajaEvent):
 
 class SelectEvent(NajaEvent):
     TYPE = "SELECT"
+
+
+class LoadGameEvent(NajaEvent):
+    TYPE = "LOAD_GAME"
+
+    @classmethod
+    def post(cls, state):
+        super(LoadGameEvent, cls).post(state=state)
index 485b032f61179e1e54a145e416c3e52be446761b..28795041623321dfc8e3490111914c02a0c1fde5 100644 (file)
@@ -9,7 +9,8 @@ from datetime import datetime
 import pygame.locals as pgl
 
 from naja.constants import KEYS
-from naja.events import SceneChangeEvent, InvalidateTheWorld
+from naja.events import SceneChangeEvent, InvalidateTheWorld, LoadGameEvent
+from naja.gamestate import GameState
 from naja.options import options
 from naja.scenes.scene import Scene
 from naja.widgets.save_slot import SaveSlotWidget
@@ -26,9 +27,9 @@ def ensure_save_path_exists():
         os.makedirs(location)
 
 
-class SaveGameScene(Scene):
+class LoadSaveGameBase(Scene):
     def __init__(self, state):
-        super(SaveGameScene, self).__init__(state)
+        super(LoadSaveGameBase, self).__init__(state)
         selector = SelectorWidget()
         self.add(selector)
         self.slots = {}
@@ -55,10 +56,21 @@ class SaveGameScene(Scene):
         game_data = self.get_game_data(slot_num)
         y_offset = 74 * slot_num
         slot = SaveSlotWidget((100, y_offset), slot_num, game_data)
-        slot.add_callback('click', lambda event: self.save_game(slot_num))
+        slot.add_callback('click', lambda event: self.perform_action(slot_num))
         return slot
 
-    def save_game(self, slot_num):
+    def handle_scene_event(self, ev):
+        if ev.type == pgl.KEYDOWN and ev.key in KEYS.QUIT:
+            from naja.scenes.menu import MenuScene
+            SceneChangeEvent.post(MenuScene)
+            return
+
+    def perform_action(self, slot_num):
+        raise NotImplementedError("Nothing to see here.")
+
+
+class SaveGameScene(LoadSaveGameBase):
+    def perform_action(self, slot_num):
         save_data = {
             'timestamp': datetime.now().ctime(),
             'data': self.state.gameboard.export(),
@@ -72,8 +84,10 @@ class SaveGameScene(Scene):
         self.slots[slot_num].game_data = self.get_game_data(slot_num)
         InvalidateTheWorld.post()
 
-    def handle_scene_event(self, ev):
-        if ev.type == pgl.KEYDOWN and ev.key in KEYS.QUIT:
-            from naja.scenes.menu import MenuScene
-            SceneChangeEvent.post(MenuScene)
-            return
+
+class LoadGameScene(LoadSaveGameBase):
+    def perform_action(self, slot_num):
+        game_data = self.get_game_data(slot_num)
+        state = GameState(game_data['data'])
+        if game_data is not None:
+            LoadGameEvent.post(state)
index e0383b5ca3159a3d3abd63b4a0e04f56cb2c4107..a06e061f0d7c03afec99b22122841420f2d1f7d8 100644 (file)
@@ -9,7 +9,7 @@ from naja.events import SceneChangeEvent, QuitGameEvent
 from naja.scenes.scene import Scene
 from naja.scenes.credits import CreditsScene
 from naja.scenes.game import GameScene
-from naja.scenes.load_save import SaveGameScene
+from naja.scenes.load_save import LoadGameScene, SaveGameScene
 from naja.widgets.selector import SelectorWidget
 from naja.widgets.text import TextWidget
 
@@ -31,6 +31,11 @@ class MenuScene(Scene):
             'click', lambda event: SceneChangeEvent.post(CreditsScene))
         selector.add(credits)
 
+        load = TextWidget((100, 200), 'Load', fontsize=32, colour='white')
+        load.add_callback(
+            'click', lambda event: SceneChangeEvent.post(LoadGameScene))
+        selector.add(load)
+
         save = TextWidget((100, 250), 'Save', fontsize=32, colour='white')
         save.add_callback(
             'click', lambda event: SceneChangeEvent.post(SaveGameScene))