Changeset 108:437cbd856a03


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

Add occupants and abodes. Allowing moving chickens around.

Location:
gamelib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • gamelib/animal.py

    r104 r108  
    2121        self.pos = Position(tile_pos[0], tile_pos[1])
    2222        self.equipment = []
     23        self.abode = None
    2324
    2425    def loop(self, tv, _sprite):
     
    3132        # Default is not to move
    3233        pass
     34
     35    def set_pos(self, tile_pos):
     36        """Move an animal to the given tile_pos."""
     37        new_pos = Position(*tile_pos)
     38        self._fix_face(new_pos)
     39        self.pos = new_pos
    3340
    3441    def _fix_face(self, final_pos):
  • gamelib/buildings.py

    r65 r108  
    2020        self._buy_price = self.BUY_PRICE
    2121        self._sell_price = self.SELL_PRICE
     22        self._occupants = set()
    2223
    2324        # Create the building somewhere far off screen
     
    8586            self.setimage(self.night_image)
    8687
     88    def occupants(self):
     89        """Return list of buildings occupants."""
     90        return list(self._occupants)
     91
     92    def add_occupant(self, occupant):
     93        if occupant.abode is not None:
     94            occupant.abode.remove_occupant(occupant)
     95        occupant.abode = self
     96        self._occupants.add(occupant)
     97
     98    def remove_occupant(self, occupant):
     99        if occupant in self._occupants:
     100            self._occupants.remove(occupant)
     101            occupant.abode = None
    87102
    88103class HenHouse(Building):
  • gamelib/gameboard.py

    r105 r108  
    218218        chicken = self.get_chicken(tile_pos)
    219219        if chicken:
    220             self.animal_to_place = chicken
    221             print "Selected animal %r" % (chicken,)
     220            if chicken is self.animal_to_place:
     221                self.animal_to_place = None
     222            else:
     223                self.animal_to_place = chicken
     224            print "Selected animal %r" % (self.animal_to_place,)
    222225            return
    223226        building = self.get_building(tile_pos)
    224227        if building:
    225228            if self.animal_to_place is not None:
    226                 self.put_animal_in_building(self.animal_to_place, building)
     229                occupant = self.animal_to_place
     230                if occupant in self.tv.sprites:
     231                    self.tv.sprites.remove(occupant)
     232                building.add_occupant(occupant)
     233                print building, building.occupants()
    227234            else:
    228235                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)
     236            return
     237        if self.tv.get(tile_pos) == self.GRASSLAND:
     238            if self.animal_to_place is not None:
     239                occupant = self.animal_to_place
     240                if occupant.abode is not None:
     241                    occupant.abode.remove_occupant(occupant)
     242                occupant.set_pos(tile_pos)
    234243
    235244    def select_animal_from_building(self, building):
Note: See TracChangeset for help on using the changeset viewer.