changeset 419:d110d55c8449

Move hatching logic into chickens.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 21 Nov 2009 15:22:41 +0000
parents 2ccfadcae3b2
children f20ccd43a282
files gamelib/animal.py gamelib/gameboard.py
diffstat 2 files changed, 25 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- 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."""
--- 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