annotate gamelib/tiles.py @ 62:98ce098f8cbb

Add broken fence tile
author Neil Muller <drnlmuller@gmail.com>
date Mon, 31 Aug 2009 19:32:02 +0000
parents 205789321a46
children b7dc405a28be
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
1 """Extension to pgu's tilevid."""
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
2
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
3 from pgu import tilevid, vid
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
4 import os
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
5 import imagecache
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
6
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
7 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
8 """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
9
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 # 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
11 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
12 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
13 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
14 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
15 3: ("henhouse", "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
16 4: ("guardtower", "guardtower.png"),
62
98ce098f8cbb Add broken fence tile
Neil Muller <drnlmuller@gmail.com>
parents: 56
diff changeset
17 5: ("broken fence", "broken_fence.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
18 }
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
19
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 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
21 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
22 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
23 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
24 self._reverse_map = dict((v[0], k) for k, v in self._tiles.iteritems())
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
25
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
26 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
27 """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
28 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
29
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 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
31 """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
32 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
33
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 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
35 """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
36 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
37 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
38 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
39
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 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
41 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
42
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
43
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
44 class FarmVid(tilevid.Tilevid):
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
45 """Extension of pgu's TileVid class to handle the complications
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
46 of raising chickens.
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
47 """
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
48 def __init__(self):
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
49 tilevid.Tilevid.__init__(self)
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
50
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
51 def png_folder_load_tiles(self, path):
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
52 """Load tiles from a folder of PNG files."""
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
53 for dirpath, dirnames, filenames in os.walk(path):
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
54 abstract_dirpath = "/".join(dirpath.split(os.path.sep))
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
55 for filename in filenames:
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
56 image_name = abstract_dirpath + "/" + 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
57 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
58 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
59 self.tiles[tile_no] = FarmTile(tile_no, tile_name, image_name)
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
60
30
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
61 def sun(self, sun_on):
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
62 """Make it night."""
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
63 for tile in self.tiles:
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
64 if hasattr(tile, "sun"):
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
65 tile.sun(sun_on)
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
66 for sprite in self.sprites:
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
67 if hasattr(sprite, "sun"):
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
68 sprite.sun(sun_on)
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
69
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
70 class FarmTile(vid.Tile):
30
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
71
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
72 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
73 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
74 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
75 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
76 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
77 vid.Tile.__init__(self, self.day_image)
30
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
78
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
79 def sun(self, sun_on):
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
80 if sun_on:
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
81 self.image = self.day_image
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
82 else:
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
83 self.image = self.night_image