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