Mercurial > rinkhals
comparison gamelib/tiles.py @ 56:205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Mon, 31 Aug 2009 18:33:33 +0000 |
parents | 32fb395cf71c |
children | 98ce098f8cbb |
comparison
equal
deleted
inserted
replaced
55:3a409e058608 | 56:205789321a46 |
---|---|
2 | 2 |
3 from pgu import tilevid, vid | 3 from pgu import tilevid, vid |
4 import os | 4 import os |
5 import imagecache | 5 import imagecache |
6 | 6 |
7 TILE_MAP = { | 7 class TileMap(object): |
8 0: "woodland", | 8 """Helper class for describing all the game tiles.""" |
9 1: "grassland", | |
10 2: "fence", | |
11 3: "henhouse", | |
12 4: "chicken", | |
13 } | |
14 | 9 |
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 | |
39 TILE_MAP = TileMap() | |
40 REVERSE_TILE_MAP = TILE_MAP._reverse_map | |
41 | |
16 | 42 |
17 class FarmVid(tilevid.Tilevid): | 43 class FarmVid(tilevid.Tilevid): |
18 """Extension of pgu's TileVid class to handle the complications | 44 """Extension of pgu's TileVid class to handle the complications |
19 of raising chickens. | 45 of raising chickens. |
20 """ | 46 """ |
24 def png_folder_load_tiles(self, path): | 50 def png_folder_load_tiles(self, path): |
25 """Load tiles from a folder of PNG files.""" | 51 """Load tiles from a folder of PNG files.""" |
26 for dirpath, dirnames, filenames in os.walk(path): | 52 for dirpath, dirnames, filenames in os.walk(path): |
27 abstract_dirpath = "/".join(dirpath.split(os.path.sep)) | 53 abstract_dirpath = "/".join(dirpath.split(os.path.sep)) |
28 for filename in filenames: | 54 for filename in filenames: |
29 basename, ext = os.path.splitext(filename) | 55 image_name = abstract_dirpath + "/" + filename |
30 if ext != ".png": | 56 for tile_no in TILE_MAP.tiles_for_image(filename): |
31 continue | 57 tile_name = TILE_MAP[tile_no] |
32 if basename in REVERSE_TILE_MAP: | 58 self.tiles[tile_no] = FarmTile(tile_no, tile_name, image_name) |
33 n = REVERSE_TILE_MAP[basename] | |
34 self.tiles[n] = FarmTile(abstract_dirpath + "/" + filename) | |
35 | 59 |
36 def sun(self, sun_on): | 60 def sun(self, sun_on): |
37 """Make it night.""" | 61 """Make it night.""" |
38 for tile in self.tiles: | 62 for tile in self.tiles: |
39 if hasattr(tile, "sun"): | 63 if hasattr(tile, "sun"): |
42 if hasattr(sprite, "sun"): | 66 if hasattr(sprite, "sun"): |
43 sprite.sun(sun_on) | 67 sprite.sun(sun_on) |
44 | 68 |
45 class FarmTile(vid.Tile): | 69 class FarmTile(vid.Tile): |
46 | 70 |
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 | |
48 self.day_image = imagecache.load_image(image_name) | 74 self.day_image = imagecache.load_image(image_name) |
49 self.night_image = imagecache.load_image(image_name, ("night",)) | 75 self.night_image = imagecache.load_image(image_name, ("night",)) |
50 vid.Tile.__init__(self, self.day_image) | 76 vid.Tile.__init__(self, self.day_image) |
51 | 77 |
52 def sun(self, sun_on): | 78 def sun(self, sun_on): |