Changeset 251:844bfb23d4b6


Ignore:
Timestamp:
Sep 5, 2009, 12:35:37 PM (11 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@252
Message:

Refactored animal death and added death animations.

Location:
gamelib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • gamelib/animal.py

    r243 r251  
    4141        self.rect.y = ppos[1]
    4242
     43    def die(self, gameboard):
     44        """Play death animation, noises, whatever."""
     45        if hasattr(self, 'DEATH_SOUND'):
     46            sound.play_sound(self.DEATH_SOUND)
     47        if hasattr(self, 'DEATH_ANIMATION'):
     48            gameboard.animations.append(self.DEATH_ANIMATION(self.pos))
     49        self._game_death(gameboard)
     50
     51    def _game_death(self, gameboard):
     52        # Call appropriate gameboard cleanup here.
     53        pass
     54
    4355    def move(self, state):
    4456        """Given the game state, return a new position for the object"""
     
    117129        return self.abode is None
    118130
    119     def survive_damage(self):
     131    def damage(self, gameboard):
    120132        for a in self.armour():
    121133            if not a.survive_damage():
    122134                self.unequip(a)
    123135            return True
     136        self.die(gameboard)
    124137        return False
    125138
     
    128141
    129142    EQUIPMENT_IMAGE_ATTRIBUTE = 'CHICKEN_IMAGE_FILE'
     143    DEATH_ANIMATION = animations.ChickenDeath
     144    DEATH_SOUND = 'kill-chicken.ogg'
    130145
    131146    def __init__(self, pos):
     
    135150        Animal.__init__(self, image_left, image_right, pos)
    136151        self.eggs = []
     152
     153    def _game_death(self, gameboard):
     154        gameboard.remove_chicken(self)
    137155
    138156    def move(self, gameboard):
     
    201219        self._fix_face(fox.pos)
    202220        if weapon.hit(gameboard, self, fox):
    203             sound.play_sound("kill-fox.ogg")
    204             gameboard.kill_fox(fox)
     221            fox.damage(gameboard)
    205222
    206223class Egg(Animal):
     
    225242    STEALTH = 20
    226243    IMAGE_FILE = 'sprites/fox.png'
     244    DEATH_ANIMATION = animations.FoxDeath
     245    DEATH_SOUND = 'kill-fox.ogg'
    227246
    228247    costs = {
     
    249268        self.closest = None
    250269        self.last_steps = []
     270
     271    def _game_death(self, gameboard):
     272        gameboard.kill_fox(self)
    251273
    252274    def _cost_tile(self, pos, gameboard):
     
    344366    def _catch_chicken(self, chicken, gameboard):
    345367        """Catch a chicken"""
    346         if not chicken.survive_damage():
    347             sound.play_sound("kill-chicken.ogg")
    348             gameboard.remove_chicken(chicken)
     368        chicken.damage(gameboard)
    349369        self.closest = None
    350370        self.hunting = False
     
    458478
    459479    def _catch_chicken(self, chicken, gameboard):
    460         gameboard.remove_chicken(chicken)
     480        chicken.damage(gameboard)
    461481        self.closest = None
    462482        self.chickens_eaten += 1
  • gamelib/animations.py

    r241 r251  
    1818       # (assuming all animations are triggered by the move loop, of course)
    1919
    20     def __init__(self, sequence, tile_pos):
     20    def __init__(self, tile_pos, sequence=None):
    2121        # Create the first frame
     22        if sequence is None:
     23            sequence = self.SEQUENCE
    2224        self.iter = iter(sequence)
    2325        Sprite.__init__(self, self.iter.next(), (-1000, -1000))
     
    5355    def __init__(self, chicken):
    5456        if chicken.facing == 'right':
    55             Animation.__init__(self, self.SEQUENCE_RIGHT, chicken.pos)
     57            Animation.__init__(self, chicken.pos, self.SEQUENCE_RIGHT)
    5658        else:
    57             Animation.__init__(self, self.SEQUENCE_LEFT, chicken.pos)
     59            Animation.__init__(self, chicken.pos, self.SEQUENCE_LEFT)
    5860
    5961class FenceExplosion(Animation):
    60 
    6162    FLASH_LEFT = imagecache.load_image('sprites/muzzle_flash.png')
    6263    FLASH_RIGHT = imagecache.load_image('sprites/muzzle_flash.png',
    6364            ("right_facing",))
    64 
    6565    SEQUENCE = [FLASH_LEFT, FLASH_RIGHT, FLASH_LEFT, FLASH_RIGHT]
    6666
    67     def __init__(self, fencetile):
    68         Animation.__init__(self, self.SEQUENCE, fencetile)
    69 
     67class FoxDeath(Animation):
     68    BLOOD_SPLAT = imagecache.load_image('sprites/fox_death.png')
     69    SEQUENCE = [BLOOD_SPLAT, BLOOD_SPLAT]
     70   
     71class ChickenDeath(Animation):
     72    BLOOD_SPLAT = imagecache.load_image('sprites/fox_death.png')
     73    FEATHER_SPLAT = imagecache.load_image('sprites/chkn_death.png')
     74    SEQUENCE = [BLOOD_SPLAT, FEATHER_SPLAT, FEATHER_SPLAT]
  • gamelib/gameboard.py

    r247 r251  
    760760
    761761    def kill_fox(self, fox):
    762         if fox in self.foxes:
    763             if not fox.survive_damage():
    764                 self.killed_foxes += 1
    765                 self.toolbar.update_fox_counter(self.killed_foxes)
    766                 self.add_cash(constants.SELL_PRICE_DEAD_FOX)
    767                 self.remove_fox(fox)
     762        self.killed_foxes += 1
     763        self.toolbar.update_fox_counter(self.killed_foxes)
     764        self.add_cash(constants.SELL_PRICE_DEAD_FOX)
     765        self.remove_fox(fox)
    768766
    769767    def remove_fox(self, fox):
Note: See TracChangeset for help on using the changeset viewer.