Add load level scene
authorNeil <neil@dip.sun.ac.za>
Fri, 9 Sep 2016 19:05:03 +0000 (21:05 +0200)
committerNeil <neil@dip.sun.ac.za>
Fri, 9 Sep 2016 19:06:57 +0000 (21:06 +0200)
tabakrolletjie/scenes/load_level.py [new file with mode: 0644]
tabakrolletjie/scenes/menu.py

diff --git a/tabakrolletjie/scenes/load_level.py b/tabakrolletjie/scenes/load_level.py
new file mode 100644 (file)
index 0000000..7b34789
--- /dev/null
@@ -0,0 +1,65 @@
+""" Menu scene. """
+
+import os
+
+import pygame.locals as pgl
+
+from .base import BaseScene
+from ..events import SceneChangeEvent
+from ..widgets import TextButton
+from ..loader import loader
+
+
+class LoadLevelScene(BaseScene):
+
+    def _list_stations(self):
+        station_path = loader.full_path("stations")
+        files = os.listdir(station_path)
+        result = []
+        for f in files:
+            name, ext = os.path.splitext(f)
+            if ext == '.json':
+                result.append((name, f))
+        result.sort()
+        return result
+
+    def enter(self, gamestate):
+        """Construct list of stations"""
+        height = 50
+        self._buttons = []
+        width = 30
+        for station, filename in self._list_stations():
+            pos = (width, height)
+            button = TextButton(station, (255, 255, 255), filename, pos)
+            if width < 400:
+                width += 350
+            else:
+                width = 30
+                height += button.get_height() + 20
+            self._buttons.append(button)
+
+    def render(self, surface, gamestate):
+        surface.fill((0, 128, 128))
+        for button in self._buttons:
+            button.render(surface)
+
+    def _get_pressed(self, ev):
+        for button in self._buttons:
+            if button.pressed(ev):
+                return button.name
+        return None
+
+    def _do_load(self, name, gamestate):
+        from .day import DayScene
+        print "Loading station", name
+        gamestate.load_station(name)
+        SceneChangeEvent.post(scene=DayScene())
+
+    def event(self, ev, gamestate):
+        if ev.type == pgl.KEYDOWN:
+            if ev.key == pgl.K_l:
+                self._do_load("station_alpha.json", gamestate)
+        elif ev.type == pgl.MOUSEBUTTONDOWN:
+            pressed = self._get_pressed(ev)
+            if pressed:
+                self._do_load(pressed, gamestate)
index 1d14abcd70d80f71fc445a2f280c5f5fbce8cf7d..7a4366aedeb4a686e7c7060dbb4368124c7610ed 100644 (file)
@@ -47,6 +47,10 @@ class MenuScene(BaseScene):
         from .day import DayScene
         SceneChangeEvent.post(scene=DayScene())
 
+    def _do_load_level(self):
+        from .load_level import LoadLevelScene
+        SceneChangeEvent.post(scene=LoadLevelScene())
+
     def event(self, ev, gamestate):
         if ev.type == pgl.KEYDOWN:
             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
@@ -57,13 +61,11 @@ class MenuScene(BaseScene):
             elif ev.key == pgl.K_d:
                 self._do_day()
             elif ev.key == pgl.K_l:
-                print "Loading Station Alpha ..."
-                gamestate.load_station("station-alpha.json")
+                self._do_load_level()
         elif ev.type == pgl.MOUSEBUTTONDOWN:
             pressed = self._get_pressed(ev)
             if pressed == 'load level':
-                print "Loading Station Alpha ..."
-                gamestate.load_station("station-alpha.json")
+                self._do_load_level()
             elif pressed == 'start game':
                 self._do_day()
             elif pressed: