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,
12 TURNIP_CATEGORY, COLOURS)
13 from .loader import loader
14 from .sound import sound
15 from .transforms import Multiply
16 from .utils import debug_timer
18 MOULD_FILTER = pymunk.ShapeFilter(
19 mask=MOULD_CATEGORY | OBSTACLE_CATEGORY,
20 categories=MOULD_CATEGORY)
22 EAT_TURNIP_FILTER = pymunk.ShapeFilter(mask=TURNIP_CATEGORY)
31 # Increase in health per day
36 MOULD_STAGES = [15, 25]
39 def calc_colour_transform(resistances):
41 for colour, value in resistances.items():
43 new_value = 63 + 64 * value
44 r, g, b = COLOURS[colour]
51 # Scale if we've exceeded 255
52 # Should only be required when we have lights that aren't soley red,
54 max_value = max(fr, fg, fb)
56 fr = int(255 * fr / float(max_value))
57 fg = int(255 * fg / float(max_value))
58 fb = int(255 * fb / float(max_value))
59 return Multiply(colour=(fr, fg, fb))
62 class Mould(pymunk.Body):
63 """A segment of Boyd"""
65 def __init__(self, gamestate, space, pos, resistances, transform):
66 super(Mould, self).__init__(0, 0, pymunk.Body.STATIC)
68 self._shape = pymunk.Circle(self, 16)
69 space.add(self, self._shape)
70 self._shape.filter = MOULD_FILTER
74 self.has_eyeball = False
76 self._resistances = resistances
77 self._transform = transform
79 def pygame_pos(self, surface):
80 """Convert to pygame coordinates and offset position so
81 our position is the centre of the image."""
82 # The odd sign combination is because of the pymunk / pygame
83 # transform, but we do it this way to exploit Vec2d math magic
84 return pymunk.pygame_util.to_pygame(self.position + (-16, 16), surface)
89 ('mouldA.png', 'mouldB.png', 'mouldC.png'))
91 "16" if self._age < MOULD_STAGES[0] else
92 "32" if self._age < MOULD_STAGES[1] else "64")
93 self._img = loader.load_image(size, name,
94 transform=self._transform)
97 def get_eyeball(self):
100 ('eyeballA.png', 'eyeballB.png', 'eyeballC.png'))
101 self._eyeball = loader.load_image("32", name)
104 def set_health(self, new_health):
105 self._health = new_health
107 def tick(self, gamestate, space, moulds):
108 """Grow and / or Die"""
112 # we regain a health every tick, so we heal in the dark
113 if self._health < MAX_HEALTH:
114 self._health += HEAL_FACTOR
118 if (self._age % SPAWN_RATE) == 0 and len(moulds) < MAX_ELEMENTS:
119 # Spawn a new child, if we can
121 choice = random.randint(0, 3)
123 pos = self.position + (0, 24)
125 pos = self.position + (24, 0)
127 pos = self.position + (-24, 0)
129 pos = self.position + (0, -24)
131 if pos[0] < 0 or pos[0] >= SCREEN_SIZE[0]:
133 if pos[1] < 0 or pos[1] >= SCREEN_SIZE[1]:
135 # Check for free space
136 # We allow some overlap, hence not checking full radius
137 query = space.point_query(pos, 8, MOULD_FILTER)
141 child = Mould(gamestate, space, pos, self._resistances,
143 child._health = self._health
146 if random.randint(0, 10) < 2:
147 sound.play_sound("mouth_pop_2a.ogg")
149 if self._age in MOULD_STAGES:
152 self._img = None # invalidate cached image
154 if self._age > MOULD_STAGES[1] and random.randint(0, 500) < 1:
155 # Maybe we grow an eyeball
156 self.has_eyeball = True
158 if self._age > MAX_AGE:
160 space.remove(self, self._shape)
164 # Check for turnips we can eat
165 # Note that we can only eat a tick after we spawn
166 query = space.point_query(self.position, 16, EAT_TURNIP_FILTER)
168 query[0].shape.body.turnip.eaten = True
171 def damage(self, light, space, moulds):
172 """Take damage for light, adjusted for resistances."""
173 damage = light.base_damage()
174 colour = light.colour
175 damage = int(damage * (3 - self._resistances.get(colour, 0)) / 3.0)
176 self._health -= damage
177 if self._health <= 0 and self._age <= MAX_AGE:
179 space.remove(self, self._shape)
187 def __init__(self, gamestate, space):
189 self._seen_colours = set()
190 self._mould_transform = calc_colour_transform(gamestate.resistances)
191 for position in gamestate.get_spawn_positions():
192 seed = Mould(gamestate, space, position,
193 gamestate.resistances, self._mould_transform)
194 seed.set_health(MAX_HEALTH + gamestate.days * DAY_HEALTH)
195 self._moulds.append(seed)
196 self._image = pygame.surface.Surface(SCREEN_SIZE)
197 self._image = self._image.convert_alpha(pygame.display.get_surface())
200 def _draw_moulds(self):
201 self._image.fill((0, 0, 0, 0))
202 for m in self._moulds:
203 self._image.blit(m.get_image(),
204 m.pygame_pos(self._image), None,
206 for m in self._moulds:
208 self._image.blit(m.get_eyeball(), m.pygame_pos(self._image),
211 @debug_timer('Boyd.tick')
212 def tick(self, gamestate, space, lights):
214 # Handle spawn events
215 for mould in self._moulds[:]:
217 if mould.tick(gamestate, space, self._moulds):
220 lit_by = lights.light_query(mould._shape)
222 self._seen_colours.add(light.colour)
223 if mould.damage(light, space, self._moulds):
225 break # we only die once
229 def render(self, surface):
231 surface.blit(self._image, (0, 0), None, 0)
234 return len(self._moulds) > 0
236 def update_resistances(self, gamestate):
237 for colour in self._seen_colours:
238 cur_reistance = gamestate.resistances.get(colour, 0)
239 gamestate.resistances[colour] = cur_reistance + 2
240 for colour in gamestate.resistances:
241 gamestate.resistances[colour] -= 1
242 if gamestate.resistances[colour] > 3:
243 gamestate.resistances[colour] = 3
244 if gamestate.resistances[colour] < 0:
245 gamestate.resistances[colour] = 0