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):
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)
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
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 = {}
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(),
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)
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
'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))