From: Simon Cross Date: Sun, 4 Sep 2016 16:17:57 +0000 (+0200) Subject: Add very hacky level loading. X-Git-Tag: tabakrolletjie-v1.0.0~259 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=7d9a02acc6edcb3989bf655f201e29f0e9f16684;p=tabakrolletjie.git Add very hacky level loading. --- diff --git a/tabakrolletjie/gamestate.py b/tabakrolletjie/gamestate.py index b77e979..14da6d8 100644 --- a/tabakrolletjie/gamestate.py +++ b/tabakrolletjie/gamestate.py @@ -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 index 0000000..7dad79a --- /dev/null +++ b/tabakrolletjie/loader.py @@ -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) diff --git a/tabakrolletjie/scenes/menu.py b/tabakrolletjie/scenes/menu.py index fb0c5f9..c8b32ad 100644 --- a/tabakrolletjie/scenes/menu.py +++ b/tabakrolletjie/scenes/menu.py @@ -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") diff --git a/tabakrolletjie/scenes/night.py b/tabakrolletjie/scenes/night.py index 27ab21c..630dab3 100644 --- a/tabakrolletjie/scenes/night.py +++ b/tabakrolletjie/scenes/night.py @@ -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))