comparison gamelib/gameboard.py @ 67:9171d9b9ab35

Sanity check and fix up buildings on map after loading.
author Simon Cross <hodgestar@gmail.com>
date Mon, 31 Aug 2009 21:22:10 +0000
parents edc15ce8fa30
children 6b8ac424da83
comparison
equal deleted inserted replaced
66:edc15ce8fa30 67:9171d9b9ab35
93 self.foxes = [] 93 self.foxes = []
94 self.buildings = [] 94 self.buildings = []
95 self.cash = 0 95 self.cash = 0
96 self.add_cash(constants.STARTING_CASH) 96 self.add_cash(constants.STARTING_CASH)
97 97
98 self.fix_buildings()
99
98 def create_disp(self): 100 def create_disp(self):
99 width, height = pygame.display.get_surface().get_size() 101 width, height = pygame.display.get_surface().get_size()
100 tbl = gui.Table() 102 tbl = gui.Table()
101 tbl.tr() 103 tbl.tr()
102 self.toolbar = ToolBar(self) 104 self.toolbar = ToolBar(self)
230 self.tv.sprites.remove(building) 232 self.tv.sprites.remove(building)
231 233
232 def add_cash(self, amount): 234 def add_cash(self, amount):
233 self.cash += amount 235 self.cash += amount
234 self.toolbar.update_cash_counter(self.cash) 236 self.toolbar.update_cash_counter(self.cash)
237
238 def fix_buildings(self):
239 """Go through the level map looking for buildings that haven't
240 been added to self.buildings and adding them.
241
242 Where partial buildings exist (i.e. places where the building
243 cannot fit on the available tiles) the building is added anyway
244 to the top left corner.
245
246 Could be a lot faster.
247 """
248 tile_to_building = dict((b.TILE_NO, b) for b in buildings.BUILDINGS)
249 print tile_to_building
250
251 w, h = self.tv.size
252 for x in xrange(w):
253 for y in xrange(h):
254 tile_pos = (x, y)
255 tile_no = self.tv.get(tile_pos)
256 if tile_no not in tile_to_building:
257 continue
258
259 covered = False
260 for building in self.buildings:
261 if building.covers(tile_pos):
262 covered = True
263 break
264
265 if covered:
266 continue
267
268 building_cls = tile_to_building[tile_no]
269 building = building_cls(tile_pos)
270 building.remove(self.tv)
271 building.place(self.tv)
272 self.add_building(building)