source: gamelib/tiles.py @ 56:205789321a46

Last change on this file since 56:205789321a46 was 56:205789321a46, checked in by Simon Cross <hodgestar@…>, 11 years ago

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

File size: 2.6 KB
Line 
1"""Extension to pgu's tilevid."""
2
3from pgu import tilevid, vid
4import os
5import imagecache
6
7class TileMap(object):
8    """Helper class for describing all the game tiles."""
9
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
42
43class FarmVid(tilevid.Tilevid):
44    """Extension of pgu's TileVid class to handle the complications
45       of raising chickens.
46       """
47    def __init__(self):
48        tilevid.Tilevid.__init__(self)
49
50    def png_folder_load_tiles(self, path):
51        """Load tiles from a folder of PNG files."""
52        for dirpath, dirnames, filenames in os.walk(path):
53            abstract_dirpath = "/".join(dirpath.split(os.path.sep))
54            for filename in filenames:
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)
59
60    def sun(self, sun_on):
61        """Make it night."""
62        for tile in self.tiles:
63            if hasattr(tile, "sun"):
64                tile.sun(sun_on)
65        for sprite in self.sprites:
66            if hasattr(sprite, "sun"):
67                sprite.sun(sun_on)
68
69class FarmTile(vid.Tile):
70
71    def __init__(self, tile_no, tile_name, image_name):
72        self.tile_no = tile_no
73        self.tile_name = tile_name
74        self.day_image = imagecache.load_image(image_name)
75        self.night_image = imagecache.load_image(image_name, ("night",))
76        vid.Tile.__init__(self, self.day_image)
77
78    def sun(self, sun_on):
79        if sun_on:
80            self.image = self.day_image
81        else:
82            self.image = self.night_image
Note: See TracBrowser for help on using the repository browser.