Simple light damage using lit_by
authorNeil <neil@dip.sun.ac.za>
Tue, 6 Sep 2016 21:58:06 +0000 (23:58 +0200)
committerNeil <neil@dip.sun.ac.za>
Tue, 6 Sep 2016 21:58:06 +0000 (23:58 +0200)
tabakrolletjie/enemies.py
tabakrolletjie/scenes/night.py

index f668f1afe0f50fbf19a6655fd7a2bf3754af6816..fd7a6d4c0df6d71c0f51c25da72f4ba45a16b17d 100644 (file)
@@ -30,6 +30,7 @@ class Mould(pymunk.Body):
         self._resistances = {}
         self._age = 0
         self._img = None
+        self._health = 500
 
     def pygame_pos(self, surface):
         """Convert to pygame coordinates and offset position so
@@ -49,6 +50,10 @@ class Mould(pymunk.Body):
         """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
+
         refresh = False
 
         if (self._age % 15) == 0 and len(moulds) < 1000:
@@ -78,6 +83,7 @@ class Mould(pymunk.Body):
                 spawn = False
             if spawn:
                 child = Mould(gamestate, space, pos)
+                child._health = self._health
                 moulds.append(child)
                 refresh = True
 
@@ -88,14 +94,21 @@ class Mould(pymunk.Body):
             refresh = True
         return refresh
 
-    def damage(self, light_color, intensity):
+    def damage(self, light_color, intensity, space, moulds):
         """Take damage for light, adjusted for resistances."""
+        self._health -= 3
+        if self._health <= 0 and self._age <= 120:
+            # We die of damage
+            space.remove(self, self._shape)
+            moulds.remove(self)
+            return True
+        return False
 
 
 class Boyd(object):
 
     def __init__(self, gamestate, space):
-        seed = Mould(gamestate, space, (350, 350))
+        seed = Mould(gamestate, space, (350, 370))
         self._moulds = [seed]
         self._image = pygame.surface.Surface(SCREEN_SIZE)
         self._image.convert_alpha(pygame.display.get_surface())
@@ -103,11 +116,17 @@ class Boyd(object):
         self._image.blit(seed.get_image(),
             seed.pygame_pos(self._image), None, pgl.BLEND_RGBA_ADD)
 
-    def tick(self, gamestate, space):
+    def tick(self, gamestate, space, lights):
         redraw = False
+        # Handle spawn events
         for mould in self._moulds[:]:
+            # Handle updates
             if mould.tick(gamestate, space, self._moulds):
                 redraw = True
+            # Check for damage
+            if lights.lit_by(mould.position):
+                if mould.damage(None, None, space, self._moulds):
+                    redraw = True
         if redraw:
             self._image.fill((0, 0, 0, 0))
             for mould in self._moulds:
index 3504461ea71fd1cdeb42f99f66242da09f27034a..b4e19e8379704b46655a9338452cbd6f498115df 100644 (file)
@@ -39,4 +39,4 @@ class NightScene(BaseScene):
 
     @debug_timer("night.tick")
     def tick(self, gamestate):
-        self._mould.tick(gamestate, self._space)
+        self._mould.tick(gamestate, self._space, self._lights)