Let there be roaches.
[koperkapel.git] / koperkapel / scenes / base.py
index c6b8752d41de58893a8ac3174ace3f2b37b5c284..aa30be492baa518327975e8d5a96377dc88e3935 100644 (file)
@@ -13,11 +13,13 @@ def apply_events(f):
 
 
 class Engine:
-    """ A holder for game state. """
+    """ A holder for game state & scene management.
+        """
 
-    def __init__(self, app, scene):
+    def __init__(self, app, scene, world):
         self._app = app
         self._scene = scene
+        self._world = world
         self._viewport = (0, 0)
 
     def _apply_events(self, events):
@@ -27,9 +29,12 @@ class Engine:
             ev.apply(self)
 
     def change_scene(self, scene):
-        self._scene.exit()
+        self._apply_events(self._scene.exit(self._world))
         self._scene = scene
-        self._scene.enter()
+        self._apply_events(self._scene.enter(self._world))
+
+    def change_world(self, *args, **kw):
+        self._world.apply_event(*args, **kw)
 
     def quit_game(self):
         from pgzero.game import exit
@@ -37,11 +42,11 @@ class Engine:
 
     def move_screen(self, offset):
         self._viewport = (self._viewport[0] + offset[0],
-                            self._viewport[1] + offset[1])
+                          self._viewport[1] + offset[1])
 
     @apply_events
     def update(self, dt):
-        return self._scene.update(dt)
+        return self._scene.update(self._world, dt)
 
     def draw(self):
         self._scene.draw(self._app.screen, self._viewport)
@@ -86,6 +91,12 @@ class ChangeSceneEvent(Event):
     ENGINE_METHOD = "change_scene"
 
 
+class WorldEvent(Event):
+    """ Be a hero. Change the world. """
+
+    ENGINE_METHOD = "change_world"
+
+
 class QuitEvent(Event):
     """ Quit the game. """
 
@@ -123,13 +134,13 @@ class Scene:
     def __init__(self):
         self.actors = Actors()
 
-    def enter(self):
+    def enter(self, world):
         pass
 
-    def exit(self):
+    def exit(self, world):
         pass
 
-    def update(self, dt):
+    def update(self, world, dt):
         pass
 
     def draw(self, screen, viewport):