Changeset 192:a490ee2ef446


Ignore:
Timestamp:
Sep 4, 2009, 6:17:55 PM (11 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@193
Message:

Allow selling of chickens in henhouses

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gamelib/gameboard.py

    r191 r192  
    255255            self.sell_chicken(self.tv.screen_to_tile(e.pos))
    256256        elif self.selected_tool == constants.TOOL_SELL_EGG:
    257             pass
     257            self.sell_egg(self.tv.screen_to_tile(e.pos))
    258258        elif self.selected_tool == constants.TOOL_PLACE_ANIMALS:
    259259            self.place_animal(self.tv.screen_to_tile(e.pos))
     
    284284
    285285    def sell_chicken(self, tile_pos):
     286
     287        def do_sell(chicken):
     288            if not chicken:
     289                return False # sanity check
     290            if len(self.chickens) == 1:
     291                print "You can't sell your last chicken!"
     292                return False
     293            self.add_cash(constants.SELL_PRICE_CHICKEN)
     294            sound.play_sound("sell-chicken.ogg")
     295            self.remove_chicken(chicken)
     296            return True
     297
    286298        chick = self.get_outside_chicken(tile_pos)
    287299        if chick is None:
    288             return
    289         if len(self.chickens) == 1:
    290             print "You can't sell your last chicken!"
    291             return
    292         self.add_cash(constants.SELL_PRICE_CHICKEN)
    293         sound.play_sound("sell-chicken.ogg")
    294         self.remove_chicken(chick)
     300            building = self.get_building(tile_pos)
     301            if building and building.NAME in buildings.HENHOUSES:
     302                self.open_building_dialog(building, do_sell)
     303            return
     304        do_sell(chick)
     305
     306
     307    def sell_egg(self, tile_pos):
     308        def do_sell(chicken):
     309            # Placeholde
     310            return False
     311        building = self.get_building(tile_pos)
     312        if building and building.NAME in buildings.HENHOUSES:
     313            self.open_building_dialog(building)
    295314
    296315    def place_animal(self, tile_pos):
     
    351370        return tbl
    352371
    353     def open_building_dialog(self, building):
     372    def open_building_dialog(self, building, sell_callback=None):
    354373        """Create dialog for manipulating the contents of a building."""
    355         def select_occupant(place, button):
     374        def select_occupant(place, button, sell_callback):
    356375            """Select occupant in place."""
     376            # sell_callback should return true if we need to remove the
     377            # occupant
    357378            self.animal_to_place = place.occupant
    358             pygame.mouse.set_cursor(*cursors.cursors['chicken'])
    359 
    360         def set_occupant(place, button):
     379            if not sell_callback:
     380                pygame.mouse.set_cursor(*cursors.cursors['chicken'])
     381            else:
     382                # Attempt to sell the occupant
     383                self.animal_to_place = None
     384                if sell_callback(place.occupant):
     385                    button.value = icons.EMPTY_NEST_ICON
     386                    button.disconnect(gui.CLICK, select_occupant)
     387                    button.connect(gui.CLICK, set_occupant, place, button,
     388                            sell_callback)
     389
     390        def set_occupant(place, button, sell_callback):
    361391            """Set occupant of a given place."""
    362392            if self.animal_to_place is not None:
    363393                button.value = icons.CHKN_NEST_ICON
    364394                button.disconnect(gui.CLICK, set_occupant)
    365                 button.connect(gui.CLICK, select_occupant, place, button)
     395                button.connect(gui.CLICK, select_occupant, place, button,
     396                        sell_callback)
    366397
    367398                old_abode = self.animal_to_place.abode
     
    372403                    old_button.value = icons.EMPTY_NEST_ICON
    373404                    old_button.disconnect(gui.CLICK, select_occupant)
    374                     old_button.connect(gui.CLICK, set_occupant, place, button)
     405                    old_button.connect(gui.CLICK, set_occupant, place, button,
     406                            sell_callback)
    375407
    376408                chicken = self.animal_to_place
     
    393425                    if place.occupant is None:
    394426                        button = gui.Button(icons.EMPTY_NEST_ICON)
    395                         button.connect(gui.CLICK, set_occupant, place, button)
     427                        button.connect(gui.CLICK, set_occupant, place, button,
     428                                sell_callback)
    396429                    else:
    397430                        button = gui.Button(icons.CHKN_NEST_ICON)
    398                         button.connect(gui.CLICK, select_occupant, place, button)
     431                        button.connect(gui.CLICK, select_occupant, place, button,
     432                                sell_callback)
    399433                    place_button_map[id(place)] = button
    400434                    tbl.td(button, **kwargs)
Note: See TracChangeset for help on using the changeset viewer.