Mercurial > rinkhals
comparison gamelib/gameboard.py @ 125:2e3a05b9594d
Chickens in buildings\!
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Wed, 02 Sep 2009 21:28:04 +0000 |
parents | 69fd96eafde8 |
children | d98654287317 |
comparison
equal
deleted
inserted
replaced
124:69fd96eafde8 | 125:2e3a05b9594d |
---|---|
231 if chicken: | 231 if chicken: |
232 if chicken is self.animal_to_place: | 232 if chicken is self.animal_to_place: |
233 self.animal_to_place = None | 233 self.animal_to_place = None |
234 else: | 234 else: |
235 self.animal_to_place = chicken | 235 self.animal_to_place = chicken |
236 print "Selected animal %r" % (self.animal_to_place,) | |
237 return | 236 return |
238 building = self.get_building(tile_pos) | 237 building = self.get_building(tile_pos) |
239 if building: | 238 if building: |
240 # XXX: quick hack so egg loop triggers | 239 self.open_building_dialog(building) |
241 if self.animal_to_place: | |
242 building.add_occupant(self.animal_to_place) | |
243 if self.animal_to_place in self.tv.sprites: | |
244 self.tv.sprites.remove(self.animal_to_place) | |
245 self.animal_to_place = None | |
246 self.open_building_dialog(building) | |
247 return | 240 return |
248 if self.tv.get(tile_pos) == self.GRASSLAND: | 241 if self.tv.get(tile_pos) == self.GRASSLAND: |
249 if self.animal_to_place is not None: | 242 if self.animal_to_place is not None: |
250 occupant = self.animal_to_place | 243 occupant = self.animal_to_place |
251 if occupant.abode is not None: | 244 if occupant.abode is not None: |
252 occupant.abode.remove_occupant(occupant) | 245 occupant.abode.clear_occupant() |
253 occupant.set_pos(tile_pos) | 246 occupant.set_pos(tile_pos) |
247 self.set_visibility(occupant) | |
248 | |
249 def set_visibility(self, chicken): | |
250 if chicken.outside(): | |
251 if chicken not in self.tv.sprites: | |
252 self.tv.sprites.append(chicken) | |
253 else: | |
254 if chicken in self.tv.sprites: | |
255 self.tv.sprites.remove(chicken) | |
254 | 256 |
255 def open_dialog(self, widget): | 257 def open_dialog(self, widget): |
256 """Open a dialog for the given widget. Add close button.""" | 258 """Open a dialog for the given widget. Add close button.""" |
257 tbl = gui.Table() | 259 tbl = gui.Table() |
258 | 260 |
271 | 273 |
272 self.disp.open(tbl) | 274 self.disp.open(tbl) |
273 | 275 |
274 def open_building_dialog(self, building): | 276 def open_building_dialog(self, building): |
275 """Create dialog for manipulating the contents of a building.""" | 277 """Create dialog for manipulating the contents of a building.""" |
278 def select_occupant(place, button): | |
279 """Select occupant in place.""" | |
280 self.animal_to_place = place.occupant | |
281 | |
282 def set_occupant(place, button): | |
283 """Set occupant of a given place.""" | |
284 if self.animal_to_place is not None: | |
285 button.value = icons.CHKN_NEST_ICON | |
286 button.disconnect(gui.CLICK, set_occupant) | |
287 button.connect(gui.CLICK, select_occupant, place, button) | |
288 | |
289 old_abode = self.animal_to_place.abode | |
290 if id(old_abode) in place_button_map: | |
291 old_button = place_button_map[id(old_abode)] | |
292 old_button.value = icons.EMPTY_NEST_ICON | |
293 old_button.disconnect(gui.CLICK, select_occupant) | |
294 old_button.connect(gui.CLICK, set_occupant, place, button) | |
295 | |
296 chicken = self.animal_to_place | |
297 place.set_occupant(chicken) | |
298 chicken.set_pos(place.get_pos()) | |
299 self.set_visibility(self.animal_to_place) | |
300 | |
301 place_button_map = {} | |
302 | |
276 width, height = pygame.display.get_surface().get_size() | 303 width, height = pygame.display.get_surface().get_size() |
277 tbl = gui.Table() | 304 tbl = gui.Table() |
278 for row in range(building.size[0]): | 305 columns = building.max_floor_width() |
306 kwargs = { 'style': { 'padding_left': 10, 'padding_bottom': 10 }} | |
307 for floor in building.floors(): | |
279 tbl.tr() | 308 tbl.tr() |
280 for col in range(building.size[1]): | 309 tbl.td(gui.Button(floor.title), colspan=columns, align=-1, **kwargs) |
281 button = gui.Button("%s, %s" % (row, col)) | 310 tbl.tr() |
282 tbl.td(button) | 311 for row in floor.rows(): |
312 tbl.tr() | |
313 for place in row: | |
314 if place.occupant is None: | |
315 button = gui.Button(icons.EMPTY_NEST_ICON) | |
316 button.connect(gui.CLICK, set_occupant, place, button) | |
317 else: | |
318 button = gui.Button(icons.CHKN_NEST_ICON) | |
319 button.connect(gui.CLICK, select_occupant, place, button) | |
320 place_button_map[id(place)] = button | |
321 tbl.td(button, **kwargs) | |
283 | 322 |
284 self.open_dialog(tbl) | 323 self.open_dialog(tbl) |
285 | 324 |
286 def buy_fence(self, tile_pos): | 325 def buy_fence(self, tile_pos): |
287 this_tile = self.tv.get(tile_pos) | 326 this_tile = self.tv.get(tile_pos) |
399 if building.NAME in [buildings.HenHouse.NAME]: | 438 if building.NAME in [buildings.HenHouse.NAME]: |
400 for chicken in building.occupants(): | 439 for chicken in building.occupants(): |
401 new_chick = chicken.hatch() | 440 new_chick = chicken.hatch() |
402 if new_chick: | 441 if new_chick: |
403 self.eggs -= 1 | 442 self.eggs -= 1 |
404 building.add_occupant(new_chick) | 443 try: |
405 self.add_chicken(new_chick) | 444 building.add_occupant(new_chick) |
445 self.add_chicken(new_chick) | |
446 except buildings.BuildingFullError: | |
447 print "Building full." | |
406 self.toolbar.update_egg_counter(self.eggs) | 448 self.toolbar.update_egg_counter(self.eggs) |
407 | 449 |
408 def kill_fox(self, fox): | 450 def kill_fox(self, fox): |
409 if fox in self.foxes: | 451 if fox in self.foxes: |
410 self.killed_foxes += 1 | 452 self.killed_foxes += 1 |
421 self.chickens.discard(chick) | 463 self.chickens.discard(chick) |
422 if chick.egg: | 464 if chick.egg: |
423 self.eggs -= 1 | 465 self.eggs -= 1 |
424 self.toolbar.update_egg_counter(self.eggs) | 466 self.toolbar.update_egg_counter(self.eggs) |
425 if chick.abode: | 467 if chick.abode: |
426 chick.abode.remove_occupant(chick) | 468 chick.abode.clear_occupant() |
427 self.toolbar.update_chicken_counter(len(self.chickens)) | 469 self.toolbar.update_chicken_counter(len(self.chickens)) |
428 if chick in self.tv.sprites: | 470 if chick in self.tv.sprites: |
429 if chick.outside(): | 471 if chick.outside(): |
430 self.tv.sprites.remove(chick) | 472 self.tv.sprites.remove(chick) |
431 | 473 |