changeset 557:50d6c68ce267

Add gameboard to buildings. Update save version as this breaks old save games
author Neil Muller <drnlmuller@gmail.com>
date Sat, 28 Nov 2009 18:20:46 +0000
parents 46f6f1a98f3f
children 5cdf26bde2f2
files gamelib/animal.py gamelib/buildings.py gamelib/gameboard.py gamelib/version.py
diffstat 4 files changed, 22 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/animal.py	Sat Nov 28 18:04:28 2009 +0000
+++ b/gamelib/animal.py	Sat Nov 28 18:20:46 2009 +0000
@@ -672,7 +672,7 @@
         fence = self.gameboard.get_building(self.dig_pos.to_tile_tuple())
         # Another fox could have made the same hole this turn
         if fence:
-            fence.damage(self.gameboard.tv)
+            fence.damage()
         self.dig_pos = None
 
     def move(self):
--- a/gamelib/buildings.py	Sat Nov 28 18:04:28 2009 +0000
+++ b/gamelib/buildings.py	Sat Nov 28 18:20:46 2009 +0000
@@ -93,12 +93,14 @@
         '_sun_on',
         '_broken',
         '_floors',
+        'gameboard',
     ]
 
-    def __init__(self, pos):
+    def __init__(self, pos, gameboard):
         """Initial image, tile vid position, size and tile number for building."""
         self._set_images()
         self.pos = pos
+        self.gameboard = gameboard
         self.size = self.SIZE
         self.tile_no = self.TILE_NO
         self._buy_price = self.BUY_PRICE
@@ -133,7 +135,7 @@
     @classmethod
     def make(cls):
         """Override default Simplifiable object creation."""
-        return cls((0, 0))
+        return cls((0, 0), None)
 
     @classmethod
     def unsimplify(cls, *args, **kwargs):
@@ -208,18 +210,18 @@
         # Default is not to move
         return self.pos
 
-    def place(self, tv):
+    def place(self):
         """Check that the building can be placed at its current position
            and place it if possible.
            """
         # check that all spaces under the structure are grassland
         for tile_pos in self.tile_positions():
-            if not tv.get(tile_pos) == self.GRASSLAND:
+            if not self.gameboard.tv.get(tile_pos) == self.GRASSLAND:
                 return False
 
         # place tile
         for tile_pos in self.tile_positions():
-            tv.set(tile_pos, self.tile_no)
+            self.gameboard.tv.set(tile_pos, self.tile_no)
 
         return True
 
@@ -233,29 +235,29 @@
     def broken(self):
         return self._broken
 
-    def damage(self, tv):
+    def damage(self):
         if not self.BREAKABLE:
             return False
         self._broken = True
         self._sell_price = self.SELL_PRICE_BROKEN
         self.tile_no = self.TILE_NO_BROKEN
         for tile_pos in self.tile_positions():
-            tv.set(tile_pos, self.tile_no)
+            self.gameboard.tv.set(tile_pos, self.tile_no)
         self._set_main_image()
 
-    def repair(self, tv):
+    def repair(self):
         self._broken = False
         self._sell_price = self.SELL_PRICE
         self.tile_no = self.TILE_NO
         for tile_pos in self.tile_positions():
-            tv.set(tile_pos, self.tile_no)
+            self.gameboard.tv.set(tile_pos, self.tile_no)
         self._set_main_image()
 
-    def remove(self, tv):
+    def remove(self):
         """Remove the building from its current position."""
         # remove tile
         for tile_pos in self.tile_positions():
-            tv.set(tile_pos, self.GRASSLAND)
+            self.gameboard.tv.set(tile_pos, self.GRASSLAND)
 
     def buy_price(self):
         return self._buy_price
--- a/gamelib/gameboard.py	Sat Nov 28 18:04:28 2009 +0000
+++ b/gamelib/gameboard.py	Sat Nov 28 18:20:46 2009 +0000
@@ -755,12 +755,12 @@
         self.open_dialog(tbl, close_callback=close_callback)
 
     def buy_building(self, tile_pos, building_cls):
-        building = building_cls(tile_pos)
+        building = building_cls(tile_pos, self)
         if self.wood < building.buy_price():
             return
         if any(building.covers((chicken.pos.x, chicken.pos.y)) for chicken in self.chickens):
             return
-        if building.place(self.tv):
+        if building.place():
             self.add_wood(-building.buy_price())
             self.add_building(building)
 
@@ -802,7 +802,7 @@
             self.open_dialog(warning)
             return
         self.add_wood(building.sell_price())
-        building.remove(self.tv)
+        building.remove()
         self.remove_building(building)
 
     def repair_building(self, tile_pos):
@@ -812,7 +812,7 @@
         if self.wood < building.repair_price():
             return
         self.add_wood(-building.repair_price())
-        building.repair(self.tv)
+        building.repair()
 
     def sell_equipment(self, tile_pos):
         x, y = 0, 0
@@ -1150,9 +1150,9 @@
                     continue
 
                 building_cls = tile_to_building[tile_no]
-                building = building_cls(tile_pos)
-                building.remove(self.tv)
-                building.place(self.tv)
+                building = building_cls(tile_pos, self)
+                building.remove()
+                building.place()
                 self.add_building(building)
 
     def trees_left(self):
--- a/gamelib/version.py	Sat Nov 28 18:04:28 2009 +0000
+++ b/gamelib/version.py	Sat Nov 28 18:20:46 2009 +0000
@@ -9,7 +9,7 @@
 }[VERSION[3]]
 
 # incremement whenever a change breaks the save game file format
-SAVE_GAME_VERSION = 1
+SAVE_GAME_VERSION = 2
 
 NAME = 'Operation Fox Assault'
 DESCRIPTION = 'Turn-based strategy game written using Pygame.'