# HG changeset patch # User Jeremy Thurgood # Date 1258816961 0 # Node ID d110d55c8449e998708d65f34c0ee8370f3bb712 # Parent 2ccfadcae3b29548666ac83b872b446e964f72ee Move hatching logic into chickens. diff -r 2ccfadcae3b2 -r d110d55c8449 gamelib/animal.py --- a/gamelib/animal.py Sat Nov 21 13:14:47 2009 +0000 +++ b/gamelib/animal.py Sat Nov 21 15:22:41 2009 +0000 @@ -168,6 +168,9 @@ self.lay(gameboard) self.reload_weapon() + def start_day(self, gameboard): + self.hatch(gameboard) + def _game_death(self, gameboard): gameboard.remove_chicken(self) @@ -211,8 +214,7 @@ for egg in self.eggs[:]: gameboard.sell_one_egg(self) self.remove_eggs() # clean up stale images, etc. - return chick - return None + gameboard.place_hatched_chicken(chick, self.abode.building) def _find_killable_fox(self, weapon, gameboard): """Choose a random fox within range of this weapon.""" diff -r 2ccfadcae3b2 -r d110d55c8449 gamelib/gameboard.py --- a/gamelib/gameboard.py Sat Nov 21 13:14:47 2009 +0000 +++ b/gamelib/gameboard.py Sat Nov 21 15:22:41 2009 +0000 @@ -370,7 +370,7 @@ self._cache_animal_positions() self.spawn_foxes() self.eggs = 0 - for chicken in self.chickens: + for chicken in self.chickens.copy(): chicken.start_night(self) self.toolbar.update_egg_counter(self.eggs) @@ -382,7 +382,9 @@ self._pos_cache = { 'fox' : [], 'chicken' : []} self.advance_day() self.clear_foxes() - self.hatch_eggs() + for chicken in self.chickens.copy(): + chicken.start_day(self) + self.toolbar.update_egg_counter(self.eggs) def in_bounds(self, pos): """Check if a position is within the game boundaries""" @@ -876,29 +878,23 @@ self.buildings.append(building) self.tv.sprites.append(building, layer='buildings') - def hatch_eggs(self): - for building in self.buildings: - if building.HENHOUSE: - for chicken in building.occupants(): - new_chick = chicken.hatch(self) - if new_chick: - try: - building.add_occupant(new_chick) - self.add_chicken(new_chick) - new_chick.equip(equipment.Nest()) - except buildings.BuildingFullError: - # No space in the hen house, look nearby - for tile_pos in building.adjacent_tiles(): - if self.tv.get(tile_pos) != self.GRASSLAND: - continue - if self.get_outside_chicken(tile_pos) is None: - self.add_chicken(new_chick) - self.relocate_animal(new_chick, tile_pos=tile_pos) - break - # if there isn't a space for the - # new chick it dies. :/ Farm life - # is cruel. - self.toolbar.update_egg_counter(self.eggs) + def place_hatched_chicken(self, new_chick, building): + try: + building.add_occupant(new_chick) + self.add_chicken(new_chick) + new_chick.equip(equipment.Nest()) + except buildings.BuildingFullError: + # No space in the hen house, look nearby + for tile_pos in building.adjacent_tiles(): + if self.tv.get(tile_pos) != self.GRASSLAND: + continue + if self.get_outside_chicken(tile_pos) is None: + self.add_chicken(new_chick) + self.relocate_animal(new_chick, tile_pos=tile_pos) + break + # if there isn't a space for the + # new chick it dies. :/ Farm life + # is cruel. def kill_fox(self, fox): self.killed_foxes += 1