Allow actors to be placed on different layers.
authorSimon Cross <hodgestar@gmail.com>
Wed, 2 Mar 2016 19:32:08 +0000 (21:32 +0200)
committerSimon Cross <hodgestar@gmail.com>
Wed, 2 Mar 2016 19:32:08 +0000 (21:32 +0200)
koperkapel/scenes/base.py

index 19ba212fce407eb91047652b5a114ceed719284c..d0b9a951ae16c57e20e279560f54fd9ab8a7b7a4 100644 (file)
@@ -110,22 +110,34 @@ class MoveViewportEvent(Event):
 
 
 class Actors:
-    """ A list of actors. """
+    """ A list of actors.
+
+    Actors may be rendered in different layers. Layers with lower levels
+    are rendered lower than layers with higher ones.
+    """
 
     def __init__(self):
-        self._actors = []
+        self._layer_order = []
+        self._layers = {}
+        self.add_layer("default", 0)
+
+    def add_layer(self, name, level):
+        self._layer_order.append((level, name))
+        self._layer_order.sort()
+        self._layers[name] = []
 
-    def add(self, actor):
-        self._actors.append(actor)
+    def add(self, actor, layer="default"):
+        self._layers[layer].append(actor)
         return actor
 
-    def remove(self, actor):
-        self._actors.remove(actor)
+    def remove(self, actor, layer="default"):
+        self._layers[layer].remove(actor)
         return actor
 
     def draw(self, screen):
-        for actor in self._actors:
-            actor.draw()  # TODO: allow an option screen to be passed in
+        for lvl, layer in self._layer_order:
+            for actor in self._layers[layer]:
+                actor.draw()  # TODO: allow an option screen to be passed in
 
 
 class Scene: