Use WorldEvent to switch level.
authorSimon Cross <hodgestar@gmail.com>
Wed, 2 Mar 2016 15:54:25 +0000 (17:54 +0200)
committerSimon Cross <hodgestar@gmail.com>
Wed, 2 Mar 2016 15:54:25 +0000 (17:54 +0200)
koperkapel/scenes/level.py
koperkapel/scenes/menu.py
koperkapel/world.py

index f4a031a551e5e4b63cbc4290b4b1e963621e73b4..70f82c59d338133f9bf7173412a54e199be3714a 100644 (file)
@@ -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()
index d8e343800cf4de0c8f9d27631afcf8fa932b8621..c4707c976eb6da73eec1bc7dd75e59d2f3d03355 100644 (file)
@@ -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
index efb64030587c2bdc95aa697a67fb869dcf766112..8a4af23d1407a08c32c02aef57eb9d493deed882 100644 (file)
@@ -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,))