Mercurial > rinkhals
diff gamelib/gameboard.py @ 65:7e9c8ad06d32
Implement building selling.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Mon, 31 Aug 2009 20:44:00 +0000 |
parents | 99fbb652ce8d |
children | edc15ce8fa30 |
line wrap: on
line diff
--- 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)