Mercurial > mamba
comparison mamba/level.py @ 123:915fd0ee28f5
Added test tile orientation
author | Gideon Visser <gideon@gideonvisser.com> |
---|---|
date | Sun, 11 Sep 2011 20:59:16 -0700 |
parents | 62a705f4e876 |
children | d285cf18396e |
comparison
equal
deleted
inserted
replaced
122:45dd79e9ba1b | 123:915fd0ee28f5 |
---|---|
71 | 71 |
72 | 72 |
73 class Level(object): | 73 class Level(object): |
74 def __init__(self, level_name): | 74 def __init__(self, level_name): |
75 self.level_name = level_name | 75 self.level_name = level_name |
76 self.tiles_ascii = '' | |
76 self.load_level_data() | 77 self.load_level_data() |
77 | 78 |
78 def load_level_data(self): | 79 def load_level_data(self): |
79 """ | 80 """ |
80 This file format is potentially yucky. | 81 This file format is potentially yucky. |
81 """ | 82 """ |
82 level_data = load_file('levels/%s.txt' % (self.level_name,)) | 83 level_data = load_file('levels/%s.txt' % (self.level_name,)) |
83 tileset_name = level_data.readline().strip() | 84 tileset_name = level_data.readline().strip() |
84 self.tileset = Tileset(tileset_name) | 85 self.tileset = Tileset(tileset_name) |
85 tiles_ascii = [line.strip() for line in level_data.readlines()] | 86 tiles_ascii = [line.strip() for line in level_data.readlines()] |
87 self.tiles_ascii = tiles_ascii | |
86 self.setup_tiles(tiles_ascii) | 88 self.setup_tiles(tiles_ascii) |
87 self.make_background() | 89 self.make_background() |
88 | 90 |
89 def setup_tiles(self, tiles_ascii): | 91 def setup_tiles(self, tiles_ascii): |
90 self.sprites = RenderUpdates() | 92 self.sprites = RenderUpdates() |
94 for y, row in enumerate(tiles_ascii): | 96 for y, row in enumerate(tiles_ascii): |
95 if len(row) != self.tile_size[0]: | 97 if len(row) != self.tile_size[0]: |
96 raise InvalidMapError("Map not rectangular.") | 98 raise InvalidMapError("Map not rectangular.") |
97 tile_row = [] | 99 tile_row = [] |
98 for x, tile_char in enumerate(row): | 100 for x, tile_char in enumerate(row): |
101 tile_orientation = self.get_tile_orientation(y, x, row, tile_char) | |
102 print tile_orientation | |
99 tile = self.tileset.get_tile(tile_char, (x, y), self.sprites) | 103 tile = self.tileset.get_tile(tile_char, (x, y), self.sprites) |
100 tile_row.append(tile) | 104 tile_row.append(tile) |
101 if isinstance(tile, EntrySprite): | 105 if isinstance(tile, EntrySprite): |
102 if self.entry is not None: | 106 if self.entry is not None: |
103 raise InvalidMapError("Too many entry points.") | 107 raise InvalidMapError("Too many entry points.") |
104 self.entry = tile | 108 self.entry = tile |
105 self.tiles.append(tile_row) | 109 self.tiles.append(tile_row) |
106 if self.entry is None: | 110 if self.entry is None: |
107 raise InvalidMapError("Not enough entry points.") | 111 raise InvalidMapError("Not enough entry points.") |
112 | |
113 | |
114 def is_same_tile(self, tile, x, y): | |
115 """Is there a tile of the same type?""" | |
116 try: | |
117 temp_tile = self.tiles_ascii[y][x] | |
118 except IndexError: | |
119 return 'The map went out of range' | |
120 try: | |
121 return temp_tile == tile | |
122 except KeyError: | |
123 return 'The map tile is not existing' | |
124 | |
125 return self.get_bool(x, y, 'wall') | |
126 | |
127 #please note: planning on tile tuple to be -1, 0, 1 of x then y | |
128 def get_tile_orientation(self, map_y, map_x, row, tile_char): | |
129 #print "y: ", map_y, " x: ", map_x, " row: ", row, " tile_char: ", tile_char | |
130 #tile_char | |
131 if self.is_same_tile(tile_char, map_x, map_y): | |
132 # Draw different tiles depending on neighbourhood | |
133 if not self.is_same_tile(tile_char, map_x, map_y+1): | |
134 if self.is_same_tile(tile_char, map_x+1, map_y) and self.is_same_tile(tile_char, map_x-1, map_y): # | |
135 tile = 1, 2 | |
136 elif self.is_same_tile(tile_char, map_x+1, map_y): | |
137 tile = 0, 2 | |
138 elif self.is_same_tile(tile_char, map_x-1, map_y): | |
139 tile = 2, 2 | |
140 else: | |
141 tile = 3, 2 | |
142 else: | |
143 if self.is_same_tile(tile_char, map_x+1, map_y+1) and self.is_same_tile(tile_char, map_x-1, map_y+1): | |
144 tile = 1, 1 | |
145 elif self.is_same_tile(tile_char, map_x+1, map_y+1): | |
146 tile = 0, 1 | |
147 elif self.is_same_tile(tile_char, map_x-1, map_y+1): | |
148 tile = 2, 1 | |
149 else: | |
150 tile = 3, 1 | |
151 return tile | |
108 | 152 |
109 def get_tile_size(self): | 153 def get_tile_size(self): |
110 return self.tile_size | 154 return self.tile_size |
111 | 155 |
112 def get_size(self): | 156 def get_size(self): |