Redo keyboard navigation code to support WASD as well
[erdslangetjie.git] / erdslangetjie / level.py
index 790557179a7ba5fb6d11361887396fbad8b4896f..e1c534d71f064b75b6917e442b791504e3a3892b 100644 (file)
 # The level object
 
-from data import load_image
+import os
+from data import load_image, load, filepath
+
+WALL = '.'
+FLOOR = ' '
+ENTRY = 'E'
+EXIT = 'X'
+GATE = 'G'
+BUTTON = 'B'
 
 
 class Level(object):
 
-    def __init__(self):
-        self.data = []
+    def __init__(self, levelfile):
+        self._data = []
         self.exit_pos = []
         self.enter_pos = None
-        self.tiles = []
-
-    def load(self, levelfile):
-        """Load the level"""
-        self.data = []
-        for line in levelfile.readlines():
-            self.data.append(list(line.strip('\n')))
+        self._tiles = []
+        self._changed = []
+        self._gates = {}
+        self._buttons = {}
+        # Because of how kivy's coordinate system works,
+        # we reverse the lines so things match up between
+        # the file and the display (top of file == top of display)
+        for line in reversed(levelfile.readlines()):
+            self._data.append(list(line.strip('\n')))
 
     def load_tiles(self):
         """Load the list of tiles for the level"""
-        self.tiles = []
+        self._tiles = []
+        self._gates = {}
+        self._buttons = {}
         self.exit_pos = []
+        self._changed = []
         self.enter_pos = None
-        for j, line in enumerate(self.data):
+        for j, line in enumerate(self._data):
             tile_line = []
             for i, c in enumerate(line):
-                if c == ' ':
-                    tile_line.append(load_image('tiles/floor.bmp'))
-                elif c == '.':
-                    tile_line.append(load_image('tiles/wall.bmp'))
-                elif c == 'E' or c == 'X':
-                    if c == 'E':
-                        if self.enter_pos:
-                            raise RuntimeError('Multiple entry points')
-                        self.enter_pos = (i, j)
-                    else:
-                        self.exit_pos.append((i, j))
-                    tile_line.append(load_image('tiles/door.bmp'))
-            self.tiles.append(tile_line)
+                if c == FLOOR:
+                    tile_line.append(load_image('tiles/floor.png'))
+                elif c == WALL:
+                    tile_line.append(self._get_wall_tile(i, j))
+                elif c == ENTRY:
+                    if self.enter_pos:
+                        raise RuntimeError('Multiple entry points')
+                    self.enter_pos = (i, j)
+                    tile_line.append(load_image('tiles/entry.png'))
+                elif c == EXIT:
+                    self.exit_pos.append((i, j))
+                    tile_line.append(load_image('tiles/door.png'))
+                elif c == GATE:
+                    tile_line.append('tiles/gate_down.png')
+                    self._gates[(i, j)] = -1  # down
+                elif c == BUTTON:
+                    tile_line.append('tiles/button.png')
+                    self._buttons[(i, j)] = 'active'
+            self._tiles.append(tile_line)
 
     def get_tiles(self):
-        return self.tiles
+        return self._tiles
+
+    def get_single_tile(self, pos):
+        return self._tiles[pos[1]][pos[0]]
+
+    def get_size(self):
+        return len(self._tiles[0]), len(self._tiles)
 
     def at_exit(self, pos):
         return pos in self.exit_pos
+
+    def _get_wall_tile(self, x, y):
+        # Is the neighbour in this direction also a wall?
+        left = right = top = bottom = False
+        if x == 0:
+            left = True
+        elif self._data[y][x - 1] == WALL:
+            left = True
+        if x == len(self._data[0]) - 1:
+            right = True
+        elif self._data[y][x + 1] == WALL:
+            right = True
+        if y == 0:
+            top = True
+        elif self._data[y - 1][x] == WALL:
+            top = True
+        if y == len(self._data) - 1:
+            bottom = True
+        elif self._data[y + 1][x] == WALL:
+            bottom = True
+        if top and bottom and left and right:
+            return load_image('tiles/cwall.png')
+        elif bottom and left and right:
+            return load_image('tiles/bottom_wall.png')
+        elif top and left and right:
+            return load_image('tiles/top_wall.png')
+        elif top and bottom and right:
+            return load_image('tiles/left_wall.png')
+        elif top and bottom and left:
+            return load_image('tiles/right_wall.png')
+        elif top and bottom:
+            return load_image('tiles/vert_wall.png')
+        elif left and right:
+            return load_image('tiles/horiz_wall.png')
+        elif left and top:
+            return load_image('tiles/corner_lt.png')
+        elif left and bottom:
+            return load_image('tiles/corner_lb.png')
+        elif right and top:
+            return load_image('tiles/corner_rt.png')
+        elif right and bottom:
+            return load_image('tiles/corner_rb.png')
+        elif top:
+            return load_image('tiles/end_top.png')
+        elif bottom:
+            return load_image('tiles/end_bottom.png')
+        elif left:
+            return load_image('tiles/end_right.png')
+        elif right:
+            return load_image('tiles/end_left.png')
+        return load_image('tiles/pillar.png')
+
+    def blocked(self, pos):
+        if pos[0] < 0:
+            return True
+        if pos[1] < 0:
+            return True
+        try:
+            tile = self._data[pos[1]][pos[0]]
+        except IndexError:
+            return True
+        if tile == WALL or tile == ENTRY:
+            return True
+        if tile == GATE:
+            if self._gates[pos] != 'down':
+                return True
+        return False
+
+    def is_gate(self, pos):
+        return self._data[pos[1]][pos[0]] == GATE
+
+    def is_button(self, pos):
+        return self._data[pos[1]][pos[0]] == BUTTON
+
+    def trigger_button(self, pos):
+        if not self.is_button(pos):
+            return False
+        if not self._buttons[pos] == 'active':
+            return False
+        # Find the closest gate down gate and trigger it
+        gate_pos = pos
+
+        self._changed.append((pos, self.get_single_tile(pos)))
+        self._changed.append((gate_pos, self.get_single_tile(pos)))
+
+    def damage_gate(self, pos):
+        if not self.is_gate(pos):
+            return False
+        if self._gates[pos] == -1 or self._gates[pos] == 0:
+            return False
+        self._gates[pos] = self._gates[pos] - 1
+        self._fix_gate_tile(pos)
+        self._changed.append((pos, self.get_single_tile(pos)))
+        return True
+
+    def get_changed_tiles(self):
+        ret = self._changed[:]
+        self._changed = []
+        return ret
+
+
+class LevelList(object):
+
+    LEVELS = 'level_list'
+
+    def __init__(self):
+        self.levels = []
+        level_list = load(self.LEVELS)
+        for line in level_list:
+            line = line.strip()
+            if os.path.exists(filepath(line)):
+                level_file = load(line)
+                self.levels.append(Level(level_file))
+                level_file.close()
+            else:
+                print 'Level list includes non-existant level %s' % line
+        level_list.close()
+        self._cur_level = 0
+
+    def get_current_level(self):
+        if self._cur_level < len(self.levels):
+            return self.levels[self._cur_level]
+        else:
+            return None
+
+    def advance_to_next_level(self):
+        self._cur_level += 1
+        return self.get_current_level()
+
+    def reset(self):
+        self._cur_level = 0