From bf20049741bc0d599092143738e4633f1f87de8f Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Wed, 2 Mar 2016 22:23:49 +0200 Subject: [PATCH] Better layer management. --- koperkapel/scenes/base.py | 40 ++++++++++++++++++++++++++---------- koperkapel/scenes/roaches.py | 14 ++++++------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/koperkapel/scenes/base.py b/koperkapel/scenes/base.py index d0b9a95..fd53f81 100644 --- a/koperkapel/scenes/base.py +++ b/koperkapel/scenes/base.py @@ -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 diff --git a/koperkapel/scenes/roaches.py b/koperkapel/scenes/roaches.py index 7198ee2..09169f3 100644 --- a/koperkapel/scenes/roaches.py +++ b/koperkapel/scenes/roaches.py @@ -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) -- 2.34.1