Refactor code and add some editor functions
[erdslangetjie.git] / erdslangetjie / level.py
1 # The level object
2
3 import os
4 from data import load_image, load, filepath
5
6 WALL = '.'
7 FLOOR = ' '
8 ENTRY = 'E'
9 EXIT = 'X'
10 GATE = 'G'
11 BUTTON = 'B'
12
13
14 class Level(object):
15
16     def __init__(self, levelfile):
17         self._data = []
18         self.exit_pos = []
19         self.enter_pos = None
20         self._tiles = []
21         self._changed = []
22         self._gates = {}
23         self._buttons = {}
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')))
29
30     def load_tiles(self):
31         """Load the list of tiles for the level"""
32         self._tiles = []
33         self._gates = {}
34         self._buttons = {}
35         self.exit_pos = []
36         self._changed = []
37         self.enter_pos = None
38         for j, line in enumerate(self._data):
39             tile_line = []
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)
44
45     def _get_tile_image(self, pos, c):
46         if pos in self._gates:
47             del self._gates[pos]
48         if pos in self._buttons:
49             del self._buttons[pos]
50         image = None
51         if c == FLOOR:
52             image = load_image('tiles/floor.png')
53         elif c == WALL:
54             image = self._get_wall_tile(pos)
55         elif c == ENTRY:
56             self.enter_pos = pos
57             image = load_image('tiles/entry.png')
58         elif c == EXIT:
59             self.exit_pos.append(pos)
60             image = load_image('tiles/door.png')
61         elif c == GATE:
62             image = load_image('tiles/gate_down.png')
63             self._gates[pos] = -1  # down
64         elif c == BUTTON:
65             image = load_image('tiles/button.png')
66             self._buttons[pos] = 'active'
67         if image is None:
68             raise RuntimeError('Unknown tile type %s at %s' % (c, pos))
69         return image
70
71     def validate(self):
72         entry_points = 0
73         exit_points = 0
74         for line in self._data:
75             if ENTRY in line:
76                 entry_points += 1
77             if EXIT in line:
78                 exit_points += 1
79         if entry_points == 0:
80             raise RuntimeError('No entry point')
81         if entry_points > 1:
82             raise RuntimeError('Multiple entry points')
83         if exit_points == 0:
84             raise RuntimeError('No exit')
85
86     def get_tiles(self):
87         return self._tiles
88
89     def get_single_tile(self, pos):
90         return self._tiles[pos[1]][pos[0]]
91
92     def get_tile_type(self, pos):
93         return self._data[pos[1]][pos[0]]
94
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):
104                 continue
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))
109
110     def get_size(self):
111         return len(self._tiles[0]), len(self._tiles)
112
113     def at_exit(self, pos):
114         return pos in self.exit_pos
115
116     def _get_wall_tile(self, pos):
117         # Is the neighbour in this direction also a wall?
118         x, y = pos
119         left = right = top = bottom = False
120         if x == 0:
121             left = True
122         elif self._data[y][x - 1] == WALL:
123             left = True
124         if x == len(self._data[0]) - 1:
125             right = True
126         elif self._data[y][x + 1] == WALL:
127             right = True
128         if y == 0:
129             top = True
130         elif self._data[y - 1][x] == WALL:
131             top = True
132         if y == len(self._data) - 1:
133             bottom = True
134         elif self._data[y + 1][x] == WALL:
135             bottom = True
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')
146         elif top and bottom:
147             return load_image('tiles/vert_wall.png')
148         elif left and right:
149             return load_image('tiles/horiz_wall.png')
150         elif left and top:
151             return load_image('tiles/corner_lt.png')
152         elif left and bottom:
153             return load_image('tiles/corner_lb.png')
154         elif right and top:
155             return load_image('tiles/corner_rt.png')
156         elif right and bottom:
157             return load_image('tiles/corner_rb.png')
158         elif top:
159             return load_image('tiles/end_top.png')
160         elif bottom:
161             return load_image('tiles/end_bottom.png')
162         elif left:
163             return load_image('tiles/end_right.png')
164         elif right:
165             return load_image('tiles/end_left.png')
166         return load_image('tiles/pillar.png')
167
168     def _in_limits(self, pos):
169         if pos[0] < 0:
170             return False
171         if pos[1] < 0:
172             return False
173         try:
174             self._data[pos[1]][pos[0]]
175         except IndexError:
176             return False
177         return True
178
179     def blocked(self, pos):
180         if pos[0] < 0:
181             return True
182         if pos[1] < 0:
183             return True
184         try:
185             tile = self._data[pos[1]][pos[0]]
186         except IndexError:
187             return True
188         if tile == WALL or tile == ENTRY:
189             return True
190         if tile == GATE:
191             if self._gates[pos] != 'down':
192                 return True
193         return False
194
195     def is_gate(self, pos):
196         return self._data[pos[1]][pos[0]] == GATE
197
198     def is_button(self, pos):
199         return self._data[pos[1]][pos[0]] == BUTTON
200
201     def trigger_button(self, pos):
202         if not self.is_button(pos):
203             return False
204         if not self._buttons[pos] == 'active':
205             return False
206         # Find the closest gate down gate and trigger it
207         gate_pos = pos
208
209         self._changed.append((pos, self.get_single_tile(pos)))
210         self._changed.append((gate_pos, self.get_single_tile(pos)))
211
212     def damage_gate(self, pos):
213         if not self.is_gate(pos):
214             return False
215         if self._gates[pos] == -1 or self._gates[pos] == 0:
216             return False
217         self._gates[pos] = self._gates[pos] - 1
218         self._fix_gate_tile(pos)
219         self._changed.append((pos, self.get_single_tile(pos)))
220         return True
221
222     def get_changed_tiles(self):
223         ret = self._changed[:]
224         self._changed = []
225         return ret
226
227
228 class LevelList(object):
229
230     LEVELS = 'level_list'
231
232     def __init__(self):
233         self.levels = []
234         level_list = load(self.LEVELS)
235         for line in level_list:
236             line = line.strip()
237             if os.path.exists(filepath(line)):
238                 level_file = load(line)
239                 self.levels.append(Level(level_file))
240                 level_file.close()
241             else:
242                 print 'Level list includes non-existant level %s' % line
243         level_list.close()
244         self._cur_level = 0
245
246     def get_current_level(self):
247         if self._cur_level < len(self.levels):
248             return self.levels[self._cur_level]
249         else:
250             return None
251
252     def advance_to_next_level(self):
253         self._cur_level += 1
254         return self.get_current_level()
255
256     def reset(self):
257         self._cur_level = 0