Changeset 56:205789321a46


Ignore:
Timestamp:
Aug 31, 2009, 6:33:33 PM (11 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@57
Message:

Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gamelib/tiles.py

    r51 r56  
    55import imagecache
    66
    7 TILE_MAP = {
    8     0: "woodland",
    9     1: "grassland",
    10     2: "fence",
    11     3: "henhouse",
    12     4: "chicken",
    13 }
     7class TileMap(object):
     8    """Helper class for describing all the game tiles."""
    149
    15 REVERSE_TILE_MAP = dict((v, k) for k, v in TILE_MAP.iteritems())
     10    # number -> (name, image file name)
     11    DEFAULT_TILES = {
     12        0: ("woodland", "woodland.png"),
     13        1: ("grassland", "grassland.png"),
     14        2: ("fence", "fence.png"),
     15        3: ("henhouse", "grassland.png"),
     16        4: ("guardtower", "guardtower.png"),
     17    }
     18
     19    def __init__(self, tiles=None):
     20        if tiles is None:
     21            tiles = self.DEFAULT_TILES.copy()
     22        self._tiles = tiles
     23        self._reverse_map = dict((v[0], k) for k, v in self._tiles.iteritems())
     24
     25    def __getitem__(self, n):
     26        """Get the string name of tile n."""
     27        return self._tiles[n][0]
     28
     29    def tile_number(self, name):
     30        """Return the tile number of the tile with the given name."""
     31        return self._reverse_map[name]
     32
     33    def tiles_for_image(self, image_name):
     34        """Return tile numbers associated with the given image name."""
     35        for n, (name, image) in self._tiles.iteritems():
     36            if image_name == image:
     37                yield n
     38
     39TILE_MAP = TileMap()
     40REVERSE_TILE_MAP = TILE_MAP._reverse_map
     41
    1642
    1743class FarmVid(tilevid.Tilevid):
     
    2753            abstract_dirpath = "/".join(dirpath.split(os.path.sep))
    2854            for filename in filenames:
    29                 basename, ext = os.path.splitext(filename)
    30                 if ext != ".png":
    31                     continue
    32                 if basename in REVERSE_TILE_MAP:
    33                     n = REVERSE_TILE_MAP[basename]
    34                     self.tiles[n] = FarmTile(abstract_dirpath + "/" + filename)
     55                image_name = abstract_dirpath + "/" + filename
     56                for tile_no in TILE_MAP.tiles_for_image(filename):
     57                    tile_name = TILE_MAP[tile_no]
     58                    self.tiles[tile_no] = FarmTile(tile_no, tile_name, image_name)
    3559
    3660    def sun(self, sun_on):
     
    4569class FarmTile(vid.Tile):
    4670
    47     def __init__(self, image_name):
     71    def __init__(self, tile_no, tile_name, image_name):
     72        self.tile_no = tile_no
     73        self.tile_name = tile_name
    4874        self.day_image = imagecache.load_image(image_name)
    4975        self.night_image = imagecache.load_image(image_name, ("night",))
Note: See TracChangeset for help on using the changeset viewer.