fixed eyeball colour; associated popping sound with eyeball
[tabakrolletjie.git] / tabakrolletjie / enemies.py
index 0fe1619fc175f63b970deb882e8d1ed5a15e6e6a..0bd2d43f4afe720ae31cc37f6052889147383a8d 100644 (file)
@@ -8,61 +8,122 @@ import pygame.draw
 import pygame.surface
 import pygame.display
 
-import pygame.locals as pgl
-
-from .constants import SCREEN_SIZE, MOULD_CATEGORY, OBSTACLE_CATEGORY
+from .constants import (SCREEN_SIZE, MOULD_CATEGORY, OBSTACLE_CATEGORY,
+                        TURNIP_CATEGORY, COLOURS)
 from .loader import loader
 from .sound import sound
+from .transforms import Multiply
+from .utils import debug_timer
 
 MOULD_FILTER = pymunk.ShapeFilter(
     mask=MOULD_CATEGORY | OBSTACLE_CATEGORY,
     categories=MOULD_CATEGORY)
 
+EAT_TURNIP_FILTER = pymunk.ShapeFilter(mask=TURNIP_CATEGORY)
+
+
+# Boyd parameters
+SPAWN_RATE = 5
+MAX_AGE = 30
+MAX_ELEMENTS = 400
+MAX_HEALTH = 100
+
+# Increase in health per day
+DAY_HEALTH = 10
+
+HEAL_FACTOR = 1
+
+MOULD_STAGES = [7, 13]
+
+MOULD_RADIUS = 16
+
+
+def calc_colour_transform(resistances):
+    fr = fg = fb = 0
+    for colour, value in resistances.items():
+        if value:
+            new_value = 63 + 64 * value
+            r, g, b = COLOURS[colour]
+            if r:
+                fr += new_value
+            if g:
+                fg += new_value
+            if b:
+                fb += new_value
+    # Scale if we've exceeded 255
+    # Should only be required when we have lights that aren't soley red,
+    # green or blue
+    max_value = max(fr, fg, fb)
+    if max_value > 255:
+        fr = int(255 * fr / float(max_value))
+        fg = int(255 * fg / float(max_value))
+        fb = int(255 * fb / float(max_value))
+    return Multiply(colour=(fr, fg, fb))
+
 
 class Mould(pymunk.Body):
     """A segment of Boyd"""
 
-    def __init__(self, gamestate, space, pos):
+    def __init__(self, gamestate, space, pos, resistances, transform):
         super(Mould, self).__init__(0, 0, pymunk.Body.STATIC)
         self.position = pos
-        self._shape = pymunk.Circle(self, 16)
+        self._shape = pymunk.Circle(self, MOULD_RADIUS)
         space.add(self, self._shape)
         self._shape.filter = MOULD_FILTER
-        self._resistances = {}
         self._age = 0
         self._img = None
         self._health = 500
+        self.has_eyeball = False
+        self._eyeball = None
+        self._resistances = resistances
+        self._transform = transform
 
     def pygame_pos(self, surface):
         """Convert to pygame coordinates and offset position so
            our position is the centre of the image."""
         # The odd sign combination is because of the pymunk / pygame
         # transform, but we do it this way to exploit Vec2d math magic
-        return pymunk.pygame_util.to_pygame(self.position + (-16, 16), surface)
+        return pymunk.pygame_util.to_pygame(
+            self.position + (-MOULD_RADIUS, MOULD_RADIUS), surface)
 
     def get_image(self):
         if not self._img:
             name = random.choice(
                 ('mouldA.png', 'mouldB.png', 'mouldC.png'))
-            size = "16" if self._age < 10 else "32" if self._age < 20 else "64"
-            self._img = loader.load_image(size, name)
+            size = (
+                "16" if self._age < MOULD_STAGES[0] else
+                "32" if self._age < MOULD_STAGES[1] else "64")
+            self._img = loader.load_image(size, name,
+                                          transform=self._transform)
         return self._img
 
+    def get_eyeball(self):
+        if not self._eyeball:
+            name = random.choice(
+                ('eyeballA.png', 'eyeballB.png', 'eyeballC.png'))
+            self._eyeball = loader.load_image("32", name)
+            eyelid = loader.load_image("32", "eyelid.png", transform=self._transform)
+            self._eyeball.blit(eyelid, (0, 0), None)
+        return self._eyeball
+
+    def set_health(self, new_health):
+        self._health = new_health
+
     def tick(self, gamestate, space, moulds):
         """Grow and / or Die"""
 
         self._age += 1
 
         # we regain a health every tick, so we heal in the dark
-        if self._health < 100:
-            self._health += 1
+        if self._health < MAX_HEALTH:
+            self._health += HEAL_FACTOR
 
         refresh = False
 
-        if (self._age % 15) == 0 and len(moulds) < 1000:
+        if (self._age % SPAWN_RATE) == 0 and len(moulds) < MAX_ELEMENTS:
             # Spawn a new child, if we can
             spawn = True
