333c8270b8a92b3ddc358aef9fbd28983ab98e60
[tabakrolletjie.git] / tabakrolletjie / enemies.py
1 # Boyd, the friendly, misunderstood turnip loving, light hating space mould
2
3 import random
4
5 import pymunk
6 import pymunk.pygame_util
7 import pygame.draw
8 import pygame.surface
9 import pygame.display
10
11 from .constants import (SCREEN_SIZE, MOULD_CATEGORY, OBSTACLE_CATEGORY,
12                         TURNIP_CATEGORY, COLOURS)
13 from .loader import loader
14 from .sound import sound
15 from .transforms import Multiply
16 from .utils import debug_timer
17
18 MOULD_FILTER = pymunk.ShapeFilter(
19     mask=MOULD_CATEGORY | OBSTACLE_CATEGORY,
20     categories=MOULD_CATEGORY)
21
22 EAT_TURNIP_FILTER = pymunk.ShapeFilter(mask=TURNIP_CATEGORY)
23
24
25 # Boyd parameters
26 SPAWN_RATE = 10
27 MAX_AGE = 60
28 MAX_ELEMENTS = 400
29 MAX_HEALTH = 100
30
31 # Increase in health per day
32 DAY_HEALTH = 10
33
34 HEAL_FACTOR = 1
35
36 MOULD_STAGES = [15, 25]
37
38
39 def calc_colour_transform(resistances):
40     fr = fg = fb = 0
41     for colour, value in resistances.items():
42         if value:
43             new_value = 63 + 64 * value
44             r, g, b = COLOURS[colour]
45             if r:
46                 fr += new_value
47             if g:
48                 fg += new_value
49             if b:
50                 fb += new_value
51     # Scale if we've exceeded 255
52     # Should only be required when we have lights that aren't soley red,
53     # green or blue
54     max_value = max(fr, fg, fb)
55     if max_value > 255:
56         fr = int(255 * fr / float(max_value))
57         fg = int(255 * fg / float(max_value))
58         fb = int(255 * fb / float(max_value))
59     return Multiply(colour=(fr, fg, fb))
60
61
62 class Mould(pymunk.Body):
63     """A segment of Boyd"""
64
65     def __init__(self, gamestate, space, pos, resistances, transform):
66         super(Mould, self).__init__(0, 0, pymunk.Body.STATIC)
67         self.position = pos
68         self._shape = pymunk.Circle(self, 16)
69         space.add(self, self._shape)
70         self._shape.filter = MOULD_FILTER
71         self._age = 0
72         self._img = None
73         self._health = 500
74         self.has_eyeball = False
75         self._eyeball = None
76         self._resistances = resistances
77         self._transform = transform
78
79     def pygame_pos(self, surface):
80         """Convert to pygame coordinates and offset position so
81            our position is the centre of the image."""
82         # The odd sign combination is because of the pymunk / pygame
83         # transform, but we do it this way to exploit Vec2d math magic
84         return pymunk.pygame_util.to_pygame(self.position + (-16, 16), surface)
85
86     def get_image(self):
87         if not self._img:
88             name = random.choice(
89                 ('mouldA.png', 'mouldB.png', 'mouldC.png'))
90             size = "16" if self._age < MOULD_STAGES[0] else "32" if self._age < MOULD_STAGES[1] else "64"
91             self._img = loader.load_image(size, name,
92                                           transform=self._transform)
93         return self._img
94
95     def get_eyeball(self):
96         if not self._eyeball:
97             name = random.choice(
98                 ('eyeballA.png', 'eyeballB.png', 'eyeballC.png'))
99             self._eyeball = loader.load_image("32", name)
100         return self._eyeball
101
102     def set_health(self, new_health):
103         self._health = new_health
104
105     def tick(self, gamestate, space, moulds):
106         """Grow and / or Die"""
107
108         self._age += 1
109
110         # we regain a health every tick, so we heal in the dark
111         if self._health < MAX_HEALTH:
112             self._health += HEAL_FACTOR
113
114         refresh = False
115
116         if (self._age % SPAWN_RATE) == 0 and len(moulds) < MAX_ELEMENTS:
117             # Spawn a new child, if we can
118             spawn = True
119             choice = random.randint(0, 3)
120             if choice == 0:
121                 pos = self.position + (0, 24)
122             elif choice == 1:
123                 pos = self.position + (24, 0)
124             elif choice == 2:
125                 pos = self.position + (-24, 0)
126             else:
127                 pos = self.position + (0, -24)
128             # check for bounds
129             if pos[0] < 0 or pos[0] >= SCREEN_SIZE[0]:
130                 spawn = False
131             if pos[1] < 0 or pos[1] >= SCREEN_SIZE[1]:
132                 spawn = False
133             # Check for free space
134             # We allow some overlap, hence not checking full radius
135             query = space.point_query(pos, 8, MOULD_FILTER)
136             if query:
137                 spawn = False
138             if spawn:
139                 child = Mould(gamestate, space, pos, self._resistances,
140                               self._transform)
141                 child._health = self._health
142                 moulds.append(child)
143                 refresh = True
144                 if random.randint(0, 10) < 2:
145                     sound.play_sound("mouth_pop_2a.ogg")
146
147         if self._age in MOULD_STAGES:
148             # We grow in size
149             refresh = True
150             self._img = None  # invalidate cached image
151
152         if self._age > MOULD_STAGES[1] and random.randint(0, 500) < 1:
153             # Maybe we grow an eyeball
154             self.has_eyeball = True
155
156         if self._age > MAX_AGE:
157             # We die of old age
158             space.remove(self, self._shape)
159             moulds.remove(self)
160             refresh = True
161         else:
162             # Check for turnips we can eat
163             # Note that we can only eat a tick after we spawn
164             query = space.point_query(self.position, 16, EAT_TURNIP_FILTER)
165             if query:
166                 query[0].shape.body.turnip.eaten = True
167         return refresh
168
169     def damage(self, light, space, moulds):
170         """Take damage for light, adjusted for resistances."""
171         damage = light.base_damage()
172         colour = light.colour
173         damage = int(damage * (3 - self._resistances.get(colour, 0)) / 3.0)
174         self._health -= damage
175         if self._health <= 0 and self._age <= MAX_AGE:
176             # We die of damage
177             space.remove(self, self._shape)
178             moulds.remove(self)
179             return True
180         return False
181
182
183 class Boyd(object):
184
185     def __init__(self, gamestate, space):
186         self._moulds = []
187         self._seen_colours = set()
188         self._mould_transform = calc_colour_transform(gamestate.resistances)
189         for position in gamestate.get_spawn_positions():
190             seed = Mould(gamestate, space, position,
191                          gamestate.resistances, self._mould_transform)
192             seed.set_health(MAX_HEALTH + gamestate.days * DAY_HEALTH)
193             self._moulds.append(seed)
194         self._image = pygame.surface.Surface(SCREEN_SIZE)
195         self._image = self._image.convert_alpha(pygame.display.get_surface())
196         self._draw_moulds()
197
198     def _draw_moulds(self):
199         self._image.fill((0, 0, 0, 0))
200         for m in self._moulds:
201             self._image.blit(m.get_image(),
202                              m.pygame_pos(self._image), None,
203                              0)
204         for m in self._moulds:
205             if m.has_eyeball:
206                 self._image.blit(m.get_eyeball(), m.pygame_pos(self._image),
207                                  None, 0)
208
209     @debug_timer('Boyd.tick')
210     def tick(self, gamestate, space, lights):
211         redraw = False
212         # Handle spawn events
213         for mould in self._moulds[:]:
214             # Handle updates
215             if mould.tick(gamestate, space, self._moulds):
216                 redraw = True
217             # Check for damage
218             lit_by = lights.light_query(mould._shape)
219             for light in lit_by:
220                 self._seen_colours.add(light.colour)
221                 if mould.damage(light, space, self._moulds):
222                     redraw = True
223                     break  # we only die once
224         if redraw:
225             self._draw_moulds()
226
227     def render(self, surface):
228         """Draw ourselves"""
229         surface.blit(self._image, (0, 0), None, 0)
230
231     def alive(self):
232         return len(self._moulds) > 0
233
234     def update_resistances(self, gamestate):
235         for colour in self._seen_colours:
236             cur_reistance = gamestate.resistances.get(colour, 0)
237             gamestate.resistances[colour] = cur_reistance + 2
238         for colour in gamestate.resistances:
239             gamestate.resistances[colour] -= 1
240             if gamestate.resistances[colour] > 3:
241                 gamestate.resistances[colour] = 3
242             if gamestate.resistances[colour] < 0:
243                 gamestate.resistances[colour] = 0