changeset 108:437cbd856a03

Add occupants and abodes. Allowing moving chickens around.
author Simon Cross <hodgestar@gmail.com>
date Wed, 02 Sep 2009 18:42:00 +0000
parents 672cc5598e77
children 48019afde338
files gamelib/animal.py gamelib/buildings.py gamelib/gameboard.py
diffstat 3 files changed, 39 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/animal.py	Wed Sep 02 18:26:15 2009 +0000
+++ b/gamelib/animal.py	Wed Sep 02 18:42:00 2009 +0000
@@ -20,6 +20,7 @@
         self.image_right = image_right
         self.pos = Position(tile_pos[0], tile_pos[1])
         self.equipment = []
+        self.abode = None
 
     def loop(self, tv, _sprite):
         ppos = tv.tile_to_view(self.pos.to_tuple())
@@ -31,6 +32,12 @@
         # Default is not to move
         pass
 
+    def set_pos(self, tile_pos):
+        """Move an animal to the given tile_pos."""
+        new_pos = Position(*tile_pos)
+        self._fix_face(new_pos)
+        self.pos = new_pos
+
     def _fix_face(self, final_pos):
         """Set the face correctly"""
         if final_pos.left_of(self.pos):
--- a/gamelib/buildings.py	Wed Sep 02 18:26:15 2009 +0000
+++ b/gamelib/buildings.py	Wed Sep 02 18:42:00 2009 +0000
@@ -19,6 +19,7 @@
         self.tile_no = self.TILE_NO
         self._buy_price = self.BUY_PRICE
         self._sell_price = self.SELL_PRICE
+        self._occupants = set()
 
         # Create the building somewhere far off screen
         Sprite.__init__(self, self.day_image, (-1000, -1000))
@@ -84,6 +85,20 @@
         else:
             self.setimage(self.night_image)
 
+    def occupants(self):
+        """Return list of buildings occupants."""
+        return list(self._occupants)
+
+    def add_occupant(self, occupant):
+        if occupant.abode is not None:
+            occupant.abode.remove_occupant(occupant)
+        occupant.abode = self
+        self._occupants.add(occupant)
+
+    def remove_occupant(self, occupant):
+        if occupant in self._occupants:
+            self._occupants.remove(occupant)
+            occupant.abode = None
 
 class HenHouse(Building):
     """A HenHouse."""
--- a/gamelib/gameboard.py	Wed Sep 02 18:26:15 2009 +0000
+++ b/gamelib/gameboard.py	Wed Sep 02 18:42:00 2009 +0000
@@ -217,20 +217,29 @@
            """
         chicken = self.get_chicken(tile_pos)
         if chicken:
-            self.animal_to_place = chicken
-            print "Selected animal %r" % (chicken,)
+            if chicken is self.animal_to_place:
+                self.animal_to_place = None
+            else:
+                self.animal_to_place = chicken
+            print "Selected animal %r" % (self.animal_to_place,)
             return
         building = self.get_building(tile_pos)
         if building:
             if self.animal_to_place is not None:
-                self.put_animal_in_building(self.animal_to_place, building)
+                occupant = self.animal_to_place
+                if occupant in self.tv.sprites:
+                    self.tv.sprites.remove(occupant)
+                building.add_occupant(occupant)
+                print building, building.occupants()
             else:
                 self.select_animal_from_building(building)
-
-    def put_animal_in_building(self, animal, building):
-        """Place animal in building."""
-        # XXX: unimplemented
-        print "Placing %r in %r" % (animal, building)
+            return
+        if self.tv.get(tile_pos) == self.GRASSLAND:
+            if self.animal_to_place is not None:
+                occupant = self.animal_to_place
+                if occupant.abode is not None:
+                    occupant.abode.remove_occupant(occupant)
+                occupant.set_pos(tile_pos)
 
     def select_animal_from_building(self, building):
         """Create dialog for selecting an animal from a building."""