Refactor code and add some editor functions
[erdslangetjie.git] / erdslangetjie / level.py
index cbec02c35d899a7ceb18123891bd177b1add1b16..8efc539e13a08d6b6ab7cafab4bd7fc1715f5197 100644 (file)
@@ -7,70 +7,131 @@ WALL = '.'
 FLOOR = ' '
 ENTRY = 'E'
 EXIT = 'X'
+GATE = 'G'
+BUTTON = 'B'
 
 
 class Level(object):
 
     def __init__(self, levelfile):
-        self.data = []
+        self._data = []
         self.exit_pos = []
         self.enter_pos = None
-        self.tiles = []
+        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')))
+            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 == FLOOR:
-                    tile_line.append(load_image('tiles/floor.png'))
-                elif c == WALL:
-                    tile_line.append(self.get_wall_tile(i, j))
-                elif c == ENTRY or c == EXIT:
-                    if c == ENTRY:
-                        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.png'))
-            self.tiles.append(tile_line)
+                tile_image = self._get_tile_image((i, j), c)
+                tile_line.append(tile_image)
+            self._tiles.append(tile_line)
+
+    def _get_tile_image(self, pos, c):
+        if pos in self._gates:
+            del self._gates[pos]
+        if pos in self._buttons:
+            del self._buttons[pos]
+        image = None
+        if c == FLOOR:
+            image = load_image('tiles/floor.png')
+        elif c == WALL:
+            image = self._get_wall_tile(pos)
+        elif c == ENTRY:
+            self.enter_pos = pos
+            image = load_image('tiles/entry.png')
+        elif c == EXIT:
+            self.exit_pos.append(pos)
+            image = load_image('tiles/door.png')
+        elif c == GATE:
+            image = load_image('tiles/gate_down.png')
+            self._gates[pos] = -1  # down
+        elif c == BUTTON:
+            image = load_image('tiles/button.png')
+            self._buttons[pos] = 'active'
+        if image is None:
+            raise RuntimeError('Unknown tile type %s at %s' % (c, pos))
+        return image
+
+    def validate(self):
+        entry_points = 0
+        exit_points = 0
+        for line in self._data:
+            if ENTRY in line:
+                entry_points += 1
+            if EXIT in line:
+                exit_points += 1
+        if entry_points == 0:
+            raise RuntimeError('No entry point')
+        if entry_points > 1:
+            raise RuntimeError('Multiple entry points')
+        if exit_points == 0:
+            raise RuntimeError('No exit')
 
     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_tile_type(self, pos):
+        return self._data[pos[1]][pos[0]]
+
+    def set_tile_type(self, pos, new_type):
+        self._data[pos[1]][pos[0]] = new_type
+        new_tile = self._get_tile_image(new_type, pos)
+        self._tiles[pos[1]][pos[0]] = new_tile
+        self._changed.append((pos, new_tile))
+        # Also update neighbourhood for wall types, etc.
+        for new_pos in [(pos[0] - 1, pos[1]), (pos[0] + 1, pos[1]),
+                (pos[0], pos[1] - 1), (pos[0], pos[1] + 1)]:
+            if not self._in_limits(new_pos):
+                continue
+            tile = self._data[new_pos[1]][new_pos[0]]
+            new_tile = self._get_tile_image(tile, pos)
+            self._tiles[new_pos[1]][new_pos[0]] = new_tile
+            self._changed.append((new_pos, new_tile))
 
     def get_size(self):
-        return len(self.tiles[0]), len(self.tiles)
+        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):
+    def _get_wall_tile(self, pos):
         # Is the neighbour in this direction also a wall?
-        left = right = top =  bottom = False
+        x, y = pos
+        left = right = top = bottom = False
         if x == 0:
             left = True
-        elif self.data[y][x - 1] == WALL:
+        elif self._data[y][x - 1] == WALL:
             left = True
-        if x == len(self.data[0]) - 1:
+        if x == len(self._data[0]) - 1:
             right = True
-        elif self.data[y][x + 1] == WALL:
+        elif self._data[y][x + 1] == WALL:
             right = True
         if y == 0:
             top = True
-        elif self.data[y - 1][x] == WALL:
+        elif self._data[y - 1][x] == WALL:
             top = True
-        if y == len(self.data) - 1:
+        if y == len(self._data) - 1:
             bottom = True
-        elif self.data[y + 1][x] == WALL:
+        elif self._data[y + 1][x] == WALL:
             bottom = True
         if top and bottom and left and right:
             return load_image('tiles/cwall.png')
@@ -104,6 +165,16 @@ class Level(object):
             return load_image('tiles/end_left.png')
         return load_image('tiles/pillar.png')
 
+    def _in_limits(self, pos):
+        if pos[0] < 0:
+            return False
+        if pos[1] < 0:
+            return False
+        try:
+            self._data[pos[1]][pos[0]]
+        except IndexError:
+            return False
+        return True
 
     def blocked(self, pos):
         if pos[0] < 0:
@@ -111,13 +182,48 @@ class Level(object):
         if pos[1] < 0:
             return True
         try:
-            tile = self.data[pos[1]][pos[0]]
+            tile = self._data[pos[1]][pos[0]]
         except IndexError:
             return True
-        if tile == '.':
+        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):
 
@@ -146,3 +252,6 @@ class LevelList(object):
     def advance_to_next_level(self):
         self._cur_level += 1
         return self.get_current_level()
+
+    def reset(self):
+        self._cur_level = 0