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 print '\n'.join([''.join(x) for x in self._data])
97 self._data[pos[1]][pos[0]] = new_type
99 print '\n'.join([''.join(x) for x in self._data])
100 print pos, self._in_limits(pos)
101 new_tile = self._get_tile_image(pos, new_type)
102 self._tiles[pos[1]][pos[0]] = new_tile
103 self._changed.append((pos, new_tile))
104 # Also update neighbourhood for wall types, etc.
105 for new_pos in [(pos[0] - 1, pos[1]), (pos[0] + 1, pos[1]),
106 (pos[0] - 1, pos[1] - 1), (pos[0] + 1, pos[1] + 1),
107 (pos[0], pos[1] - 1), (pos[0], pos[1] + 1),
108 (pos[0] - 1, pos[1] + 1), (pos[0] + 1, pos[1] - 1)]:
109 if not self._in_limits(new_pos):
111 tile = self._data[new_pos[1]][new_pos[0]]
113 new_tile = self._get_tile_image(new_pos, tile)
114 self._tiles[new_pos[1]][new_pos[0]] = new_tile
115 self._changed.append((new_pos, new_tile))
118 return len(self._tiles[0]), len(self._tiles)
120 def at_exit(self, pos):
121 return pos in self.exit_pos
123 def _get_wall_tile(self, pos):
124 # Is the neighbour in this direction also a wall?
126 left = right = top = bottom = False
129 elif self._data[y][x - 1] == WALL:
131 if x == len(self._data[0]) - 1:
133 elif self._data[y][x + 1] == WALL:
137 elif self._data[y - 1][x] == WALL:
139 if y == len(self._data) - 1:
141 elif self._data[y + 1][x] == WALL:
143 if top and bottom and left and right:
144 return load_image('tiles/cwall.png')
145 elif bottom and left and right:
146 return load_image('tiles/bottom_wall.png')
147 elif top and left and right:
148 return load_image('tiles/top_wall.png')
149 elif top and bottom and right:
150 return load_image('tiles/left_wall.png')
151 elif top and bottom and left:
152 return load_image('tiles/right_wall.png')
154 return load_image('tiles/vert_wall.png')
156 return load_image('tiles/horiz_wall.png')
158 return load_image('tiles/corner_lt.png')
159 elif left and bottom:
160 return load_image('tiles/corner_lb.png')
162 return load_image('tiles/corner_rt.png')
163 elif right and bottom:
164 return load_image('tiles/corner_rb.png')
166 return load_image('tiles/end_top.png')
168 return load_image('tiles/end_bottom.png')
170 return load_image('tiles/end_right.png')
172 return load_image('tiles/end_left.png')
173 return load_image('tiles/pillar.png')
175 def _in_limits(self, pos):
181 self._data[pos[1]][pos[0]]
184 print pos, self._data[pos[1]][pos[0]]
187 def blocked(self, pos):
193 tile = self._data[pos[1]][pos[0]]
196 if tile == WALL or tile == ENTRY:
199 if self._gates[pos] != 'down':
203 def is_gate(self, pos):
204 return self._data[pos[1]][pos[0]] == GATE
206 def is_button(self, pos):
207 return self._data[pos[1]][pos[0]] == BUTTON
209 def trigger_button(self, pos):
210 if not self.is_button(pos):
212 if not self._buttons[pos] == 'active':
214 # Find the closest gate down gate and trigger it
217 self._changed.append((pos, self.get_single_tile(pos)))
218 self._changed.append((gate_pos, self.get_single_tile(pos)))
220 def damage_gate(self, pos):
221 if not self.is_gate(pos):
223 if self._gates[pos] == -1 or self._gates[pos] == 0:
225 self._gates[pos] = self._gates[pos] - 1
226 self._fix_gate_tile(pos)
227 self._changed.append((pos, self.get_single_tile(pos)))
230 def get_changed_tiles(self):
231 ret = self._changed[:]
236 class LevelList(object):
238 LEVELS = 'level_list'
242 level_list = load(self.LEVELS)
243 for line in level_list:
245 if os.path.exists(filepath(line)):
246 level_file = load(line)
247 self.levels.append(Level(level_file))
250 print 'Level list includes non-existant level %s' % line
254 def get_current_level(self):
255 if self._cur_level < len(self.levels):
256 return self.levels[self._cur_level]
260 def advance_to_next_level(self):
262 return self.get_current_level()