Changeset 105:7910b4e01dba


Ignore:
Timestamp:
Sep 2, 2009, 6:10:52 PM (11 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@106
Message:

Add chicken moving tool and start of animal placement.

Location:
gamelib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • gamelib/constants.py

    r90 r105  
    3838TOOL_SELL_BUILDING = 3
    3939TOOL_BUY_FENCE = 4
     40TOOL_PLACE_ANIMALS = 5
  • gamelib/gameboard.py

    r98 r105  
    3737        self.add_counter(icons.KILLED_FOX, self.killed_foxes)
    3838
     39        self.add_tool_button("Move Animals", constants.TOOL_PLACE_ANIMALS)
    3940        self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN)
    4041        self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG)
     
    128129
    129130        self.selected_tool = None
     131        self.animal_to_place = None
    130132        self.chickens = []
    131133        self.foxes = []
     
    160162    def set_selected_tool(self, tool):
    161163        self.selected_tool = tool
     164        self.animal_to_place = None
    162165
    163166    def in_bounds(self, pos):
     
    172175    def use_tool(self, e):
    173176        if self.selected_tool == constants.TOOL_SELL_CHICKEN:
    174             self.sell_chicken(e.pos)
     177            self.sell_chicken(self.tv.screen_to_tile(e.pos))
    175178        elif self.selected_tool == constants.TOOL_SELL_EGG:
    176179            pass
     180        elif self.selected_tool == constants.TOOL_PLACE_ANIMALS:
     181            self.place_animal(self.tv.screen_to_tile(e.pos))
    177182        elif self.selected_tool == constants.TOOL_BUY_FENCE:
    178183            self.buy_fence(self.tv.screen_to_tile(e.pos))
     
    182187            self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool)
    183188
    184     def get_chicken(self, pos):
     189    def get_chicken(self, tile_pos):
    185190        for chick in self.chickens:
    186             if chick.rect.collidepoint(pos):
     191            if chick.covers(tile_pos):
    187192                return chick
    188193        return None
    189194
    190     def sell_chicken(self, pos):
    191         chick = self.get_chicken(pos)
     195    def get_building(self, tile_pos):
     196        for building in self.buildings:
     197            if building.covers(tile_pos):
     198                return building
     199        return None
     200
     201    def sell_chicken(self, tile_pos):
     202        chick = self.get_chicken(tile_pos)
    192203        if chick is None:
    193204            return
     
    198209        sound.play_sound("sell-chicken.ogg")
    199210        self.remove_chicken(chick)
     211
     212    def place_animal(self, tile_pos):
     213        """Handle an TOOL_PLACE_ANIMALS click.
     214
     215           This will either select an animal or
     216           place a selected animal in a building.
     217           """
     218        chicken = self.get_chicken(tile_pos)
     219        if chicken:
     220            self.animal_to_place = chicken
     221            print "Selected animal %r" % (chicken,)
     222            return
     223        building = self.get_building(tile_pos)
     224        if building:
     225            if self.animal_to_place is not None:
     226                self.put_animal_in_building(self.animal_to_place, building)
     227            else:
     228                self.select_animal_from_building(building)
     229
     230    def put_animal_in_building(self, animal, building):
     231        """Place animal in building."""
     232        # XXX: unimplemented
     233        print "Placing %r in %r" % (animal, building)
     234
     235    def select_animal_from_building(self, building):
     236        """Create dialog for selecting an animal from a building."""
     237        # XXX: unimplemented
     238        print "Selecting animal from building %r" % (building,)
    200239
    201240    def buy_fence(self, tile_pos):
     
    230269        if self.tv.get(tile_pos) == self.FENCE:
    231270            return self.sell_fence(tile_pos)
    232         for building in self.buildings:
    233             if building.covers(tile_pos):
    234                 self.add_cash(building.sell_price())
    235                 building.remove(self.tv)
    236                 self.remove_building(building)
    237                 break
     271        building = self.get_building(tile_pos)
     272        if building is None:
     273            return
     274        self.add_cash(building.sell_price())
     275        building.remove(self.tv)
     276        self.remove_building(building)
    238277
    239278    def event(self, e):
Note: See TracChangeset for help on using the changeset viewer.