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