--- /dev/null
+""" 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)
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):
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: