Let there be roaches.
[koperkapel.git] / koperkapel / scenes / base.py
index 9bad13cd9f03b9e1d172c9c977ae575002a51c88..aa30be492baa518327975e8d5a96377dc88e3935 100644 (file)
@@ -13,11 +13,14 @@ 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):
         if not events:
@@ -26,16 +29,27 @@ 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
+        exit()
+
+    def move_screen(self, offset):
+        self._viewport = (self._viewport[0] + offset[0],
+                          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._scene.draw(self._app.screen, self._viewport)
 
     @apply_events
     def on_mouse_down(self, pos, button):
@@ -77,21 +91,62 @@ 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. """
+
+    ENGINE_METHOD = "quit_game"
+
+
+class MoveViewportEvent(Event):
+    """ Change to a new scene. """
+
+    ENGINE_METHOD = "move_screen"
+
+
+class Actors:
+    """ A list of actors. """
+
+    def __init__(self):
+        self._actors = []
+
+    def add(self, actor):
+        self._actors.append(actor)
+        return actor
+
+    def remove(self, actor):
+        self._actors.remove(actor)
+        return actor
+
+    def draw(self, screen):
+        for actor in self._actors:
+            actor.draw()  # TODO: allow an option screen to be passed in
+
+
 class Scene:
     """ Base class for scenes. """
 
-    def enter(self):
-        pass
+    def __init__(self):
+        self.actors = Actors()
 
-    def exit(self):
+    def enter(self, world):
         pass
 
-    def update(self, dt):
+    def exit(self, world):
         pass
 
-    def draw(self, screen):
+    def update(self, world, dt):
         pass
 
+    def draw(self, screen, viewport):
+        screen.clear()
+        self.actors.draw(screen)
+
     def on_mouse_down(self, pos, button):
         pass