-            choice = random.randint(0, 4)
+            choice = random.randint(0, 3)
             if choice == 0:
                 pos = self.position + (0, 24)
             elif choice == 1:
@@ -80,34 +141,45 @@ class Mould(pymunk.Body):
             # We allow some overlap, hence not checking full radius
             query = space.point_query(pos, 8, MOULD_FILTER)
             if query:
-                # for x in query:
-                #     if not isinstance(x.shape.body, Mould):
-                #         print x.shape, x.shape.body
                 spawn = False
             if spawn:
-                child = Mould(gamestate, space, pos)
+                child = Mould(gamestate, space, pos, self._resistances,
+                              self._transform)
                 child._health = self._health
                 moulds.append(child)
                 refresh = True
-                if random.randint(0, 10) < 2:
-                    sound.play_sound("mouth_pop_2a.ogg")
 
-        if self._age in (10, 20):
-            # Segment grows in size
+        if self._age in MOULD_STAGES:
+            # We grow in size
             refresh = True
-            self._img = None # invalidate cached image
+            self._img = None  # invalidate cached image
+
+        if self._age > MOULD_STAGES[1] and random.randint(0, 500) < 1:
+            # Maybe we grow an eyeball
+            self.has_eyeball = True
+            sound.play_sound("mouth_pop_2a.ogg")
 
-        if self._age > 120:
+        if self._age > MAX_AGE:
             # We die of old age
             space.remove(self, self._shape)
             moulds.remove(self)
             refresh = True
+        else:
+            # Check for turnips we can eat
+            # Note that we can only eat a tick after we spawn
+            query = space.point_query(self.position, MOULD_RADIUS,
+                                      EAT_TURNIP_FILTER)
+            if query:
+                query[0].shape.body.turnip.eaten = True
         return refresh
 
-    def damage(self, light_color, intensity, space, moulds):
+    def damage(self, light, space, moulds):
         """Take damage for light, adjusted for resistances."""
-        self._health -= 3
-        if self._health <= 0 and self._age <= 120:
+        damage = light.base_damage()
+        colour = light.colour
+        damage = int(damage * (3 - self._resistances.get(colour, 0)) / 3.0)
+        self._health -= damage
+        if self._health <= 0 and self._age <= MAX_AGE:
             # We die of damage
             space.remove(self, self._shape)
             moulds.remove(self)
@@ -118,10 +190,16 @@ class Mould(pymunk.Body):
 class Boyd(object):
 
     def __init__(self, gamestate, space):
-        seed = Mould(gamestate, space, (350, 370))
-        self._moulds = [seed]
+        self._moulds = []
+        self._seen_colours = set()
+        self._mould_transform = calc_colour_transform(gamestate.resistances)
+        for position in gamestate.get_spawn_positions():
+            seed = Mould(gamestate, space, position,
+                         gamestate.resistances, self._mould_transform)
+            seed.set_health(MAX_HEALTH + gamestate.days * DAY_HEALTH)
+            self._moulds.append(seed)
         self._image = pygame.surface.Surface(SCREEN_SIZE)
-        self._image.convert_alpha(pygame.display.get_surface())
+        self._image = self._image.convert_alpha(pygame.display.get_surface())
         self._draw_moulds()
 
     def _draw_moulds(self):
@@ -130,7 +208,12 @@ class Boyd(object):
             self._image.blit(m.get_image(),
                              m.pygame_pos(self._image), None,
                              0)
+        for m in self._moulds:
+            if m.has_eyeball:
+                self._image.blit(m.get_eyeball(), m.pygame_pos(self._image),
+                                 None, 0)
 
+    @debug_timer('Boyd.tick')
     def tick(self, gamestate, space, lights):
         redraw = False
         # Handle spawn events
@@ -139,10 +222,10 @@ class Boyd(object):
             if mould.tick(gamestate, space, self._moulds):
                 redraw = True
             # Check for damage
-            lit_by = lights.light_query(mould._shape)
+            lit_by = lights.lit_by(mould.position, MOULD_RADIUS)
             for light in lit_by:
-                # Todo: extract colour and intensity from light
-                if mould.damage(None, None, space, self._moulds):
+                self._seen_colours.add(light.colour)
+                if mould.damage(light, space, self._moulds):
                     redraw = True
                     break  # we only die once
         if redraw:
@@ -150,4 +233,18 @@ class Boyd(object):
 
     def render(self, surface):
         """Draw ourselves"""
-        surface.blit(self._image, (0, 0), None, pgl.BLEND_RGBA_ADD)
+        surface.blit(self._image, (0, 0), None, 0)
+
+    def alive(self):
+        return len(self._moulds) > 0
+
+    def update_resistances(self, gamestate):
+        for colour in self._seen_colours:
+            cur_reistance = gamestate.resistances.get(colour, 0)
+            gamestate.resistances[colour] = cur_reistance + 2
+        for colour in gamestate.resistances:
+            gamestate.resistances[colour] -= 1
+            if gamestate.resistances[colour] > 3:
+                gamestate.resistances[colour] = 3
+            if gamestate.resistances[colour] < 0:
+                gamestate.resistances[colour] = 0