comparison gamelib/gameboard.py @ 310:49c58dda8ac2

Refactor sell_callback, so sell_equipment work
author Neil Muller <drnlmuller@gmail.com>
date Sat, 05 Sep 2009 18:05:58 +0000
parents bf1df0902883
children ae5989b3ce01
comparison
equal deleted inserted replaced
309:cdfeef53d6f1 310:49c58dda8ac2
440 return building 440 return building
441 return None 441 return None
442 442
443 def sell_chicken(self, tile_pos): 443 def sell_chicken(self, tile_pos):
444 444
445 def do_sell(chicken): 445 def do_sell(chicken, update_button=None):
446 if not chicken: 446 if not chicken:
447 return False # sanity check 447 return False # sanity check
448 if len(self.chickens) == 1: 448 if len(self.chickens) == 1:
449 print "You can't sell your last chicken!" 449 print "You can't sell your last chicken!"
450 return False 450 return False
451 for item in list(chicken.equipment): 451 for item in list(chicken.equipment):
452 self.add_cash(item.sell_price()) 452 self.add_cash(item.sell_price())
453 chicken.unequip(item) 453 chicken.unequip(item)
454 self.add_cash(constants.SELL_PRICE_CHICKEN) 454 self.add_cash(constants.SELL_PRICE_CHICKEN)
455 sound.play_sound("sell-chicken.ogg") 455 sound.play_sound("sell-chicken.ogg")
456 if update_button:
457 update_button(chicken, empty=True)
458 self.remove_chicken(chicken)
456 return True 459 return True
457 460
458 chick = self.get_outside_chicken(tile_pos) 461 chick = self.get_outside_chicken(tile_pos)
459 if chick is None: 462 if chick is None:
460 building = self.get_building(tile_pos) 463 building = self.get_building(tile_pos)
461 if building and building.NAME in buildings.HENHOUSES: 464 if building and building.NAME in buildings.HENHOUSES:
462 self.open_building_dialog(building, do_sell) 465 self.open_building_dialog(building, do_sell)
463 return 466 return
464 467 do_sell(chick)
465 if do_sell(chick):
466 self.remove_chicken(chick)
467 468
468 def sell_one_egg(self, chicken): 469 def sell_one_egg(self, chicken):
469 if chicken.eggs: 470 if chicken.eggs:
470 self.add_cash(constants.SELL_PRICE_EGG) 471 self.add_cash(constants.SELL_PRICE_EGG)
471 chicken.remove_one_egg() 472 chicken.remove_one_egg()
473 self.toolbar.update_egg_counter(self.eggs) 474 self.toolbar.update_egg_counter(self.eggs)
474 return True 475 return True
475 return False 476 return False
476 477
477 def sell_egg(self, tile_pos): 478 def sell_egg(self, tile_pos):
478 def do_sell(chicken): 479 def do_sell(chicken, update_button=None):
479 # We try sell and egg 480 # We try sell and egg
480 if self.sell_one_egg(chicken): 481 if self.sell_one_egg(chicken):
481 sound.play_sound("sell-chicken.ogg") 482 sound.play_sound("sell-chicken.ogg")
482 # Force toolbar update 483 # Force toolbar update
483 self.toolbar.chsize() 484 self.toolbar.chsize()
485 if update_button:
486 update_button(chicken)
484 return False 487 return False
485 488
486 building = self.get_building(tile_pos) 489 building = self.get_building(tile_pos)
487 if building and building.NAME in buildings.HENHOUSES: 490 if building and building.NAME in buildings.HENHOUSES:
488 self.open_building_dialog(building, do_sell) 491 self.open_building_dialog(building, do_sell)
592 else: 595 else:
593 button.value = icons.animal_icon(animal) 596 button.value = icons.animal_icon(animal)
594 597
595 def nest_clicked(place, button): 598 def nest_clicked(place, button):
596 """Handle a nest being clicked.""" 599 """Handle a nest being clicked."""
597 # sell_callback should return true if we need to remove the
598 # occupant
599 if place.occupant: 600 if place.occupant:
600 # there is an occupant, select or sell it 601 # there is an occupant, select or sell it
601 if not sell_callback: 602 if not sell_callback:
602 old_animal = self.animal_to_place 603 old_animal = self.animal_to_place
603 self.select_animal_to_place(place.occupant) 604 self.select_animal_to_place(place.occupant)
605 update_button(old_animal) 606 update_button(old_animal)
606 # select new animal (on button) 607 # select new animal (on button)
607 update_button(self.animal_to_place) 608 update_button(self.animal_to_place)
608 else: 609 else:
609 # Attempt to sell the occupant 610 # Attempt to sell the occupant
610 if sell_callback(place.occupant): 611 sell_callback(place.occupant, update_button)
611 # empty the nest (on button)
612 update_button(place.occupant, empty=True)
613 self.remove_chicken(place.occupant)
614 else:
615 # Update for equipment changes, etc.
616 update_button(place.occupant)
617 else: 612 else:
618 # there is no occupant, attempt to fill the space 613 # there is no occupant, attempt to fill the space
619 if self.animal_to_place is not None: 614 if self.animal_to_place is not None:
620 # empty old nest (on button) 615 # empty old nest (on button)
621 update_button(self.animal_to_place, empty=True) 616 update_button(self.animal_to_place, empty=True)
711 706
712 def buy_equipment(self, tile_pos, equipment_cls): 707 def buy_equipment(self, tile_pos, equipment_cls):
713 708
714 equipment = equipment_cls() 709 equipment = equipment_cls()
715 710
716 def do_equip(chicken): 711 def do_equip(chicken, update_button=None):
717 # Try to equip the chicken 712 # Try to equip the chicken
718 if equipment.place(chicken): 713 if equipment.place(chicken):
719 self.add_cash(-equipment.buy_price()) 714 self.add_cash(-equipment.buy_price())
720 chicken.equip(equipment) 715 chicken.equip(equipment)
716 if update_button:
717 update_button(chicken)
721 return False 718 return False
722 719
723 chicken = self.get_outside_chicken(tile_pos) 720 chicken = self.get_outside_chicken(tile_pos)
724 if self.cash < equipment.buy_price(): 721 if self.cash < equipment.buy_price():
725 return 722 return
746 building.remove(self.tv) 743 building.remove(self.tv)
747 self.remove_building(building) 744 self.remove_building(building)
748 745
749 def sell_equipment(self, tile_pos): 746 def sell_equipment(self, tile_pos):
750 x, y = 0, 0 747 x, y = 0, 0
751 def do_sell(chicken): 748 def do_sell(chicken, update_button=None):
752 if not chicken.equipment: 749 if not chicken.equipment:
753 return 750 return
754 elif len(chicken.equipment) == 1: 751 elif len(chicken.equipment) == 1:
755 item = chicken.equipment[0] 752 item = chicken.equipment[0]
756 self.add_cash(item.sell_price()) 753 self.add_cash(item.sell_price())
757 chicken.unequip(item) 754 chicken.unequip(item)
755 if update_button:
756 update_button(chicken)
758 else: 757 else:
759 self.open_equipment_dialog(chicken, x, y) 758 self.open_equipment_dialog(chicken, x, y, update_button)
760 return False 759 return False
761 760
762 chicken = self.get_outside_chicken(tile_pos) 761 chicken = self.get_outside_chicken(tile_pos)
763 if chicken is not None: 762 if chicken is not None:
764 do_sell(chicken) 763 do_sell(chicken)
767 if building is None: 766 if building is None:
768 return 767 return
769 x, y = 50, 0 768 x, y = 50, 0
770 self.open_building_dialog(building, do_sell) 769 self.open_building_dialog(building, do_sell)
771 770
772 def open_equipment_dialog(self, chicken, x, y): 771 def open_equipment_dialog(self, chicken, x, y, update_button=None):
773 tbl = gui.Table() 772 tbl = gui.Table()
774 773
775 def sell_item(item, button): 774 def sell_item(item, button):
776 """Select item of equipment.""" 775 """Select item of equipment."""
777 self.add_cash(item.sell_price()) 776 self.add_cash(item.sell_price())
778 chicken.unequip(item) 777 chicken.unequip(item)
778 if update_button:
779 update_button(chicken)
779 self.disp.close(dialog) 780 self.disp.close(dialog)
780 781
781 kwargs = { 'style': { 'padding_left': 10, 'padding_bottom': 10 }} 782 kwargs = { 'style': { 'padding_left': 10, 'padding_bottom': 10 }}
782 783
783 tbl.tr() 784 tbl.tr()