Add base support for deferring world updates.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 16:01:11 +0000 (18:01 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 16:01:11 +0000 (18:01 +0200)
koperkapel/scenes/base.py

index 2a094f62b1b2dbbea04f77fc149a5412c24bfe81..8bc803ae2ba0fbdc29db684f643be3a046300b17 100644 (file)
@@ -170,12 +170,21 @@ class Actors:
                 screen.blit(actor._surf, actor.topleft)
 
 
+def defer_to_update(f):
+    """ Defers a function until the next update run. """
+    @functools.wraps(f)
+    def wrapper(self, *args, **kw):
+        self._deferred_updates.append((f, args, kw))
+    return wrapper
+
+
 class Scene:
     """ Base class for scenes. """
 
     def __init__(self):
         self.actors = Actors()
         self.viewport = (0, 0)
+        self._deferred_updates = []
 
     def move_screen(self, offset):
         self.viewport = (self.viewport[0] + offset[0],
@@ -192,7 +201,9 @@ class Scene:
         pass
 
     def update(self, world, engine, dt):
-        pass
+        deferred_updates, self._deferred_updates = self._deferred_updates, []
+        for f, args, kw in deferred_updates:
+            f(self, world, *args, **kw)
 
     def draw(self, screen):
         screen.clear()