Mercurial > rinkhals
annotate gamelib/tiles.py @ 449:938498b8cd03
Fix toolbar alignment
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 21 Nov 2009 20:11:08 +0000 |
parents | 7a58dadfd251 |
children | e3408c803b12 |
rev | line source |
---|---|
24 | 1 """Extension to pgu's tilevid.""" |
2 | |
3 from pgu import tilevid, vid | |
4 import os | |
303
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
285
diff
changeset
|
5 import data |
49
dd9d2a4dd494
Update tile loading to use imagecache and new data.py auto-converting of paths.
Simon Cross <hodgestar@gmail.com>
parents:
30
diff
changeset
|
6 import imagecache |
24 | 7 |
56
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
8 class TileMap(object): |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
9 """Helper class for describing all the game tiles.""" |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
10 |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
11 # number -> (name, image file name) |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
12 DEFAULT_TILES = { |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
13 0: ("woodland", "woodland.png"), |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
14 1: ("grassland", "grassland.png"), |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
15 2: ("fence", "fence.png"), |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
16 3: ("henhouse", "grassland.png"), |
175
a4e3d26d16f0
Fix tile image for guardtower.
Simon Cross <hodgestar@gmail.com>
parents:
133
diff
changeset
|
17 4: ("guardtower", "grassland.png"), |
62 | 18 5: ("broken fence", "broken_fence.png"), |
133
b7dc405a28be
Add Hendominium image support.
Simon Cross <hodgestar@gmail.com>
parents:
62
diff
changeset
|
19 6: ("hendominium", "grassland.png"), |
56
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
20 } |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
21 |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
22 def __init__(self, tiles=None): |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
23 if tiles is None: |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
24 tiles = self.DEFAULT_TILES.copy() |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
25 self._tiles = tiles |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
26 self._reverse_map = dict((v[0], k) for k, v in self._tiles.iteritems()) |
24 | 27 |
56
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
28 def __getitem__(self, n): |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
29 """Get the string name of tile n.""" |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
30 return self._tiles[n][0] |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
31 |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
32 def tile_number(self, name): |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
33 """Return the tile number of the tile with the given name.""" |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
34 return self._reverse_map[name] |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
35 |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
36 def tiles_for_image(self, image_name): |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
37 """Return tile numbers associated with the given image name.""" |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
38 for n, (name, image) in self._tiles.iteritems(): |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
39 if image_name == image: |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
40 yield n |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
41 |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
42 TILE_MAP = TileMap() |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
43 REVERSE_TILE_MAP = TILE_MAP._reverse_map |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
44 |
379
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
45 |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
46 class LayeredSprites(object): |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
47 # This is as small a subset of list behaviour as we can get away with. |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
48 # WARNING: It makes assumptions that may not be justified in the general case. |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
49 # Caveat utilitor and all the rest. |
218
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
50 |
379
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
51 def __init__(self, layers=None, default_layer=None): |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
52 # Layers are ordered from bottom to top. |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
53 if layers is None: |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
54 layers = ['default'] |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
55 self.layers = layers |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
56 if default_layer is None: |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
57 default_layer = layers[0] |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
58 self.default_layer = default_layer |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
59 self._sprites = {} |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
60 for layer in layers: |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
61 self._sprites[layer] = [] |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
62 self.removed = [] |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
63 |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
64 def append(self, sprite, layer=None): |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
65 if layer is None: |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
66 layer = self.default_layer |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
67 self._sprites[layer].append(sprite) |
218
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
68 sprite.updated = 1 |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
69 |
379
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
70 def remove(self, sprite, layer=None): |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
71 if layer is None: |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
72 layer = self.default_layer |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
73 self._sprites[layer].remove(sprite) |
218
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
74 sprite.updated = 1 |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
75 self.removed.append(sprite) |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
76 |
379
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
77 def __getitem__(self, key): |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
78 # vid.py uses sprites[:] to get a mutation-safe list copy, so we need this. |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
79 # No other indexing or slicing operations make sense. |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
80 if not isinstance(key, slice): |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
81 raise IndexError('[:] is the only supported slice/index operation.') |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
82 return iter(self) |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
83 |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
84 def __iter__(self): |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
85 # We iterate over all sprites in layer order. |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
86 for layer in self.layers: |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
87 for sprite in self._sprites[layer]: |
a8a7ada27fa2
Multilayer sprite ordering. Only used by cursors so far.
Jeremy Thurgood <firxen@gmail.com>
parents:
303
diff
changeset
|
88 yield sprite |
218
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
89 |
24 | 90 |
91 class FarmVid(tilevid.Tilevid): | |
92 """Extension of pgu's TileVid class to handle the complications | |
93 of raising chickens. | |
94 """ | |
95 def __init__(self): | |
96 tilevid.Tilevid.__init__(self) | |
381
7a58dadfd251
little cleanups, every sprite in its proper layer.
Jeremy Thurgood <firxen@gmail.com>
parents:
379
diff
changeset
|
97 self.sprites = LayeredSprites(['buildings', 'animals', 'animations', 'cursor'], 'animals') |
24 | 98 |
99 def png_folder_load_tiles(self, path): | |
100 """Load tiles from a folder of PNG files.""" | |
303
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
285
diff
changeset
|
101 full_path = data.filepath(path) |
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
285
diff
changeset
|
102 for dirpath, dirnames, filenames in os.walk(full_path): |
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
285
diff
changeset
|
103 relative_path = dirpath[len(full_path):] |
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
285
diff
changeset
|
104 relative_path = "/".join(relative_path.split(os.path.sep)) |
24 | 105 for filename in filenames: |
303
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
285
diff
changeset
|
106 image_name = "/".join([path, relative_path, filename]) |
56
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
107 for tile_no in TILE_MAP.tiles_for_image(filename): |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
108 tile_name = TILE_MAP[tile_no] |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
109 self.tiles[tile_no] = FarmTile(tile_no, tile_name, image_name) |
24 | 110 |
30 | 111 def sun(self, sun_on): |
112 """Make it night.""" | |
113 for tile in self.tiles: | |
114 if hasattr(tile, "sun"): | |
115 tile.sun(sun_on) | |
116 for sprite in self.sprites: | |
117 if hasattr(sprite, "sun"): | |
118 sprite.sun(sun_on) | |
119 | |
24 | 120 class FarmTile(vid.Tile): |
30 | 121 |
56
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
122 def __init__(self, tile_no, tile_name, image_name): |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
123 self.tile_no = tile_no |
205789321a46
Replace TILE_MAP with richer (but backwards compatible) structure. Record tile number and tile name on tile object.
Simon Cross <hodgestar@gmail.com>
parents:
51
diff
changeset
|
124 self.tile_name = tile_name |
49
dd9d2a4dd494
Update tile loading to use imagecache and new data.py auto-converting of paths.
Simon Cross <hodgestar@gmail.com>
parents:
30
diff
changeset
|
125 self.day_image = imagecache.load_image(image_name) |
dd9d2a4dd494
Update tile loading to use imagecache and new data.py auto-converting of paths.
Simon Cross <hodgestar@gmail.com>
parents:
30
diff
changeset
|
126 self.night_image = imagecache.load_image(image_name, ("night",)) |
51
32fb395cf71c
Add missing call to vid.Tile.__init__ to FarmTile.
Simon Cross <hodgestar@gmail.com>
parents:
49
diff
changeset
|
127 vid.Tile.__init__(self, self.day_image) |
30 | 128 |
129 def sun(self, sun_on): | |
130 if sun_on: | |
131 self.image = self.day_image | |
132 else: | |
133 self.image = self.night_image |