this is totally a real rat
[koperkapel.git] / koperkapel / roaches.py
1 """ Tools for creating roach actors. """
2
3 from pgzero.loaders import images
4 from pygame.constants import BLEND_RGBA_MULT
5 from .actors.animsurf import AnimatedSurfActor
6 from .serums import roach_serum_color
7 from .util import safepath
8
9 NAMES = [
10     "roupert",
11     "roachel",
12     "roeginald",
13     "roichard",
14     "rory",
15     "roalph",
16     "roabia",
17     "roafi",
18     "roaman",
19     "roemus",
20     "roadley",
21     "roanaell",
22     "roashwan",
23     "roashid",
24     "roaphael",
25     "roenfield",
26     "roani",
27     "roaya",
28     "roaza",
29     "robekka",
30     "rogan",
31     "roiana",
32     "roberta",
33 ]
34
35
36 def roach_by_name(world, roach_name):
37     roaches = [r for r in world.roaches if r.name == roach_name]
38     if not roaches:
39         return None
40     return roaches[0]
41
42
43 def next_roach_name(world):
44     roach_names = [x['name'] for x in world.roaches]
45     for cand in NAMES:
46         if cand not in roach_names:
47             return cand
48
49
50 def build_roach(world, name=None, health=5, **kw):
51     if name is None:
52         name = next_roach_name(world)
53     if name is None:
54         return
55     roach = {
56         "name": name,
57         "health": health,
58     }
59     roach.update(kw)
60     return roach
61
62
63 class WorldRoach(object):
64     """A roach proxy with no properties for display on the game level."""
65
66     def __init__(self):
67         self.smart = False
68         self.strong = False
69         self.fast = False
70
71
72 class RoachFactory:
73
74     def __init__(self, suffix, frames=4):
75         self.suffix = suffix
76         self.frames = 4
77
78     def assemble_frame(self, i, color, roach_data, weapon=None):
79         roach = images.load(safepath("roach%s/roach_%d") % (self.suffix, i + 1))
80         eyes = images.load(safepath("roach%s/eyes_%d") % (self.suffix, i + 1))
81         if weapon is None:
82             frame = roach.copy()
83             frame.fill(color, None, BLEND_RGBA_MULT)
84         else:
85             frame = weapon.surf.copy()
86             roach = roach.copy()
87             roach.fill(color, None, BLEND_RGBA_MULT)
88             frame.blit(roach, (0, 0))
89         frame.blit(eyes, (0, 0))
90         return frame
91
92     def assemble(self, roach_data, color=None, weapon=None):
93         if not color:
94             color = roach_serum_color(roach_data)
95         frames = []
96         frames = [
97             self.assemble_frame(i, color, roach_data, weapon)
98             for i in range(self.frames)]
99         return AnimatedSurfActor(frames)
100
101
102 class RatFactory:
103     
104     def __init__(self, frames=4):
105         self.frames = 4
106
107     def assemble_frame(self, i):
108         roach = images.load(safepath("rat/rat_%d") % (i + 1))
109         return roach
110
111     def assemble(self):
112         frames = [self.assemble_frame(i) for i in range(self.frames)]
113         return AnimatedSurfActor(frames)
114
115 default_roaches = RoachFactory("")
116 t32_roaches = RoachFactory("_32")
117 t21_roaches = RoachFactory("_21")
118 big_roaches = RoachFactory("_big")
119 roaches_quartet = RoachFactory("_quartet")
120 roaches_nonet = RoachFactory("_nonet")
121 default_rats = RatFactory()