changeset 65:7e9c8ad06d32

Implement building selling.
author Simon Cross <hodgestar@gmail.com>
date Mon, 31 Aug 2009 20:44:00 +0000
parents 99fbb652ce8d
children edc15ce8fa30
files gamelib/buildings.py gamelib/gameboard.py
diffstat 2 files changed, 36 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/buildings.py	Mon Aug 31 20:25:11 2009 +0000
+++ b/gamelib/buildings.py	Mon Aug 31 20:44:00 2009 +0000
@@ -18,6 +18,7 @@
         self.size = self.SIZE
         self.tile_no = self.TILE_NO
         self._buy_price = self.BUY_PRICE
+        self._sell_price = self.SELL_PRICE
 
         # Create the building somewhere far off screen
         Sprite.__init__(self, self.day_image, (-1000, -1000))
@@ -47,9 +48,6 @@
         """Check that the building can be placed at its current position
            and place it if possible.
            """
-        xpos, ypos = self.pos
-        xsize, ysize = self.size
-
         # check that all spaces under the structure are grassland
         for tile_pos in self.tile_positions():
             if not tv.get(tile_pos) == self.GRASSLAND:
@@ -61,9 +59,25 @@
 
         return True
 
+    def covers(self, tile_pos):
+        """Return True if build covers tile_pos, False otherwise."""
+        xpos, ypos = self.pos
+        xsize, ysize = self.size
+        return (xpos <= tile_pos[0] < xpos + xsize) and \
+            (ypos <= tile_pos[1] < ypos + ysize)
+
+    def remove(self, tv):
+        """Remove the building from its current position."""
+        # remove tile
+        for tile_pos in self.tile_positions():
+            tv.set(tile_pos, self.GRASSLAND)
+
     def buy_price(self):
         return self._buy_price
 
+    def sell_price(self):
+        return self._sell_price
+
     def sun(self, sun_on):
         if sun_on:
             self.setimage(self.day_image)
@@ -76,6 +90,7 @@
 
     TILE_NO = tiles.REVERSE_TILE_MAP['henhouse']
     BUY_PRICE = 100
+    SELL_PRICE = 90
     SIZE = (3, 2)
     IMAGE = 'sprites/henhouse.png'
     NAME = 'Hen House'
@@ -86,6 +101,7 @@
 
     TILE_NO = tiles.REVERSE_TILE_MAP['guardtower']
     BUY_PRICE = 200
+    SELL_PRICE = 150
     SIZE = (2, 2)
     IMAGE = 'sprites/watchtower.png'
     NAME = 'Watch Tower'
--- a/gamelib/gameboard.py	Mon Aug 31 20:25:11 2009 +0000
+++ b/gamelib/gameboard.py	Mon Aug 31 20:44:00 2009 +0000
@@ -123,11 +123,10 @@
             pass
         elif self.selected_tool == constants.TOOL_BUY_FENCE:
             self.buy_fence(self.tv.screen_to_tile(e.pos))
+        elif self.selected_tool == constants.TOOL_SELL_BUILDING:
+            self.sell_building(self.tv.screen_to_tile(e.pos))
         elif buildings.is_building(self.selected_tool):
-            building_cls = self.selected_tool
-            tile_pos = self.tv.screen_to_tile(e.pos)
-            building = building_cls(tile_pos)
-            self.buy_building(building)
+            self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool)
 
     def get_chicken(self, pos):
         for chick in self.chickens:
@@ -154,13 +153,21 @@
         self.add_cash(-constants.BUY_PRICE_FENCE)
         self.tv.set(tile_pos, tiles.REVERSE_TILE_MAP['fence'])
 
-    def buy_building(self, building):
+    def buy_building(self, tile_pos, building_cls):
+        building = building_cls(tile_pos)
         if self.cash < building.buy_price():
             return
         if building.place(self.tv):
             self.add_cash(-building.buy_price())
             self.add_building(building)
 
+    def sell_building(self, tile_pos):
+        for building in self.buildings:
+            if building.covers(tile_pos):
+                self.add_cash(building.sell_price())
+                building.remove(self.tv)
+                self.remove_building(building)
+
     def event(self, e):
         if e.type == KEYDOWN:
             if e.key == K_UP:
@@ -205,6 +212,11 @@
             self.chickens.remove(chick)
             self.tv.sprites.remove(chick)
 
+    def remove_building(self, building):
+        if building in self.buildings:
+            self.buildings.remove(building)
+            self.tv.sprites.remove(building)
+
     def add_cash(self, amount):
         self.cash += amount
         self.toolbar.update_cash_counter(self.cash)