annotate gamelib/tiles.py @ 373:c14a6d640507 1.0.x 1.0.1

Version 1.0.1.
author Simon Cross <hodgestar@gmail.com>
date Tue, 13 Oct 2009 20:30:08 +0000
parents e12d99215b74
children a8a7ada27fa2
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
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
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
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
98ce098f8cbb Add broken fence tile
Neil Muller <drnlmuller@gmail.com>
parents: 56
diff changeset
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
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
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
218
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
45 class FarmSprites(list):
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
46 def __init__(self):
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
47 list.__init__(self)
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
48 self.removed = []
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
49 self._cursor = None
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
50
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
51 def append(self, sprite):
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
52 if self._cursor is not None:
285
e694aa7731ed Use insert instead of pop, append, append to insert sprite beneath cursor.
Simon Cross <hodgestar@gmail.com>
parents: 218
diff changeset
53 assert(self._cursor is self[-1])
e694aa7731ed Use insert instead of pop, append, append to insert sprite beneath cursor.
Simon Cross <hodgestar@gmail.com>
parents: 218
diff changeset
54 list.insert(self, -1, sprite)
218
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
55 else:
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
56 list.append(self, sprite)
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
57 sprite.updated = 1
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
58
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
59 def remove(self, sprite):
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
60 list.remove(self, sprite)
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
61 sprite.updated = 1
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
62 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
63
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
64 def set_cursor(self, cursor):
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
65 if self._cursor is not None:
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
66 self.remove(self._cursor)
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
67 self._cursor = cursor
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
68 if cursor is not None:
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
69 list.append(self, cursor)
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
70
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
71
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
72 class FarmVid(tilevid.Tilevid):
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
73 """Extension of pgu's TileVid class to handle the complications
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
74 of raising chickens.
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
75 """
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
76 def __init__(self):
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
77 tilevid.Tilevid.__init__(self)
218
5cb0e0b9cd16 Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents: 175
diff changeset
78 self.sprites = FarmSprites()
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
79
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
80 def png_folder_load_tiles(self, path):
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
81 """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
82 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
83 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
84 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
85 relative_path = "/".join(relative_path.split(os.path.sep))
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
86 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
87 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
88 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
89 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
90 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
91
30
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
92 def sun(self, sun_on):
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
93 """Make it night."""
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
94 for tile in self.tiles:
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
95 if hasattr(tile, "sun"):
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
96 tile.sun(sun_on)
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
97 for sprite in self.sprites:
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
98 if hasattr(sprite, "sun"):
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
99 sprite.sun(sun_on)
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
100
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
101 class FarmTile(vid.Tile):
30
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
102
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
103 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
104 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
105 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
106 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
107 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
108 vid.Tile.__init__(self, self.day_image)
30
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
109
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
110 def sun(self, sun_on):
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
111 if sun_on:
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
112 self.image = self.day_image
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
113 else:
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
114 self.image = self.night_image