Merge branch 'master' of ctpug.org.za:koperkapel
[koperkapel.git] / koperkapel / scenes / base.py
index 6bb7f5584347e7c6f39e20e509c37fe3c5789286..b6d6ff9bf86262a89345e0de38f8c0fadb162286 100644 (file)
 """ Scene utilities. """
 
+import functools
+
+
+def apply_events(f):
+    """ Decorator that applies events to an engine. """
+    @functools.wraps(f)
+    def wrap(self, *args, **kw):
+        events = f(self, *args, **kw)
+        self._apply_events(events)
+    return wrap
+
+
+class Engine:
+    """ A holder for game state. """
+
+    def __init__(self, app, scene):
+        self._app = app
+        self._scene = scene
+
+    def _apply_events(self, events):
+        if not events:
+            return
+        for ev in events:
+            ev.apply(self)
+
+    def change_scene(self, scene):
+        self._scene.exit()
+        self._scene = scene
+        self._scene.enter()
+
+    @apply_events
+    def update(self, dt):
+        return self._scene.update(dt)
+
+    def draw(self):
+        self._scene.draw(self._app.screen)
+
+    @apply_events
+    def on_mouse_down(self, pos, button):
+        return self._scene.on_mouse_down(pos, button)
+
+    @apply_events
+    def on_mouse_up(self, pos, button):
+        return self._scene.on_mouse_up(pos, button)
+
+    @apply_events
+    def on_key_down(self, key, mod, unicode):
+        return self._scene.on_key_down(key, mod, unicode)
+
+    @apply_events
+    def on_key_up(self, key, mod):
+        return self._scene.on_key_up(key, mod)
+
+    @apply_events
+    def on_music_end(self):
+        return self._scene.on_music_end()
+
+
+class Event:
+    """ Base class for events. """
+
+    ENGINE_METHOD = "unknown_event"
+
+    def __init__(self, *args, **kw):
+        self._args = args
+        self._kw = kw
+
+    def apply(self, engine):
+        getattr(engine, self.ENGINE_METHOD)(*self._args, **self._kw)
+
+
+class ChangeSceneEvent(Event):
+    """ Change to a new scene. """
+
+    ENGINE_METHOD = "change_scene"
+
+
+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 __init__(self):
+        self.actors = Actors()
+
+    def enter(self):
+        pass
+
+    def exit(self):
+        pass
+
     def update(self, dt):
         pass
 
     def draw(self, screen):
+        screen.clear()
+        self.actors.draw(screen)
+
+    def on_mouse_down(self, pos, button):
+        pass
+
+    def on_mouse_up(self, pos, button):
+        pass
+
+    def on_key_down(self, key, mod, unicode):
+        pass
+
+    def on_key_up(self, key, mod):
+        pass
+
+    def on_music_end(self):
         pass