Editor now changes the map
[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         print '\n'.join([''.join(x) for x in self._data])
97         self._data[pos[1]][pos[0]] = new_type
98         print
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):
110                 continue
111             tile = self._data[new_pos[1]][new_pos[0]]
112             print new_pos, tile
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))
116
117     def get_size(self):
118         return len(self._tiles[0]), len(self._tiles)
119
120     def at_exit(self, pos):
121         return pos in self.exit_pos
122
123     def _get_wall_tile(self, pos):
124         # Is the neighbour in this direction also a wall?
125         x, y = pos
126         left = right = top = bottom = False
127         if x == 0:
128             left = True
129         elif self._data[y][x - 1] == WALL:
130             left = True
131         if x == len(self._data[0]) - 1:
132             right = True
133         elif self._data[y][x + 1] == WALL:
134             right = True
135         if y == 0:
136             top = True
137         elif self._data[y - 1][x] == WALL:
138             top = True
139         if y == len(self._data) - 1:
140             bottom = True
141         elif self._data[y + 1][x] == WALL:
142             bottom = True
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')
153         elif top and bottom:
154             return load_image('tiles/vert_wall.png')
155         elif left and right:
156             return load_image('tiles/horiz_wall.png')
157         elif left and top:
158             return load_image('tiles/corner_lt.png')
159         elif left and bottom:
160             return load_image('tiles/corner_lb.png')
161         elif right and top:
162             return load_image('tiles/corner_rt.png')
163         elif right and bottom:
164             return load_image('tiles/corner_rb.png')
165         elif top:
166             return load_image('tiles/end_top.png')
167         elif bottom:
168             return load_image('tiles/end_bottom.png')
169         elif left:
170             return load_image('tiles/end_right.png')
171         elif right:
172             return load_image('tiles/end_left.png')
173         return load_image('tiles/pillar.png')
174
175     def _in_limits(self, pos):
176         if pos[0] < 0:
177             return False
178         if pos[1] < 0:
179             return False
180         try:
181             self._data[pos[1]][pos[0]]
182         except IndexError:
183             return False
184         print pos, self._data[pos[1]][pos[0]]
185         return True
186
187     def blocked(self, pos):
188         if pos[0] < 0:
189             return True
190         if pos[1] < 0:
191             return True
192         try:
193             tile = self._data[pos[1]][pos[0]]
194         except IndexError:
195             return True
196         if tile == WALL or tile == ENTRY:
197             return True
198         if tile == GATE:
199             if self._gates[pos] != 'down':
200                 return True
201         return False
202
203     def is_gate(self, pos):
204         return self._data[pos[1]][pos[0]] == GATE
205
206     def is_button(self, pos):
207         return self._data[pos[1]][pos[0]] == BUTTON
208
209     def trigger_button(self, pos):
210         if not self.is_button(pos):
211             return False
212         if not self._buttons[pos] == 'active':
213             return False
214         # Find the closest gate down gate and trigger it
215         gate_pos = pos
216
217         self._changed.append((pos, self.get_single_tile(pos)))
218         self._changed.append((gate_pos, self.get_single_tile(pos)))
219
220     def damage_gate(self, pos):
221         if not self.is_gate(pos):
222             return False
223         if self._gates[pos] == -1 or self._gates[pos] == 0:
224             return False
225         self._gates[pos] = self._gates[pos] - 1
226         self._fix_gate_tile(pos)
227         self._changed.append((pos, self.get_single_tile(pos)))
228         return True
229
230     def get_changed_tiles(self):
231         ret = self._changed[:]
232         self._changed = []
233         return ret
234
235
236 class LevelList(object):
237
238     LEVELS = 'level_list'
239
240     def __init__(self):
241         self.levels = []
242         level_list = load(self.LEVELS)
243         for line in level_list:
244             line = line.strip()
245             if os.path.exists(filepath(line)):
246                 level_file = load(line)
247                 self.levels.append(Level(level_file))
248                 level_file.close()
249             else:
250                 print 'Level list includes non-existant level %s' % line
251         level_list.close()
252         self._cur_level = 0
253
254     def get_current_level(self):
255         if self._cur_level < len(self.levels):
256             return self.levels[self._cur_level]
257         else:
258             return None
259
260     def advance_to_next_level(self):
261         self._cur_level += 1
262         return self.get_current_level()
263
264     def reset(self):
265         self._cur_level = 0