Factor out frame based actors.
[koperkapel.git] / koperkapel / roaches.py
index 20220c4c6da52165651910e691ad354d62c83dde..c844b5277c404c41c4448239d58daa564fecb570 100644 (file)
@@ -1,35 +1,25 @@
 """ Tools for creating roach actors. """
 
-import random
-from pgzero.clock import each_tick
 from pgzero.loaders import images
 from pygame.constants import BLEND_RGBA_MULT
-from .actors.surf import SurfActor
+from .actors.animsurf import AnimatedSurfActor
+from .serums import roach_serum_color
 
-ROACH_COLORS = {
-    "blue": (0, 0, 255, 255),
-    "green": (0, 255, 0, 255),
-    "purple": (255, 0, 255, 255),
-    "brown": (170, 68, 0, 255),
-}
 
+def roach_by_name(world, roach_name):
+    roaches = [r for r in world.roaches if r.name == roach_name]
+    if not roaches:
+        return None
+    return roaches[0]
 
-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 WorldRoach(object):
+    """A roach proxy with no properties for display on the game level."""
+
+    def __init__(self):
+        self.smart = False
+        self.strong = False
+        self.fast = False
 
 
 class RoachFactory:
@@ -38,32 +28,25 @@ class RoachFactory:
         self.suffix = suffix
         self.frames = 4
 
-    def roach_color(self, roach):
-        if roach.smart:
-            return ROACH_COLORS["blue"]
-        elif roach.fast:
-            return ROACH_COLORS["green"]
-        elif roach.strong:
-            return ROACH_COLORS["purple"]
-        return ROACH_COLORS["brown"]
-
     def assemble_frame(self, i, color, roach_data):
         roach = images.load("roach%s/roach_%d" % (self.suffix, i + 1))
         eyes = images.load("roach%s/eyes_%d" % (self.suffix, i + 1))
-        roach = roach.copy()
-        roach.fill(color, None, BLEND_RGBA_MULT)
-        roach.blit(eyes, (0, 0))
-        return roach
+        frame = roach.copy()
+        frame.fill(color, None, BLEND_RGBA_MULT)
+        frame.blit(eyes, (0, 0))
+        return frame
 
     def assemble(self, roach_data):
-        color = self.roach_color(roach_data)
+        color = roach_serum_color(roach_data)
         frames = [
             self.assemble_frame(i, color, roach_data)
             for i in range(self.frames)]
-        return RoachActor(frames)
+        return AnimatedSurfActor(frames)
 
 
 default_roaches = RoachFactory("")
 t32_roaches = RoachFactory("_32")
 t21_roaches = RoachFactory("_21")
 big_roaches = RoachFactory("_big")
+roaches_quartet = RoachFactory("_quartet")
+roaches_nonet = RoachFactory("_nonet")