List levels by title.
authorSimon Cross <hodgestar@gmail.com>
Sat, 10 Sep 2016 12:12:20 +0000 (14:12 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 10 Sep 2016 12:12:20 +0000 (14:12 +0200)
tabakrolletjie/gamestate.py
tabakrolletjie/scenes/load_level.py
tabakrolletjie/scenes/menu.py

index c678befb968eb83d6520531698ee78a82c4fabcb..f50d9f93ab28ed2599829d3634f34dd5a1e20b28 100644 (file)
@@ -38,8 +38,8 @@ class GameState(object):
     def seeds(self, value):
         self._state['seeds'] = value
 
-    def load_station(self, station):
-        self._state["station"] = loader.load_station(station)
+    def set_station(self, station):
+        self._state["station"] = station
 
     def get_spawn_positions(self):
         return self._state["station"]["config"]["spawn positions"]
index 7b3478918209e3042a3eeb8fb3ed5b4cdd7cca7f..4cf267b0f2ba264cc5d8fa454bf45516b0a1e096 100644 (file)
@@ -14,23 +14,20 @@ 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
+        files = [f for f in os.listdir(station_path) if f.endswith(".json")]
+        files.sort()
+        return [loader.load_station(f) for f in files]
 
     def enter(self, gamestate):
         """Construct list of stations"""
         height = 50
-        self._buttons = []
         width = 30
-        for station, filename in self._list_stations():
+        self._buttons = []
+        for station in self._list_stations():
+            title = station["config"]["name"]
             pos = (width, height)
-            button = TextButton(station, (255, 255, 255), filename, pos)
+            button = TextButton(title, (255, 255, 255), None, pos)
+            button.station = station
             if width < 400:
                 width += 350
             else:
@@ -46,13 +43,13 @@ class LoadLevelScene(BaseScene):
     def _get_pressed(self, ev):
         for button in self._buttons:
             if button.pressed(ev):
-                return button.name
+                return button
         return None
 
-    def _do_load(self, name, gamestate):
+    def _do_load(self, station, gamestate):
         from .day import DayScene
-        print "Loading station", name
-        gamestate.load_station(name)
+        print "Loading station", station["config"]["name"]
+        gamestate.set_station(station)
         SceneChangeEvent.post(scene=DayScene())
 
     def event(self, ev, gamestate):
@@ -62,4 +59,4 @@ class LoadLevelScene(BaseScene):
         elif ev.type == pgl.MOUSEBUTTONDOWN:
             pressed = self._get_pressed(ev)
             if pressed:
-                self._do_load(pressed, gamestate)
+                self._do_load(pressed.station, gamestate)
index 7a4366aedeb4a686e7c7060dbb4368124c7610ed..9dc54c1ceb93a4d14dd81f11f0e81cb535c35b1b 100644 (file)
@@ -13,7 +13,7 @@ class MenuScene(BaseScene):
     def enter(self, gamestate):
         if gamestate.station is None:
             print "Loading Station Alpha ..."
-            gamestate.load_station("station-alpha.json")
+            gamestate.set_station(loader.load_station("station-alpha.json"))
         font_title = loader.load_font(FONTS['bold'], size=32)
         self._title = font_title.render('A Game with a title', True,
                                         (255, 255, 255))