changeset 396:19e583e5cdc0

Refactor for further move work
author Neil Muller <drnlmuller@gmail.com>
date Thu, 12 Nov 2009 21:31:52 +0000
parents 2d0ff46118e2
children 532f1ea476ff
files gamelib/animal.py gamelib/engine.py gamelib/gameboard.py
diffstat 3 files changed, 31 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/animal.py	Thu Nov 12 21:14:21 2009 +0000
+++ b/gamelib/animal.py	Thu Nov 12 21:31:52 2009 +0000
@@ -57,6 +57,11 @@
         # Default is not to move
         pass
 
+    def attack(self, gameboard):
+        """Given the game state, attack a suitable target"""
+        # Default is not to attack
+        pass
+
     def set_pos(self, tile_pos):
         """Move an animal to the given tile_pos."""
         new_pos = Position(*tile_pos)
@@ -364,8 +369,7 @@
             self.hunting = False
             return self.pos
         if self.closest.pos == self.pos:
-            # Caught a chicken
-            self._catch_chicken(self.closest, gameboard)
+            # No need to move
             return self.pos
         if self.closest.pos.to_tile_tuple() == self.pos.to_tile_tuple():
             # Only differ in z, so next step is in z
@@ -376,6 +380,11 @@
             return Position(self.pos.x, self.pos.y, new_z)
         return self._find_best_path_step(self.closest.pos, gameboard)
 
+    def attack(self, gameboard):
+        """Attack a chicken"""
+        if self.closest and self.closest.pos == self.pos:
+            self._catch_chicken(self.closest, gameboard)
+
     def _catch_chicken(self, chicken, gameboard):
         """Catch a chicken"""
         chicken.damage(gameboard)
--- a/gamelib/engine.py	Thu Nov 12 21:14:21 2009 +0000
+++ b/gamelib/engine.py	Thu Nov 12 21:31:52 2009 +0000
@@ -226,7 +226,7 @@
             self.cycle_count += 1
             if self.cycle_count > constants.NIGHT_LENGTH:
                 return pygame.event.post(START_DAY)
-            if self.game.gameboard.move_foxes():
+            if self.game.gameboard.do_night_step():
                 # All foxes are gone/safe, so dawn happens
                 return pygame.event.post(START_DAY)
             # Re-enable timers
--- a/gamelib/gameboard.py	Thu Nov 12 21:14:21 2009 +0000
+++ b/gamelib/gameboard.py	Thu Nov 12 21:31:52 2009 +0000
@@ -283,7 +283,7 @@
         self.tv.tga_load_level(level.map)
         width, height = self.tv.size
         # Ensure we don't every try to create more foxes then is sane
-        self.max_foxes = min(height+width-15, level.max_foxes)
+        self.max_foxes = min(2*height+2*width-15, level.max_foxes)
         self.create_display()
 
         self.selected_tool = None
@@ -788,21 +788,35 @@
         if self.toolbar.anim_clear_tool:
             self.toolbar.clear_tool()
 
-    def move_foxes(self):
-        """Move the foxes.
-        
+    def do_night_step(self):
+        """Handle the events of the night.
+
            We return True if there are no more foxes to move or all the
            foxes are safely back. This end's the night"""
         if not self.foxes:
             return True
+        # Move all the foxes
+        over = self.foxes_move()
+        if not over:
+            self.foxes_attack()
+            self.chickens_attack()
+        return over
+
+    def foxes_move(self):
         over = True
         for fox in self.foxes:
             fox.move(self)
             if not fox.safe:
                 over = False
+        return over
+
+    def foxes_attack(self):
+        for fox in self.foxes:
+            fox.attack(self)
+
+    def chickens_attack(self):
         for chicken in self.chickens:
             chicken.attack(self)
-        return over
 
     def add_chicken(self, chicken):
         self.chickens.add(chicken)