Add Boyd's spawn position to stations
[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)
13 from .loader import loader
14 from .sound import sound
15
16 MOULD_FILTER = pymunk.ShapeFilter(
17     mask=MOULD_CATEGORY | OBSTACLE_CATEGORY,
18     categories=MOULD_CATEGORY)
19
20 EAT_TURNIP_FILTER = pymunk.ShapeFilter(mask=TURNIP_CATEGORY)
21
22
23 class Mould(pymunk.Body):
24     """A segment of Boyd"""
25
26     def __init__(self, gamestate, space, pos):
27         super(Mould, self).__init__(0, 0, pymunk.Body.STATIC)
28         self.position = pos
29         self._shape = pymunk.Circle(self, 16)
30         space.add(self, self._shape)
31         self._shape.filter = MOULD_FILTER
32         self._resistances = {}
33         self._age = 0
34         self._img = None
35         self._health = 500
36         self.has_eyeball = False
37         self._eyeball = None
38
39     def pygame_pos(self, surface):
40         """Convert to pygame coordinates and offset position so
41            our position is the centre of the image."""
42         # The odd sign combination is because of the pymunk / pygame
43         # transform, but we do it this way to exploit Vec2d math magic
44         return pymunk.pygame_util.to_pygame(self.position + (-16, 16), surface)
45
46     def get_image(self):
47         if not self._img:
48             name = random.choice(
49                 ('mouldA.png', 'mouldB.png', 'mouldC.png'))
50             size = "16" if self._age < 10 else "32" if self._age < 20 else "64"
51             self._img = loader.load_image(size, name)
52         return self._img
53
54     def get_eyeball(self):
55         if not self._eyeball:
56             name = random.choice(
57                 ('eyeballA.png', 'eyeballB.png', 'eyeballC.png'))
58             self._eyeball = loader.load_image("32", name)
59         return self._eyeball
60
61     def tick(self, gamestate, space, moulds):
62         """Grow and / or Die"""
63
64         self._age += 1
65
66         # we regain a health every tick, so we heal in the dark
67         if self._health < 100:
68             self._health += 1
69
70         refresh = False
71
72         if (self._age % 15) == 0 and len(moulds) < 1000:
73             # Spawn a new child, if we can
74             spawn = True
75             choice = random.randint(0, 4)
76             if choice == 0:
77                 pos = self.position + (0, 24)
78             elif choice == 1:
79                 pos = self.position + (24, 0)
80             elif choice == 2:
81                 pos = self.position + (-24, 0)
82             else:
83                 pos = self.position + (0, -24)
84             # check for bounds
85             if pos[0] < 0 or pos[0] >= SCREEN_SIZE[0]:
86                 spawn = False
87             if pos[1] < 0 or pos[1] >= SCREEN_SIZE[1]:
88                 spawn = False
89             # Check for free space
90             # We allow some overlap, hence not checking full radius
91             query = space.point_query(pos, 8, MOULD_FILTER)
92             if query:
93                 # for x in query:
94                 #     if not isinstance(x.shape.body, Mould):
95                 #         print x.shape, x.shape.body
96                 spawn = False
97             if spawn:
98                 child = Mould(gamestate, space, pos)
99                 child._health = self._health
100                 moulds.append(child)
101                 refresh = True
102                 if random.randint(0, 10) < 2:
103                     sound.play_sound("mouth_pop_2a.ogg")
104
105         if self._age in (10, 20):
106             # We grow in size
107             refresh = True
108             self._img = None  # invalidate cached image
109
110         if self._age > 20 and random.randint(0, 500) < 1:
111             # Maybe we grow an eyeball
112             self.has_eyeball = True
113
114         if self._age > 120:
115             # We die of old age
116             space.remove(self, self._shape)
117             moulds.remove(self)
118             refresh = True
119         else:
120             # Check for turnips we can eat
121             # Note that we can only eat a tick after we spawn
122             query = space.point_query(self.position, 16, EAT_TURNIP_FILTER)
123             if query:
124                 query[0].shape.body.turnip.eaten = True
125         return refresh
126
127     def damage(self, light, space, moulds):
128         """Take damage for light, adjusted for resistances."""
129         self._health -= light.base_damage()
130         if self._health <= 0 and self._age <= 120:
131             # We die of damage
132             space.remove(self, self._shape)
133             moulds.remove(self)
134             return True
135         return False
136
137
138 class Boyd(object):
139
140     def __init__(self, gamestate, space):
141         self._moulds = []
142         for position in gamestate.get_spawn_positions():
143             seed = Mould(gamestate, space, position)
144             self._moulds.append(seed)
145         self._image = pygame.surface.Surface(SCREEN_SIZE)
146         self._image = self._image.convert_alpha(pygame.display.get_surface())
147         self._draw_moulds()
148
149     def _draw_moulds(self):
150         self._image.fill((0, 0, 0, 0))
151         for m in self._moulds:
152             self._image.blit(m.get_image(),
153                              m.pygame_pos(self._image), None,
154                              0)
155         for m in self._moulds:
156             if m.has_eyeball:
157                 self._image.blit(m.get_eyeball(), m.pygame_pos(self._image),
158                                  None, 0)
159
160     def tick(self, gamestate, space, lights):
161         redraw = False
162         # Handle spawn events
163         for mould in self._moulds[:]:
164             # Handle updates
165             if mould.tick(gamestate, space, self._moulds):
166                 redraw = True
167             # Check for damage
168             lit_by = lights.light_query(mould._shape)
169             for light in lit_by:
170                 # Todo: extract colour and intensity from light
171                 if mould.damage(light, space, self._moulds):
172                     redraw = True
173                     break  # we only die once
174         if redraw:
175             self._draw_moulds()
176
177     def render(self, surface):
178         """Draw ourselves"""
179         surface.blit(self._image, (0, 0), None, 0)