comparison gamelib/animal.py @ 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 4f86c2616cdf
children fcaae2cfe3cd
comparison
equal deleted inserted replaced
250:048510e95812 251:844bfb23d4b6
38 def loop(self, tv, _sprite): 38 def loop(self, tv, _sprite):
39 ppos = tv.tile_to_view(self.pos.to_tuple()) 39 ppos = tv.tile_to_view(self.pos.to_tuple())
40 self.rect.x = ppos[0] 40 self.rect.x = ppos[0]
41 self.rect.y = ppos[1] 41 self.rect.y = ppos[1]
42 42
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
43 def move(self, state): 55 def move(self, state):
44 """Given the game state, return a new position for the object""" 56 """Given the game state, return a new position for the object"""
45 # Default is not to move 57 # Default is not to move
46 pass 58 pass
47 59
114 return tile_pos[0] == self.pos.x and tile_pos[1] == self.pos.y 126 return tile_pos[0] == self.pos.x and tile_pos[1] == self.pos.y
115 127
116 def outside(self): 128 def outside(self):
117 return self.abode is None 129 return self.abode is None
118 130
119 def survive_damage(self): 131 def damage(self, gameboard):
120 for a in self.armour(): 132 for a in self.armour():
121 if not a.survive_damage(): 133 if not a.survive_damage():
122 self.unequip(a) 134 self.unequip(a)
123 return True 135 return True
136 self.die(gameboard)
124 return False 137 return False
125 138
126 class Chicken(Animal): 139 class Chicken(Animal):
127 """A chicken""" 140 """A chicken"""
128 141
129 EQUIPMENT_IMAGE_ATTRIBUTE = 'CHICKEN_IMAGE_FILE' 142 EQUIPMENT_IMAGE_ATTRIBUTE = 'CHICKEN_IMAGE_FILE'
143 DEATH_ANIMATION = animations.ChickenDeath
144 DEATH_SOUND = 'kill-chicken.ogg'
130 145
131 def __init__(self, pos): 146 def __init__(self, pos):
132 image_left = imagecache.load_image('sprites/chkn.png') 147 image_left = imagecache.load_image('sprites/chkn.png')
133 image_right = imagecache.load_image('sprites/chkn.png', 148 image_right = imagecache.load_image('sprites/chkn.png',
134 ("right_facing",)) 149 ("right_facing",))
135 Animal.__init__(self, image_left, image_right, pos) 150 Animal.__init__(self, image_left, image_right, pos)
136 self.eggs = [] 151 self.eggs = []
152
153 def _game_death(self, gameboard):
154 gameboard.remove_chicken(self)
137 155
138 def move(self, gameboard): 156 def move(self, gameboard):
139 """A free chicken will move away from other free chickens""" 157 """A free chicken will move away from other free chickens"""
140 pass 158 pass
141 159
198 fox = self._find_killable_fox(weapon, gameboard) 216 fox = self._find_killable_fox(weapon, gameboard)
199 if not fox: 217 if not fox:
200 return 218 return
201 self._fix_face(fox.pos) 219 self._fix_face(fox.pos)
202 if weapon.hit(gameboard, self, fox): 220 if weapon.hit(gameboard, self, fox):
203 sound.play_sound("kill-fox.ogg") 221 fox.damage(gameboard)
204 gameboard.kill_fox(fox)
205 222
206 class Egg(Animal): 223 class Egg(Animal):
207 """An egg""" 224 """An egg"""
208 225
209 def __init__(self, pos): 226 def __init__(self, pos):
222 class Fox(Animal): 239 class Fox(Animal):
223 """A fox""" 240 """A fox"""
224 241
225 STEALTH = 20 242 STEALTH = 20
226 IMAGE_FILE = 'sprites/fox.png' 243 IMAGE_FILE = 'sprites/fox.png'
244 DEATH_ANIMATION = animations.FoxDeath
245 DEATH_SOUND = 'kill-fox.ogg'
227 246
228 costs = { 247 costs = {
229 # weighting for movement calculation 248 # weighting for movement calculation
230 'grassland' : 2, 249 'grassland' : 2,
231 'woodland' : 1, # Try to keep to the woods if possible 250 'woodland' : 1, # Try to keep to the woods if possible
246 self.dig_pos = None 265 self.dig_pos = None
247 self.tick = 0 266 self.tick = 0
248 self.safe = False 267 self.safe = False
249 self.closest = None 268 self.closest = None
250 self.last_steps = [] 269 self.last_steps = []
270
271 def _game_death(self, gameboard):
272 gameboard.kill_fox(self)
251 273
252 def _cost_tile(self, pos, gameboard): 274 def _cost_tile(self, pos, gameboard):
253 if gameboard.in_bounds(pos): 275 if gameboard.in_bounds(pos):
254 this_tile = gameboard.tv.get(pos.to_tuple()) 276 this_tile = gameboard.tv.get(pos.to_tuple())
255 cost = self.costs.get(tiles.TILE_MAP[this_tile], 100) 277 cost = self.costs.get(tiles.TILE_MAP[this_tile], 100)
341 return self.pos 363 return self.pos
342 return self._find_best_path_step(self.closest.pos, gameboard) 364 return self._find_best_path_step(self.closest.pos, gameboard)
343 365
344 def _catch_chicken(self, chicken, gameboard): 366 def _catch_chicken(self, chicken, gameboard):
345 """Catch a chicken""" 367 """Catch a chicken"""
346 if not chicken.survive_damage(): 368 chicken.damage(gameboard)
347 sound.play_sound("kill-chicken.ogg")
348 gameboard.remove_chicken(chicken)
349 self.closest = None 369 self.closest = None
350 self.hunting = False 370 self.hunting = False
351 self.last_steps = [] # Forget history here 371 self.last_steps = [] # Forget history here
352 372
353 def _update_pos(self, gameboard, new_pos): 373 def _update_pos(self, gameboard, new_pos):
455 def __init__(self, pos): 475 def __init__(self, pos):
456 Fox.__init__(self, pos) 476 Fox.__init__(self, pos)
457 self.chickens_eaten = 0 477 self.chickens_eaten = 0
458 478
459 def _catch_chicken(self, chicken, gameboard): 479 def _catch_chicken(self, chicken, gameboard):
460 gameboard.remove_chicken(chicken) 480 chicken.damage(gameboard)
461 self.closest = None 481 self.closest = None
462 self.chickens_eaten += 1 482 self.chickens_eaten += 1
463 if self.chickens_eaten > 2: 483 if self.chickens_eaten > 2:
464 self.hunting = False 484 self.hunting = False
465 self.last_steps = [] 485 self.last_steps = []