Walking roaches.
[koperkapel.git] / koperkapel / roaches.py
1 """ Tools for creating roach actors. """
2
3 import random
4 from pgzero.clock import each_tick
5 from pgzero.loaders import images
6 from .actors.surf import SurfActor
7
8
9 class RoachActor(SurfActor):
10     def __init__(self, frames):
11         self._frames = frames
12         self._frame = random.randint(0, len(frames) - 1)
13         self._dt = 0
14         self._cycle_dt = 0.2
15         each_tick(self.update)
16         super().__init__(surf=frames[self._frame])
17
18     def update(self, dt):
19         self._dt += dt
20         while self._dt > self._cycle_dt:
21             self._dt -= self._cycle_dt
22             self._frame += 1
23             self._frame %= len(self._frames)
24         self.surf = self._frames[self._frame]
25
26
27 class RoachFactory:
28
29     def __init__(self, suffix, frames=4):
30         self.suffix = suffix
31         self.frames = 4
32
33     def assemble_frame(self, i, roach_data):
34         base = images.load("roach%s/roach_%d" % (self.suffix, i + 1))
35         return base
36
37     def assemble(self, roach_data):
38         frames = [
39             self.assemble_frame(i, roach_data) for i in range(self.frames)]
40         return RoachActor(frames)
41
42
43 default_roaches = RoachFactory("")
44 t32_roaches = RoachFactory("_32")
45 t21_roaches = RoachFactory("_21")
46 big_roaches = RoachFactory("_big")