From 466b06423db1ffc8681b38da25c7caa1e9d6fca6 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Wed, 2 Mar 2016 21:32:08 +0200 Subject: [PATCH] Allow actors to be placed on different layers. --- koperkapel/scenes/base.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/koperkapel/scenes/base.py b/koperkapel/scenes/base.py index 19ba212..d0b9a95 100644 --- a/koperkapel/scenes/base.py +++ b/koperkapel/scenes/base.py @@ -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: -- 2.34.1