Mercurial > rinkhals
annotate gamelib/tiles.py @ 229:96d440bebdaa
Non-permenant high score table
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Fri, 04 Sep 2009 23:40:13 +0000 |
parents | 5cb0e0b9cd16 |
children | e694aa7731ed |
rev | line source |
---|---|
24 | 1 """Extension to pgu's tilevid.""" |
2 | |
3 from pgu import tilevid, vid | |
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 | 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"), |
175
a4e3d26d16f0
Fix tile image for guardtower.
Simon Cross <hodgestar@gmail.com>
parents:
133
diff
changeset
|
16 4: ("guardtower", "grassland.png"), |
62 | 17 5: ("broken fence", "broken_fence.png"), |
133
b7dc405a28be
Add Hendominium image support.
Simon Cross <hodgestar@gmail.com>
parents:
62
diff
changeset
|
18 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
|
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 |
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 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
|
22 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
|
23 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
|
24 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
|
25 self._reverse_map = dict((v[0], k) for k, v in self._tiles.iteritems()) |
24 | 26 |
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
|
27 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
|
28 """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
|
29 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
|
30 |
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 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
|
32 """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
|
33 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
|
34 |
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 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
|
36 """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
|
37 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
|
38 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
|
39 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
|
40 |
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 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
|
42 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
|
43 |
218
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
44 class FarmSprites(list): |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
45 def __init__(self): |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
46 list.__init__(self) |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
47 self.removed = [] |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
48 self._cursor = None |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
49 |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
50 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
|
51 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
|
52 # pop cursor |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
53 assert(self._cursor is self.pop()) |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
54 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
|
55 list.append(self, self._cursor) |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
56 else: |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
57 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
|
58 sprite.updated = 1 |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
59 |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
60 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
|
61 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
|
62 sprite.updated = 1 |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
63 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
|
64 |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
65 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
|
66 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
|
67 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
|
68 self._cursor = cursor |
5cb0e0b9cd16
Make sprite cursors stay on top by fudging the sprite list. :/
Simon Cross <hodgestar@gmail.com>
parents:
175
diff
changeset
|
69 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
|
70 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
|
71 |
24 | 72 |
73 class FarmVid(tilevid.Tilevid): | |
74 """Extension of pgu's TileVid class to handle the complications | |
75 of raising chickens. | |
76 """ | |
77 def __init__(self): | |
78 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
|
79 self.sprites = FarmSprites() |
24 | 80 |
81 def png_folder_load_tiles(self, path): | |
82 """Load tiles from a folder of PNG files.""" | |
83 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
|
84 abstract_dirpath = "/".join(dirpath.split(os.path.sep)) |
24 | 85 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
|
86 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
|
87 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
|
88 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
|
89 self.tiles[tile_no] = FarmTile(tile_no, tile_name, image_name) |
24 | 90 |
30 | 91 def sun(self, sun_on): |
92 """Make it night.""" | |
93 for tile in self.tiles: | |
94 if hasattr(tile, "sun"): | |
95 tile.sun(sun_on) | |
96 for sprite in self.sprites: | |
97 if hasattr(sprite, "sun"): | |
98 sprite.sun(sun_on) | |
99 | |
24 | 100 class FarmTile(vid.Tile): |
30 | 101 |
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
|
102 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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 vid.Tile.__init__(self, self.day_image) |
30 | 108 |
109 def sun(self, sun_on): | |
110 if sun_on: | |
111 self.image = self.day_image | |
112 else: | |
113 self.image = self.night_image |