changeset 251:844bfb23d4b6

Refactored animal death and added death animations.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 05 Sep 2009 12:35:37 +0000
parents 048510e95812
children e12b39132ffb
files gamelib/animal.py gamelib/animations.py gamelib/gameboard.py
diffstat 3 files changed, 44 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/animal.py	Sat Sep 05 12:34:32 2009 +0000
+++ b/gamelib/animal.py	Sat Sep 05 12:35:37 2009 +0000
@@ -40,6 +40,18 @@
         self.rect.x = ppos[0]
         self.rect.y = ppos[1]
 
+    def die(self, gameboard):
+        """Play death animation, noises, whatever."""
+        if hasattr(self, 'DEATH_SOUND'):
+            sound.play_sound(self.DEATH_SOUND)
+        if hasattr(self, 'DEATH_ANIMATION'):
+            gameboard.animations.append(self.DEATH_ANIMATION(self.pos))
+        self._game_death(gameboard)
+
+    def _game_death(self, gameboard):
+        # Call appropriate gameboard cleanup here.
+        pass
+
     def move(self, state):
         """Given the game state, return a new position for the object"""
         # Default is not to move
@@ -116,17 +128,20 @@
     def outside(self):
         return self.abode is None
 
-    def survive_damage(self):
+    def damage(self, gameboard):
         for a in self.armour():
             if not a.survive_damage():
                 self.unequip(a)
             return True
+        self.die(gameboard)
         return False
 
 class Chicken(Animal):
     """A chicken"""
 
     EQUIPMENT_IMAGE_ATTRIBUTE = 'CHICKEN_IMAGE_FILE'
+    DEATH_ANIMATION = animations.ChickenDeath
+    DEATH_SOUND = 'kill-chicken.ogg'
 
     def __init__(self, pos):
         image_left = imagecache.load_image('sprites/chkn.png')
@@ -135,6 +150,9 @@
         Animal.__init__(self, image_left, image_right, pos)
         self.eggs = []
 
+    def _game_death(self, gameboard):
+        gameboard.remove_chicken(self)
+
     def move(self, gameboard):
         """A free chicken will move away from other free chickens"""
         pass
@@ -200,8 +218,7 @@
             return
         self._fix_face(fox.pos)
         if weapon.hit(gameboard, self, fox):
-            sound.play_sound("kill-fox.ogg")
-            gameboard.kill_fox(fox)
+            fox.damage(gameboard)
 
 class Egg(Animal):
     """An egg"""
@@ -224,6 +241,8 @@
 
     STEALTH = 20
     IMAGE_FILE = 'sprites/fox.png'
+    DEATH_ANIMATION = animations.FoxDeath
+    DEATH_SOUND = 'kill-fox.ogg'
 
     costs = {
             # weighting for movement calculation
@@ -249,6 +268,9 @@
         self.closest = None
         self.last_steps = []
 
+    def _game_death(self, gameboard):
+        gameboard.kill_fox(self)
+
     def _cost_tile(self, pos, gameboard):
         if gameboard.in_bounds(pos):
             this_tile = gameboard.tv.get(pos.to_tuple())
@@ -343,9 +365,7 @@
 
     def _catch_chicken(self, chicken, gameboard):
         """Catch a chicken"""
-        if not chicken.survive_damage():
-            sound.play_sound("kill-chicken.ogg")
-            gameboard.remove_chicken(chicken)
+        chicken.damage(gameboard)
         self.closest = None
         self.hunting = False
         self.last_steps = [] # Forget history here
@@ -457,7 +477,7 @@
         self.chickens_eaten = 0
 
     def _catch_chicken(self, chicken, gameboard):
-        gameboard.remove_chicken(chicken)
+        chicken.damage(gameboard)
         self.closest = None
         self.chickens_eaten += 1
         if self.chickens_eaten > 2:
--- a/gamelib/animations.py	Sat Sep 05 12:34:32 2009 +0000
+++ b/gamelib/animations.py	Sat Sep 05 12:35:37 2009 +0000
@@ -17,8 +17,10 @@
        # will cause issues as this will overrun the next move loop.
        # (assuming all animations are triggered by the move loop, of course)
 
-    def __init__(self, sequence, tile_pos):
+    def __init__(self, tile_pos, sequence=None):
         # Create the first frame
+        if sequence is None:
+            sequence = self.SEQUENCE
         self.iter = iter(sequence)
         Sprite.__init__(self, self.iter.next(), (-1000, -1000))
         if hasattr(tile_pos, 'to_tuple'):
@@ -52,18 +54,21 @@
 
     def __init__(self, chicken):
         if chicken.facing == 'right':
-            Animation.__init__(self, self.SEQUENCE_RIGHT, chicken.pos)
+            Animation.__init__(self, chicken.pos, self.SEQUENCE_RIGHT)
         else:
-            Animation.__init__(self, self.SEQUENCE_LEFT, chicken.pos)
+            Animation.__init__(self, chicken.pos, self.SEQUENCE_LEFT)
 
 class FenceExplosion(Animation):
-
     FLASH_LEFT = imagecache.load_image('sprites/muzzle_flash.png')
     FLASH_RIGHT = imagecache.load_image('sprites/muzzle_flash.png',
             ("right_facing",))
-
     SEQUENCE = [FLASH_LEFT, FLASH_RIGHT, FLASH_LEFT, FLASH_RIGHT]
 
-    def __init__(self, fencetile):
-        Animation.__init__(self, self.SEQUENCE, fencetile)
-
+class FoxDeath(Animation):
+    BLOOD_SPLAT = imagecache.load_image('sprites/fox_death.png')
+    SEQUENCE = [BLOOD_SPLAT, BLOOD_SPLAT]
+    
+class ChickenDeath(Animation):
+    BLOOD_SPLAT = imagecache.load_image('sprites/fox_death.png')
+    FEATHER_SPLAT = imagecache.load_image('sprites/chkn_death.png')
+    SEQUENCE = [BLOOD_SPLAT, FEATHER_SPLAT, FEATHER_SPLAT]
--- a/gamelib/gameboard.py	Sat Sep 05 12:34:32 2009 +0000
+++ b/gamelib/gameboard.py	Sat Sep 05 12:35:37 2009 +0000
@@ -759,12 +759,10 @@
         self.toolbar.update_egg_counter(self.eggs)
 
     def kill_fox(self, fox):
-        if fox in self.foxes:
-            if not fox.survive_damage():
-                self.killed_foxes += 1
-                self.toolbar.update_fox_counter(self.killed_foxes)
-                self.add_cash(constants.SELL_PRICE_DEAD_FOX)
-                self.remove_fox(fox)
+        self.killed_foxes += 1
+        self.toolbar.update_fox_counter(self.killed_foxes)
+        self.add_cash(constants.SELL_PRICE_DEAD_FOX)
+        self.remove_fox(fox)
 
     def remove_fox(self, fox):
         self.foxes.discard(fox)