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()
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):
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
@property
def level(self):
- return self._level["level"]
+ return self._state["level"]
@property
def roaches(self):
self._build_roach("roichard", quickness=3),
]
state["level"] = {
- "file": "level1.json",
+ "name": "level1",
}
return state
"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,))