Mercurial > rinkhals
comparison gamelib/tiles.py @ 379:a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 24 Oct 2009 21:43:15 +0000 |
parents | e12d99215b74 |
children | 7a58dadfd251 |
comparison
equal
deleted
inserted
replaced
378:71f5897ac5ef | 379:a8a7ada27fa2 |
---|---|
40 yield n | 40 yield n |
41 | 41 |
42 TILE_MAP = TileMap() | 42 TILE_MAP = TileMap() |
43 REVERSE_TILE_MAP = TILE_MAP._reverse_map | 43 REVERSE_TILE_MAP = TILE_MAP._reverse_map |
44 | 44 |
45 class FarmSprites(list): | 45 |
46 def __init__(self): | 46 class LayeredSprites(object): |
47 list.__init__(self) | 47 # This is as small a subset of list behaviour as we can get away with. |
48 # WARNING: It makes assumptions that may not be justified in the general case. | |
49 # Caveat utilitor and all the rest. | |
50 | |
51 def __init__(self, layers=None, default_layer=None): | |
52 # Layers are ordered from bottom to top. | |
53 if layers is None: | |
54 layers = ['default'] | |
55 self.layers = layers | |
56 if default_layer is None: | |
57 default_layer = layers[0] | |
58 self.default_layer = default_layer | |
59 self._sprites = {} | |
60 for layer in layers: | |
61 self._sprites[layer] = [] | |
48 self.removed = [] | 62 self.removed = [] |
49 self._cursor = None | |
50 | 63 |
51 def append(self, sprite): | 64 def append(self, sprite, layer=None): |
52 if self._cursor is not None: | 65 if layer is None: |
53 assert(self._cursor is self[-1]) | 66 layer = self.default_layer |
54 list.insert(self, -1, sprite) | 67 self._sprites[layer].append(sprite) |
55 else: | |
56 list.append(self, sprite) | |
57 sprite.updated = 1 | 68 sprite.updated = 1 |
58 | 69 |
59 def remove(self, sprite): | 70 def remove(self, sprite, layer=None): |
60 list.remove(self, sprite) | 71 if layer is None: |
72 layer = self.default_layer | |
73 self._sprites[layer].remove(sprite) | |
61 sprite.updated = 1 | 74 sprite.updated = 1 |
62 self.removed.append(sprite) | 75 self.removed.append(sprite) |
63 | 76 |
64 def set_cursor(self, cursor): | 77 def __getitem__(self, key): |
65 if self._cursor is not None: | 78 # vid.py uses sprites[:] to get a mutation-safe list copy, so we need this. |
66 self.remove(self._cursor) | 79 # No other indexing or slicing operations make sense. |
67 self._cursor = cursor | 80 if not isinstance(key, slice): |
68 if cursor is not None: | 81 raise IndexError('[:] is the only supported slice/index operation.') |
69 list.append(self, cursor) | 82 return iter(self) |
83 | |
84 def __iter__(self): | |
85 # We iterate over all sprites in layer order. | |
86 for layer in self.layers: | |
87 for sprite in self._sprites[layer]: | |
88 yield sprite | |
70 | 89 |
71 | 90 |
72 class FarmVid(tilevid.Tilevid): | 91 class FarmVid(tilevid.Tilevid): |
73 """Extension of pgu's TileVid class to handle the complications | 92 """Extension of pgu's TileVid class to handle the complications |
74 of raising chickens. | 93 of raising chickens. |
75 """ | 94 """ |
76 def __init__(self): | 95 def __init__(self): |
77 tilevid.Tilevid.__init__(self) | 96 tilevid.Tilevid.__init__(self) |
78 self.sprites = FarmSprites() | 97 self.sprites = LayeredSprites(['animals', 'buildings', 'animations', 'cursor'], 'animals') |
79 | 98 |
80 def png_folder_load_tiles(self, path): | 99 def png_folder_load_tiles(self, path): |
81 """Load tiles from a folder of PNG files.""" | 100 """Load tiles from a folder of PNG files.""" |
82 full_path = data.filepath(path) | 101 full_path = data.filepath(path) |
83 for dirpath, dirnames, filenames in os.walk(full_path): | 102 for dirpath, dirnames, filenames in os.walk(full_path): |