0d2448216c16a00900759736ed66e32fe975dbcb
[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 pygame.constants import BLEND_RGBA_MULT
7 from .actors.orientatedsurf import OrientatedSurfActor
8 from .serums import roach_serum_color
9
10
11 class RoachActor(OrientatedSurfActor):
12     def __init__(self, frames):
13         self._frames = frames
14         self._frame = random.randint(0, len(frames) - 1)
15         self._dt = 0
16         self._cycle_dt = 0.2
17         each_tick(self.update)
18         super().__init__(surf=frames[self._frame], angle=0)
19
20     def update(self, dt):
21         self._dt += dt
22         while self._dt > self._cycle_dt:
23             self._dt -= self._cycle_dt
24             self._frame += 1
25             self._frame %= len(self._frames)
26         self.surf = self._frames[self._frame]
27
28
29 class RoachFactory:
30
31     def __init__(self, suffix, frames=4):
32         self.suffix = suffix
33         self.frames = 4
34
35     def assemble_frame(self, i, color, roach_data):
36         roach = images.load("roach%s/roach_%d" % (self.suffix, i + 1))
37         eyes = images.load("roach%s/eyes_%d" % (self.suffix, i + 1))
38         frame = roach.copy()
39         frame.fill(color, None, BLEND_RGBA_MULT)
40         frame.blit(eyes, (0, 0))
41         return frame
42
43     def assemble(self, roach_data):
44         color = roach_serum_color(roach_data)
45         frames = [
46             self.assemble_frame(i, color, roach_data)
47             for i in range(self.frames)]
48         return RoachActor(frames)
49
50
51 default_roaches = RoachFactory("")
52 t32_roaches = RoachFactory("_32")
53 t21_roaches = RoachFactory("_21")
54 big_roaches = RoachFactory("_big")