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:
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(new_type, pos)
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], pos[1] - 1), (pos[0], pos[1] + 1)]:
103 if not self._in_limits(new_pos):
105 tile = self._data[new_pos[1]][new_pos[0]]
106 new_tile = self._get_tile_image(tile, pos)
107 self._tiles[new_pos[1]][new_pos[0]] = new_tile
108 self._changed.append((new_pos, new_tile))
111 return len(self._tiles[0]), len(self._tiles)
113 def at_exit(self, pos):
114 return pos in self.exit_pos
116 def _get_wall_tile(self, pos):
117 # Is the neighbour in this direction also a wall?
119 left = right = top = bottom = False
122 elif self._data[y][x - 1] == WALL:
124 if x == len(self._data[0]) - 1:
126 elif self._data[y][x + 1] == WALL:
130 elif self._data[y - 1][x] == WALL:
132 if y == len(self._data) - 1:
134 elif self._data[y + 1][x] == WALL:
136 if top and bottom and left and right:
137 return load_image('tiles/cwall.png')
138 elif bottom and left and right:
139 return load_image('tiles/bottom_wall.png')
140 elif top and left and right:
141 return load_image('tiles/top_wall.png')
142 elif top and bottom and right:
143 return load_image('tiles/left_wall.png')
144 elif top and bottom and left:
145 return load_image('tiles/right_wall.png')
147 return load_image('tiles/vert_wall.png')
149 return load_image('tiles/horiz_wall.png')
151 return load_image('tiles/corner_lt.png')
152 elif left and bottom:
153 return load_image('tiles/corner_lb.png')
155 return load_image('tiles/corner_rt.png')
156 elif right and bottom:
157 return load_image('tiles/corner_rb.png')
159 return load_image('tiles/end_top.png')
161 return load_image('tiles/end_bottom.png')
163 return load_image('tiles/end_right.png')
165 return load_image('tiles/end_left.png')
166 return load_image('tiles/pillar.png')
168 def _in_limits(self, pos):
174 self._data[pos[1]][pos[0]]
179 def blocked(self, pos):
185 tile = self._data[pos[1]][pos[0]]
188 if tile == WALL or tile == ENTRY:
191 if self._gates[pos] != 'down':
195 def is_gate(self, pos):
196 return self._data[pos[1]][pos[0]] == GATE
198 def is_button(self, pos):
199 return self._data[pos[1]][pos[0]] == BUTTON
201 def trigger_button(self, pos):
202 if not self.is_button(pos):
204 if not self._buttons[pos] == 'active':
206 # Find the closest gate down gate and trigger it
209 self._changed.append((pos, self.get_single_tile(pos)))
210 self._changed.append((gate_pos, self.get_single_tile(pos)))
212 def damage_gate(self, pos):
213 if not self.is_gate(pos):
215 if self._gates[pos] == -1 or self._gates[pos] == 0:
217 self._gates[pos] = self._gates[pos] - 1
218 self._fix_gate_tile(pos)
219 self._changed.append((pos, self.get_single_tile(pos)))
222 def get_changed_tiles(self):
223 ret = self._changed[:]
228 class LevelList(object):
230 LEVELS = 'level_list'
234 level_list = load(self.LEVELS)
235 for line in level_list:
237 if os.path.exists(filepath(line)):
238 level_file = load(line)
239 self.levels.append(Level(level_file))
242 print 'Level list includes non-existant level %s' % line
246 def get_current_level(self):
247 if self._cur_level < len(self.levels):
248 return self.levels[self._cur_level]
252 def advance_to_next_level(self):
254 return self.get_current_level()