Walking roaches.
[koperkapel.git] / koperkapel / roaches.py
diff --git a/koperkapel/roaches.py b/koperkapel/roaches.py
new file mode 100644 (file)
index 0000000..9ac79be
--- /dev/null
@@ -0,0 +1,46 @@
+""" Tools for creating roach actors. """
+
+import random
+from pgzero.clock import each_tick
+from pgzero.loaders import images
+from .actors.surf import SurfActor
+
+
+class RoachActor(SurfActor):
+    def __init__(self, frames):
+        self._frames = frames
+        self._frame = random.randint(0, len(frames) - 1)
+        self._dt = 0
+        self._cycle_dt = 0.2
+        each_tick(self.update)
+        super().__init__(surf=frames[self._frame])
+
+    def update(self, dt):
+        self._dt += dt
+        while self._dt > self._cycle_dt:
+            self._dt -= self._cycle_dt
+            self._frame += 1
+            self._frame %= len(self._frames)
+        self.surf = self._frames[self._frame]
+
+
+class RoachFactory:
+
+    def __init__(self, suffix, frames=4):
+        self.suffix = suffix
+        self.frames = 4
+
+    def assemble_frame(self, i, roach_data):
+        base = images.load("roach%s/roach_%d" % (self.suffix, i + 1))
+        return base
+
+    def assemble(self, roach_data):
+        frames = [
+            self.assemble_frame(i, roach_data) for i in range(self.frames)]
+        return RoachActor(frames)
+
+
+default_roaches = RoachFactory("")
+t32_roaches = RoachFactory("_32")
+t21_roaches = RoachFactory("_21")
+big_roaches = RoachFactory("_big")