1 # Boyd, the friendly, misunderstood turnip loving, light hating space mould
6 import pymunk.pygame_util
11 from .constants import (SCREEN_SIZE, MOULD_CATEGORY, OBSTACLE_CATEGORY,
13 from .loader import loader
14 from .sound import sound
16 MOULD_FILTER = pymunk.ShapeFilter(
17 mask=MOULD_CATEGORY | OBSTACLE_CATEGORY,
18 categories=MOULD_CATEGORY)
20 EAT_TURNIP_FILTER = pymunk.ShapeFilter(mask=TURNIP_CATEGORY)
29 # Increase in health per day
34 MOULD_STAGES = [15, 25]
37 class Mould(pymunk.Body):
38 """A segment of Boyd"""
40 def __init__(self, gamestate, space, pos):
41 super(Mould, self).__init__(0, 0, pymunk.Body.STATIC)
43 self._shape = pymunk.Circle(self, 16)
44 space.add(self, self._shape)
45 self._shape.filter = MOULD_FILTER
46 self._resistances = {}
50 self.has_eyeball = False
53 def pygame_pos(self, surface):
54 """Convert to pygame coordinates and offset position so
55 our position is the centre of the image."""
56 # The odd sign combination is because of the pymunk / pygame
57 # transform, but we do it this way to exploit Vec2d math magic
58 return pymunk.pygame_util.to_pygame(self.position + (-16, 16), surface)
63 ('mouldA.png', 'mouldB.png', 'mouldC.png'))
64 size = "16" if self._age < MOULD_STAGES[0] else "32" if self._age < MOULD_STAGES[1] else "64"
65 self._img = loader.load_image(size, name)
68 def get_eyeball(self):
71 ('eyeballA.png', 'eyeballB.png', 'eyeballC.png'))
72 self._eyeball = loader.load_image("32", name)
75 def set_health(self, new_health):
76 self._health = new_health
78 def tick(self, gamestate, space, moulds):
79 """Grow and / or Die"""
83 # we regain a health every tick, so we heal in the dark
84 if self._health < MAX_HEALTH:
85 self._health += HEAL_FACTOR
89 if (self._age % SPAWN_RATE) == 0 and len(moulds) < MAX_ELEMENTS:
90 # Spawn a new child, if we can
92 choice = random.randint(0, 3)
94 pos = self.position + (0, 24)
96 pos = self.position + (24, 0)
98 pos = self.position + (-24, 0)
100 pos = self.position + (0, -24)
102 if pos[0] < 0 or pos[0] >= SCREEN_SIZE[0]:
104 if pos[1] < 0 or pos[1] >= SCREEN_SIZE[1]:
106 # Check for free space
107 # We allow some overlap, hence not checking full radius
108 query = space.point_query(pos, 8, MOULD_FILTER)
111 # if not isinstance(x.shape.body, Mould):
112 # print x.shape, x.shape.body
115 child = Mould(gamestate, space, pos)
116 child._health = self._health
119 if random.randint(0, 10) < 2:
120 sound.play_sound("mouth_pop_2a.ogg")
122 if self._age in MOULD_STAGES:
125 self._img = None # invalidate cached image
127 if self._age > MOULD_STAGES[1] and random.randint(0, 500) < 1:
128 # Maybe we grow an eyeball
129 self.has_eyeball = True
131 if self._age > MAX_AGE:
133 space.remove(self, self._shape)
137 # Check for turnips we can eat
138 # Note that we can only eat a tick after we spawn
139 query = space.point_query(self.position, 16, EAT_TURNIP_FILTER)
141 query[0].shape.body.turnip.eaten = True
144 def damage(self, light, space, moulds):
145 """Take damage for light, adjusted for resistances."""
146 self._health -= light.base_damage()
147 if self._health <= 0 and self._age <= MAX_AGE:
149 space.remove(self, self._shape)
157 def __init__(self, gamestate, space):
159 for position in gamestate.get_spawn_positions():
160 seed = Mould(gamestate, space, position)
161 seed.set_health(MAX_HEALTH + gamestate.days * DAY_HEALTH)
162 self._moulds.append(seed)
163 self._image = pygame.surface.Surface(SCREEN_SIZE)
164 self._image = self._image.convert_alpha(pygame.display.get_surface())
167 def _draw_moulds(self):
168 self._image.fill((0, 0, 0, 0))
169 for m in self._moulds:
170 self._image.blit(m.get_image(),
171 m.pygame_pos(self._image), None,
173 for m in self._moulds:
175 self._image.blit(m.get_eyeball(), m.pygame_pos(self._image),
178 def tick(self, gamestate, space, lights):
180 # Handle spawn events
181 for mould in self._moulds[:]:
183 if mould.tick(gamestate, space, self._moulds):
186 lit_by = lights.light_query(mould._shape)
188 # Todo: extract colour and intensity from light
189 if mould.damage(light, space, self._moulds):
191 break # we only die once
195 def render(self, surface):
197 surface.blit(self._image, (0, 0), None, 0)
200 return len(self._moulds) > 0