Add new screenshot
[erdslangetjie.git] / erdslangetjie / level.py
1 # The level object
2
3 import os
4 from data import load_image, load, filepath
5
6
7 class Level(object):
8
9     def __init__(self, levelfile):
10         self.data = []
11         self.exit_pos = []
12         self.enter_pos = None
13         self.tiles = []
14         # Because of how kivy's coordinate system works,
15         # we reverse the lines so things match up between
16         # the file and the display (top of file == top of display)
17         for line in reversed(levelfile.readlines()):
18             self.data.append(list(line.strip('\n')))
19
20     def load_tiles(self):
21         """Load the list of tiles for the level"""
22         self.tiles = []
23         self.exit_pos = []
24         self.enter_pos = None
25         for j, line in enumerate(self.data):
26             tile_line = []
27             for i, c in enumerate(line):
28                 if c == ' ':
29                     tile_line.append(load_image('tiles/floor.bmp'))
30                 elif c == '.':
31                     tile_line.append(load_image('tiles/wall.bmp'))
32                 elif c == 'E' or c == 'X':
33                     if c == 'E':
34                         if self.enter_pos:
35                             raise RuntimeError('Multiple entry points')
36                         self.enter_pos = (i, j)
37                     else:
38                         self.exit_pos.append((i, j))
39                     tile_line.append(load_image('tiles/door.bmp'))
40             self.tiles.append(tile_line)
41
42     def get_tiles(self):
43         return self.tiles
44
45     def at_exit(self, pos):
46         return pos in self.exit_pos
47
48     def blocked(self, pos):
49         if pos[0] < 0:
50             return True
51         if pos[1] < 0:
52             return True
53         try:
54             tile = self.data[pos[1]][pos[0]]
55         except IndexError:
56             return True
57         if tile == '.':
58             return True
59         return False
60
61
62 class LevelList(object):
63
64     LEVELS = 'level_list'
65
66     def __init__(self):
67         self.levels = []
68         level_list = load(self.LEVELS)
69         for line in level_list:
70             line = line.strip()
71             if os.path.exists(filepath(line)):
72                 level_file = load(line)
73                 self.levels.append(Level(level_file))
74                 level_file.close()
75             else:
76                 print 'Level list includes non-existant level %s' % line
77         level_list.close()
78         self._cur_level = 0
79
80     def get_current_level(self):
81         if self._cur_level < len(self.levels):
82             return self.levels[self._cur_level]
83         else:
84             return None
85
86     def advance_to_next_level(self):
87         self._cur_level += 1
88         return self.get_current_level()