Walking roaches.
authorSimon Cross <hodgestar@gmail.com>
Fri, 4 Mar 2016 20:24:57 +0000 (22:24 +0200)
committerSimon Cross <hodgestar@gmail.com>
Fri, 4 Mar 2016 20:24:57 +0000 (22:24 +0200)
koperkapel/roaches.py [new file with mode: 0644]
koperkapel/scenes/base.py
koperkapel/scenes/roach_management.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")
index 16353d597e607f3a96ecdd1635c971362babdf29..47891a6582784613cf4f12ae81415e32be097d53 100644 (file)
@@ -119,6 +119,12 @@ class Layer:
     def __iter__(self):
         return iter(self.actors)
 
+    def __getitem__(self, i):
+        return self.actors[i]
+
+    def __len__(self):
+        return len(self.actors)
+
     def add(self, actor):
         self.actors.append(actor)
         return actor
index cecd6ef30d2ff6e29233729b7db727009001ecdc..d0afc4e88f014a963c9e05994ae5e8fb4b2d46ce 100644 (file)
@@ -4,6 +4,7 @@ from pgzero.constants import keys, mouse
 from pgzero.actor import Actor
 from ..actors.buttons import TextButton, ImageButton
 from ..constants import WIDTH, HEIGHT
+from ..roaches import big_roaches
 from ..vehicles.base import Vehicle
 from .base import Scene, ChangeSceneEvent
 
@@ -27,7 +28,6 @@ class RoachesScene(Scene):
 
     def __init__(self):
         super().__init__()
-        self._roach_actors = {}
         self._roaches = self.actors.add_layer("roaches", level=10)
         self._pads = self.actors.add_layer("pads", level=5)
         self._seats = self.actors.add_layer("seats", level=5)
@@ -39,6 +39,7 @@ class RoachesScene(Scene):
         self._vehicle = Vehicle.by_type(world.vehicles.current)
         self._init_bg()
         self._init_seats()
+        self._init_roaches(world.roaches)
 
     def _init_bg(self):
         self.actors.default.add(self._vehicle.background)
@@ -51,6 +52,18 @@ class RoachesScene(Scene):
                 seat.pos[0] * vrad + VEHICLE_MID_X,
                 seat.pos[1] * vrad + VEHICLE_MID_Y)
 
+    def _init_roaches(self, roaches):
+        for i, roach in enumerate(roaches):
+            roach_actor = self._roaches.add(big_roaches.assemble(roach))
+            # TODO: less hacky seat assignment
+            if i < len(self._seats):
+                roach_actor.pos = self._seats[i].pos
+                # actor = TextButton("%s [%s]" % (
+                #     roach.name,
+                #     ", ".join("%s=%s" % kv for kv in sorted(
+                #         roach.attributes.items()),
+                #     )))
+
     def _init_pads(self):
         self._roach_pad = self._pads.add(
             Actor("roach_management/roach_pad", anchor=("left", "bottom")))
@@ -101,21 +114,6 @@ class RoachesScene(Scene):
     def _eject_roach(self):
         print("Eject roach.")
 
-    def update(self, world, dt):
-        for i, roach in enumerate(world.roaches):
-            if roach.name in self._roach_actors:
-                actor = self._roach_actors[roach.name]
-            else:
-                actor = TextButton("%s [%s]" % (
-                    roach.name,
-                    ", ".join("%s=%s" % kv for kv in sorted(
-                        roach.attributes.items()),
-                    )))
-                self._roach_actors[roach.name] = actor
-                self._roaches.add(actor)
-            # TODO: remove missing roaches
-            actor.pos = (300, 100 + i * 100)
-
     def on_key_down(self, key, mod, unicode):
         if key == keys.ESCAPE:
             from .menu import MenuScene