From: Simon Cross Date: Wed, 2 Mar 2016 15:54:25 +0000 (+0200) Subject: Use WorldEvent to switch level. X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=55ff545e2629cc15b03ec582ee4c1edf1244432d;p=koperkapel.git Use WorldEvent to switch level. --- diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index f4a031a..70f82c5 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -10,8 +10,8 @@ from ..constants import TILE_SIZE class LevelScene(Scene): """ Level scene. """ - def __init__(self, level_name): - self._level_data = levels.load(level_name) + def enter(self, world): + self._level_data = levels.load(world.level["name"]) self._tiles = self._level_data['tiles'] self._surface = None self._render() diff --git a/koperkapel/scenes/menu.py b/koperkapel/scenes/menu.py index d8e3438..c4707c9 100644 --- a/koperkapel/scenes/menu.py +++ b/koperkapel/scenes/menu.py @@ -3,7 +3,7 @@ from pgzero.constants import keys from ..actors.buttons import TextButton from ..actors.navigator import ActorNavigator -from .base import Scene, ChangeSceneEvent, QuitEvent +from .base import Scene, ChangeSceneEvent, WorldEvent, QuitEvent class MenuScene(Scene): @@ -32,11 +32,14 @@ class MenuScene(Scene): def change_to_level(self): from .level import LevelScene - return [ChangeSceneEvent(LevelScene('level1'))] + return [ChangeSceneEvent(LevelScene())] def change_to_viewer(self): from .viewlevel import ViewLevelScene - return [ChangeSceneEvent(ViewLevelScene('map'))] + return [ + WorldEvent("set", {"level.name": "map"}), + ChangeSceneEvent(ViewLevelScene()) + ] def change_to_roaches(self): from .roaches import RoachesScene diff --git a/koperkapel/world.py b/koperkapel/world.py index efb6403..8a4af23 100644 --- a/koperkapel/world.py +++ b/koperkapel/world.py @@ -9,7 +9,7 @@ class World: @property def level(self): - return self._level["level"] + return self._state["level"] @property def roaches(self): @@ -23,7 +23,7 @@ class World: self._build_roach("roichard", quickness=3), ] state["level"] = { - "file": "level1.json", + "name": "level1", } return state @@ -40,5 +40,15 @@ class World: "attributes": attributes, } - def apply_event(self, *args, **kw): - pass + def _apply_set(self, updates): + for name, value in updates.items(): + parts = name.split(".") + obj = self._state + for p in parts[:-1]: + obj = obj[p] + obj[parts[-1]] = value + + def apply_event(self, action, *args, **kw): + if action == "set": + return self._apply_set(*args, **kw) + raise ValueError("Unknown world event action: %r" % (action,))