Mercurial > rinkhals
comparison gamelib/tiles.py @ 558:5cdf26bde2f2
Botanical biodiversity.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 28 Nov 2009 18:20:55 +0000 |
parents | 11c4cebfe4c5 |
children |
comparison
equal
deleted
inserted
replaced
557:50d6c68ce267 | 558:5cdf26bde2f2 |
---|---|
1 """Extension to pgu's tilevid.""" | 1 """Extension to pgu's tilevid.""" |
2 | 2 |
3 import random | |
4 import os | |
5 | |
3 from pgu import tilevid, vid | 6 from pgu import tilevid, vid |
4 import os | |
5 import data | 7 import data |
6 import imagecache | 8 import imagecache |
7 import serializer | 9 import serializer |
8 | 10 |
9 class TileMap(object): | 11 class TileMap(object): |
16 2: ("fence", "fence.png"), | 18 2: ("fence", "fence.png"), |
17 3: ("henhouse", "grassland.png"), | 19 3: ("henhouse", "grassland.png"), |
18 4: ("guardtower", "grassland.png"), | 20 4: ("guardtower", "grassland.png"), |
19 5: ("broken fence", "broken_fence.png"), | 21 5: ("broken fence", "broken_fence.png"), |
20 6: ("hendominium", "grassland.png"), | 22 6: ("hendominium", "grassland.png"), |
23 } | |
21 | 24 |
25 ALTERNATE_TILES = { | |
22 255: ("woodland", "woodland.png"), | 26 255: ("woodland", "woodland.png"), |
23 254: ("woodland", "woodland2.png"), | 27 254: ("woodland", "woodland2.png"), |
24 253: ("woodland", "woodland3.png"), | 28 253: ("woodland", "woodland3.png"), |
25 } | 29 } |
26 | 30 |
27 def __init__(self, tiles=None): | 31 def __init__(self, tiles=None, alternate_tiles=None): |
28 if tiles is None: | 32 if tiles is None: |
29 tiles = self.DEFAULT_TILES.copy() | 33 tiles = self.DEFAULT_TILES.copy() |
34 if alternate_tiles is None: | |
35 alternate_tiles = self.ALTERNATE_TILES.copy() | |
36 self._alternate_tiles = alternate_tiles | |
30 self._tiles = tiles | 37 self._tiles = tiles |
31 self._reverse_map = dict((v[0], k) for k, v in self._tiles.iteritems()) | 38 self._reverse_map = dict((v[0], k) for k, v in self._tiles.iteritems()) |
32 # Wood is different: | 39 self._tiles.update(alternate_tiles) |
33 self._reverse_map["woodland"] = 0 | |
34 | 40 |
35 def __getitem__(self, n): | 41 def __getitem__(self, n): |
36 """Get the string name of tile n.""" | 42 """Get the string name of tile n.""" |
37 return self._tiles[n][0] | 43 return self._tiles[n][0] |
38 | |
39 def tile_number(self, name): | |
40 """Return the tile number of the tile with the given name.""" | |
41 return self._reverse_map[name] | |
42 | 44 |
43 def tiles_for_image(self, image_name): | 45 def tiles_for_image(self, image_name): |
44 """Return tile numbers associated with the given image name.""" | 46 """Return tile numbers associated with the given image name.""" |
45 for n, (name, image) in self._tiles.iteritems(): | 47 for n, (name, image) in self._tiles.iteritems(): |
46 if image_name == image: | 48 if image_name == image: |
47 yield n | 49 yield n |
50 | |
51 def random_alternate(self, n): | |
52 alts = [a for a, (name, image) in self._alternate_tiles.iteritems() if name == self[n]] | |
53 if alts: | |
54 return random.choice(alts) | |
55 return n | |
48 | 56 |
49 TILE_MAP = TileMap() | 57 TILE_MAP = TileMap() |
50 REVERSE_TILE_MAP = TILE_MAP._reverse_map | 58 REVERSE_TILE_MAP = TILE_MAP._reverse_map |
51 | 59 |
52 | 60 |
128 obj.bounds = None | 136 obj.bounds = None |
129 obj.updates = [] | 137 obj.updates = [] |
130 | 138 |
131 return obj | 139 return obj |
132 | 140 |
141 def tga_load_level(self, level_map): | |
142 tilevid.Tilevid.tga_load_level(self, level_map) | |
143 for xy in [(x, y) for x in range(self.size[0]) for y in range(self.size[1])]: | |
144 self.set(xy, TILE_MAP.random_alternate(self.get(xy))) | |
145 | |
133 def png_folder_load_tiles(self, path): | 146 def png_folder_load_tiles(self, path): |
134 """Load tiles from a folder of PNG files.""" | 147 """Load tiles from a folder of PNG files.""" |
135 full_path = data.filepath(path) | 148 full_path = data.filepath(path) |
136 for dirpath, dirnames, filenames in os.walk(full_path): | 149 for dirpath, dirnames, filenames in os.walk(full_path): |
137 relative_path = dirpath[len(full_path):] | 150 relative_path = dirpath[len(full_path):] |