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, Overlay
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 = [7, 13]
41 def calc_colour_transform(resistances):
43 for colour, value in resistances.items():
45 new_value = 63 + 64 * value
46 r, g, b = COLOURS[colour]
53 # Scale if we've exceeded 255
54 # Should only be required when we have lights that aren't soley red,
56 max_value = max(fr, fg, fb)
58 fr = int(255 * fr / float(max_value))
59 fg = int(255 * fg / float(max_value))
60 fb = int(255 * fb / float(max_value))
61 return Multiply(colour=(fr, fg, fb))
64 class Mould(pymunk.Body):
65 """A segment of Boyd"""
67 def __init__(self, gamestate, space, pos, resistances, transform):
68 super(Mould, self).__init__(0, 0, pymunk.Body.STATIC)
70 self._shape = pymunk.Circle(self, MOULD_RADIUS)
71 space.add(self, self._shape)
72 self._shape.filter = MOULD_FILTER
76 self.has_eyeball = False
78 self._resistances = resistances
79 self._transform = transform
81 def pygame_pos(self, surface):
82 """Convert to pygame coordinates and offset position so
83 our position is the centre of the image."""
84 # The odd sign combination is because of the pymunk / pygame
85 # transform, but we do it this way to exploit Vec2d math magic
86 return pymunk.pygame_util.to_pygame(
87 self.position + (-MOULD_RADIUS, MOULD_RADIUS), surface)
92 ('mouldA.png', 'mouldB.png', 'mouldC.png'))
94 "16" if self._age < MOULD_STAGES[0] else
95 "32" if self._age < MOULD_STAGES[1] else "64")
96 self._img = loader.load_image(size, name,
97 transform=self._transform)
100 def get_eyeball(self):
101 if not self._eyeball:
102 name = random.choice(
103 ('eyeballA.png', 'eyeballB.png', 'eyeballC.png'))
104 self._eyeball = loader.load_image("32", name,
105 transform=Overlay(colour=self._transform.colour+(127,)))
106 eyelid = loader.load_image("32", "eyelid.png", transform=self._transform)
107 self._eyeball.blit(eyelid, (0, 0), None)
110 def set_health(self, new_health):
111 self._health = new_health
113 def tick(self, gamestate, space, moulds):
114 """Grow and / or Die"""
118 # we regain a health every tick, so we heal in the dark
119 if self._health < MAX_HEALTH:
120 self._health += HEAL_FACTOR
124 if (self._age % SPAWN_RATE) == 0 and len(moulds) < MAX_ELEMENTS:
125 # Spawn a new child, if we can
127 choice = random.randint(0, 3)
129 pos = self.position + (0, 24)
131 pos = self.position + (24, 0)
133 pos = self.position + (-24, 0)
135 pos = self.position + (0, -24)
137 if pos[0] < 0 or pos[0] >= SCREEN_SIZE[0]:
139 if pos[1] < 0 or pos[1] >= SCREEN_SIZE[1]:
141 # Check for free space
142 # We allow some overlap, hence not checking full radius
143 query = space.point_query(pos, 8, MOULD_FILTER)
147 child = Mould(gamestate, space, pos, self._resistances,
149 child._health = self._health
152 if random.randint(0, 100) < 1:
153 sound.play_sound("rubber_toy_short%d.ogg" % random.randint(1, 5), volume=0.3)
155 if self._age in MOULD_STAGES:
158 self._img = None # invalidate cached image
160 if self._age > MOULD_STAGES[1] and random.randint(0, 500) < 1:
161 # Maybe we grow an eyeball
162 self.has_eyeball = True
163 sound.play_sound("mouth_pop_2a.ogg", volume=0.5)
165 if self._age > MAX_AGE:
167 space.remove(self, self._shape)
171 # Check for turnips we can eat
172 # Note that we can only eat a tick after we spawn
173 query = space.point_query(self.position, MOULD_RADIUS,
176 query[0].shape.body.turnip.eaten = True
177 sound.play_sound("eating_chips_%d.ogg" % random.randint(1, 3), volume=0.8)
180 def damage(self, light, space, moulds):
181 """Take damage for light, adjusted for resistances."""
182 damage = light.base_damage()
183 colour = light.colour
184 damage = int(damage * (3 - self._resistances.get(colour, 0)) / 3.0)
185 self._health -= damage
186 if self._health <= 0 and self._age <= MAX_AGE:
188 space.remove(self, self._shape)
196 def __init__(self, gamestate, space):
198 self._seen_colours = set()
199 self._mould_transform = calc_colour_transform(gamestate.resistances)
200 for position in gamestate.get_spawn_positions():
201 seed = Mould(gamestate, space, position,
202 gamestate.resistances, self._mould_transform)
203 seed.set_health(MAX_HEALTH + gamestate.days * DAY_HEALTH)
204 self._moulds.append(seed)
205 self._image = pygame.surface.Surface(SCREEN_SIZE)
206 self._image = self._image.convert_alpha(pygame.display.get_surface())
209 def _draw_moulds(self):
210 self._image.fill((0, 0, 0, 0))
211 for m in self._moulds:
212 self._image.blit(m.get_image(),
213 m.pygame_pos(self._image), None,
215 for m in self._moulds:
217 self._image.blit(m.get_eyeball(), m.pygame_pos(self._image),
220 @debug_timer('Boyd.tick')
221 def tick(self, gamestate, space, lights):
223 # Handle spawn events
224 for mould in self._moulds[:]:
226 if mould.tick(gamestate, space, self._moulds):
229 lit_by = lights.lit_by(mould.position, MOULD_RADIUS)
231 self._seen_colours.add(light.colour)
232 if mould.damage(light, space, self._moulds):
234 break # we only die once
238 def render(self, surface):
240 surface.blit(self._image, (0, 0), None, 0)
243 return len(self._moulds) > 0
245 def update_resistances(self, gamestate):
246 for colour in self._seen_colours:
247 cur_reistance = gamestate.resistances.get(colour, 0)
248 gamestate.resistances[colour] = cur_reistance + 2
249 for colour in gamestate.resistances:
250 gamestate.resistances[colour] -= 1
251 if gamestate.resistances[colour] > 3:
252 gamestate.resistances[colour] = 3
253 if gamestate.resistances[colour] < 0:
254 gamestate.resistances[colour] = 0