Add very hacky level loading.
authorSimon Cross <hodgestar@gmail.com>
Sun, 4 Sep 2016 16:17:57 +0000 (18:17 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sun, 4 Sep 2016 16:17:57 +0000 (18:17 +0200)
tabakrolletjie/gamestate.py
tabakrolletjie/loader.py [new file with mode: 0644]
tabakrolletjie/scenes/menu.py
tabakrolletjie/scenes/night.py

index b77e97913129d574b02da2a2a79bc365c4db916b..14da6d880f84d5e562cd30867d005b29fa6d7403 100644 (file)
@@ -1,5 +1,17 @@
 """ The game state. """
 
+from .loader import loader
+
 
 class GameState(object):
-    pass
+    def __init__(self):
+        self._state = {
+            "station": None,
+        }
+
+    @property
+    def station(self):
+        return self._state["station"]
+
+    def load_station(self, station):
+        self._state["station"] = loader.load_station(station)
diff --git a/tabakrolletjie/loader.py b/tabakrolletjie/loader.py
new file mode 100644 (file)
index 0000000..7dad79a
--- /dev/null
@@ -0,0 +1,31 @@
+""" Utilities for loading resource files. """
+
+import json
+import os
+
+
+class Loader(object):
+    """ Load data files from beneath a prefix. """
+
+    def __init__(self, prefix):
+        self._prefix = prefix
+
+    def full_path(self, *parts):
+        path = "/".join(parts)
+        rel_path = os.path.join(*path.split("/"))
+        abs_path = os.path.join(self._prefix, rel_path)
+        print abs_path
+        return abs_path
+
+    def open_file(self, *parts):
+        return file(self.full_path(*parts), "rb")
+
+    def load_station(self, *parts):
+        with self.open_file("stations", *parts) as f:
+            return json.load(f)
+
+
+_DATA_PREFIX = os.path.abspath(
+    os.path.join(os.path.dirname(__file__), "..", "data"))
+
+loader = Loader(_DATA_PREFIX)
index fb0c5f99da53bfad50626ef8812baa16acff8786..c8b32ad40a528678dfad97bc6bc867f9a6c6b990 100644 (file)
@@ -17,3 +17,6 @@ class MenuScene(BaseScene):
             elif ev.key == pgl.K_n:
                 from .night import NightScene
                 SceneChangeEvent.post(scene=NightScene())
+            elif ev.key == pgl.K_l:
+                print "Loading Station Alpha ..."
+                gamestate.load_station("station-alpha.json")
index 27ab21c7b31169b7e6c25d365f563e4cac446b4e..630dab3ef7fa46d7ace48ce3b6ea74efd25b3cc5 100644 (file)
@@ -7,6 +7,10 @@ from ..events import SceneChangeEvent
 
 
 class NightScene(BaseScene):
+    def enter(self, gamestate):
+        import pprint
+        pprint.pprint(gamestate.station)
+
     def render(self, surface, gamestate):
         surface.fill((0, 0, 255))