From: Simon Cross Date: Fri, 4 Mar 2016 20:24:57 +0000 (+0200) Subject: Walking roaches. X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=8c2e63f91b3b2710f1fb510f45a0755777231efc;p=koperkapel.git Walking roaches. --- diff --git a/koperkapel/roaches.py b/koperkapel/roaches.py new file mode 100644 index 0000000..9ac79be --- /dev/null +++ b/koperkapel/roaches.py @@ -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") diff --git a/koperkapel/scenes/base.py b/koperkapel/scenes/base.py index 16353d5..47891a6 100644 --- a/koperkapel/scenes/base.py +++ b/koperkapel/scenes/base.py @@ -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 diff --git a/koperkapel/scenes/roach_management.py b/koperkapel/scenes/roach_management.py index cecd6ef..d0afc4e 100644 --- a/koperkapel/scenes/roach_management.py +++ b/koperkapel/scenes/roach_management.py @@ -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