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