4 from data import load_image, load, filepath
16 def __init__(self, levelfile):
24 # Because of how kivy's coordinate system works,
25 # we reverse the lines so things match up between
26 # the file and the display (top of file == top of display)
27 for line in reversed(levelfile.readlines()):
28 self._data.append(list(line.strip('\n')))
31 """Load the list of tiles for the level"""
38 for j, line in enumerate(self._data):
40 for i, c in enumerate(line):
41 tile_image = self._get_tile_image((i, j), c)
42 tile_line.append(tile_image)
43 self._tiles.append(tile_line)
45 def _get_tile_image(self, pos, c):
46 if pos in self._gates:
48 if pos in self._buttons:
49 del self._buttons[pos]
52 image = load_image('tiles/floor.png')
54 image = self._get_wall_tile(pos)
57 image = load_image('tiles/entry.png')
59 self.exit_pos.append(pos)
60 image = load_image('tiles/door.png')
62 image = load_image('tiles/gate_down.png')
63 self._gates[pos] = -1 # down
65 image = load_image('tiles/button.png')
66 self._buttons[pos] = 'active'
68 raise RuntimeError('Unknown tile type %s at %s' % (c, pos))
74 for line in self._data:
76 entry_points += line.count(ENTRY)
78 exit_points += line.count(EXIT)
80 raise RuntimeError('No entry point')
82 raise RuntimeError('Multiple entry points')
84 raise RuntimeError('No exit')
89 def get_single_tile(self, pos):
90 return self._tiles[pos[1]][pos[0]]
92 def get_tile_type(self, pos):
93 return self._data[pos[1]][pos[0]]
95 def set_tile_type(self, pos, new_type):
96 self._data[pos[1]][pos[0]] = new_type
97 new_tile = self._get_tile_image(pos, new_type)
98 self._tiles[pos[1]][pos[0]] = new_tile
99 self._changed.append((pos, new_tile))
100 # Also update neighbourhood for wall types, etc.
101 for new_pos in [(pos[0] - 1, pos[1]), (pos[0] + 1, pos[1]),
102 (pos[0] - 1, pos[1] - 1), (pos[0] + 1, pos[1] + 1),
103 (pos[0], pos[1] - 1), (pos[0], pos[1] + 1),
104 (pos[0] - 1, pos[1] + 1), (pos[0] + 1, pos[1] - 1)]:
105 if not self._in_limits(new_pos):
107 tile = self._data[new_pos[1]][new_pos[0]]
108 new_tile = self._get_tile_image(new_pos, tile)
109 self._tiles[new_pos[1]][new_pos[0]] = new_tile
110 self._changed.append((new_pos, new_tile))
113 return len(self._tiles[0]), len(self._tiles)
115 def at_exit(self, pos):
116 return pos in self.exit_pos
118 def get_level_data(self):
119 return '\n'.join(reversed([''.join(x) for x in self._data]))
121 def _get_wall_tile(self, pos):
122 # Is the neighbour in this direction also a wall?
124 left = right = top = bottom = False
127 elif self._data[y][x - 1] == WALL:
129 if x == len(self._data[0]) - 1:
131 elif self._data[y][x + 1] == WALL:
135 elif self._data[y - 1][x] == WALL:
137 if y == len(self._data) - 1:
139 elif self._data[y + 1][x] == WALL:
141 if top and bottom and left and right:
142 return load_image('tiles/cwall.png')
143 elif bottom and left and right:
144 return load_image('tiles/bottom_wall.png')
145 elif top and left and right:
146 return load_image('tiles/top_wall.png')
147 elif top and bottom and right:
148 return load_image('tiles/left_wall.png')
149 elif top and bottom and left:
150 return load_image('tiles/right_wall.png')
152 return load_image('tiles/vert_wall.png')
154 return load_image('tiles/horiz_wall.png')
156 return load_image('tiles/corner_lt.png')
157 elif left and bottom:
158 return load_image('tiles/corner_lb.png')
160 return load_image('tiles/corner_rt.png')
161 elif right and bottom:
162 return load_image('tiles/corner_rb.png')
164 return load_image('tiles/end_top.png')
166 return load_image('tiles/end_bottom.png')
168 return load_image('tiles/end_right.png')
170 return load_image('tiles/end_left.png')
171 return load_image('tiles/pillar.png')
173 def _in_limits(self, pos):
179 self._data[pos[1]][pos[0]]
184 def blocked(self, pos):
190 tile = self._data[pos[1]][pos[0]]
193 if tile == WALL or tile == ENTRY:
196 if self._gates[pos] != 'down':
200 def is_gate(self, pos):
201 return self._data[pos[1]][pos[0]] == GATE
203 def is_button(self, pos):
204 return self._data[pos[1]][pos[0]] == BUTTON
206 def trigger_button(self, pos):
207 if not self.is_button(pos):
209 if not self._buttons[pos] == 'active':
211 # Find the closest gate down gate and trigger it
214 self._changed.append((pos, self.get_single_tile(pos)))
215 self._changed.append((gate_pos, self.get_single_tile(pos)))
217 def damage_gate(self, pos):
218 if not self.is_gate(pos):
220 if self._gates[pos] == -1 or self._gates[pos] == 0:
222 self._gates[pos] = self._gates[pos] - 1
223 self._fix_gate_tile(pos)
224 self._changed.append((pos, self.get_single_tile(pos)))
227 def get_changed_tiles(self):
228 ret = self._changed[:]
233 class LevelList(object):
235 LEVELS = 'level_list'
239 level_list = load(self.LEVELS)
240 for line in level_list:
242 if os.path.exists(filepath(line)):
243 level_file = load(line)
244 self.levels.append(Level(level_file))
247 raise RuntimeError('Level list includes non-existant level %s' % line)
251 def get_current_level(self):
252 if self._cur_level < len(self.levels):
253 return self.levels[self._cur_level]
257 def advance_to_next_level(self):
259 return self.get_current_level()