--- /dev/null
+""" 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")
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
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)
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)
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")))
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