From: Simon Cross Date: Sat, 5 Mar 2016 16:01:11 +0000 (+0200) Subject: Add base support for deferring world updates. X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=03537d4fda708994f08f17ae33f461eb6fbd14ba;p=koperkapel.git Add base support for deferring world updates. --- diff --git a/koperkapel/scenes/base.py b/koperkapel/scenes/base.py index 2a094f6..8bc803a 100644 --- a/koperkapel/scenes/base.py +++ b/koperkapel/scenes/base.py @@ -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()