Better layer management.
authorSimon Cross <hodgestar@gmail.com>
Wed, 2 Mar 2016 20:23:49 +0000 (22:23 +0200)
committerSimon Cross <hodgestar@gmail.com>
Wed, 2 Mar 2016 20:23:49 +0000 (22:23 +0200)
koperkapel/scenes/base.py
koperkapel/scenes/roaches.py

index d0b9a951ae16c57e20e279560f54fd9ab8a7b7a4..fd53f817b9cc6b3fade5a0b617f42a90e1f1e84f 100644 (file)
@@ -109,34 +109,52 @@ class MoveViewportEvent(Event):
     ENGINE_METHOD = "move_screen"
 
 
+class Layer:
+    """ A single layer of actors. """
+
+    def __init__(self, name):
+        self.name = name
+        self.actors = []
+
+    def add(self, actor):
+        self.actors.append(actor)
+        return actor
+
+    def remove(self, actor):
+        self.actors.remove(actor)
+        return actor
+
+
 class Actors:
-    """ A list of actors.
+    """ Layers 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._layer_order = []
+        self._ordered_layers = []
         self._layers = {}
         self.add_layer("default", 0)
 
+    def __getattr__(self, name):
+        return self._layers[name]
+
     def add_layer(self, name, level):
-        self._layer_order.append((level, name))
-        self._layer_order.sort()
-        self._layers[name] = []
+        layer = self._layers[name] = Layer(name)
+        self._ordered_layers.append((level, name))
+        self._ordered_layers.sort()
+        return layer
 
     def add(self, actor, layer="default"):
-        self._layers[layer].append(actor)
-        return actor
+        return self._layers[layer].add(actor)
 
     def remove(self, actor, layer="default"):
-        self._layers[layer].remove(actor)
-        return actor
+        return self._layers[layer].remove(actor)
 
     def draw(self, screen):
-        for lvl, layer in self._layer_order:
-            for actor in self._layers[layer]:
+        for lvl, name in self._ordered_layers:
+            for actor in self._layers[name].actors:
                 actor.draw()  # TODO: allow an option screen to be passed in
 
 
index 7198ee2363ffee41c85530a158ad82037ed45b83..09169f3ac80bd4566933c8dd97b50303fc110144 100644 (file)
@@ -12,17 +12,17 @@ class RoachesScene(Scene):
 
     def __init__(self):
         super().__init__()
-        self.actors.add_layer("pads", level=5)
-        self.actors.add_layer("roaches", level=10)
         self._roach_actors = {}
+        self._roaches = self.actors.add_layer("roaches", level=10)
+        self._pads = self.actors.add_layer("pads", level=5)
         self._init_pads()
 
     def _init_pads(self):
-        self._roach_pad = self.actors.add(
-            Actor("roach_management/roach_pad"), layer="pads")
+        self._roach_pad = self._pads.add(
+            Actor("roach_management/roach_pad"))
         self._roach_pad.pos = (WIDTH * (7/8), HEIGHT * (1/4))
-        self._inventory_pad = self.actors.add(
-            Actor("roach_management/inventory_pad"), layer="pads")
+        self._inventory_pad = self._pads.add(
+            Actor("roach_management/inventory_pad"))
         self._inventory_pad.pos = (WIDTH * (7/8), HEIGHT * (3/4))
 
     def update(self, world, dt):
@@ -36,7 +36,7 @@ class RoachesScene(Scene):
                         roach.attributes.items()),
                     )))
                 self._roach_actors[roach.name] = actor
-                self.actors.add(actor, layer="roaches")
+                self._roaches.add(actor)
             # TODO: remove missing roaches
             actor.pos = (300, 100 + i * 100)