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
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):
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)