Add starting position to level, and show it on view level screen
[koperkapel.git] / koperkapel / loaders / levelloader.py
index 6820b157598ebf42edefde880791327655c4035b..4bc24db683502361317ff4c0b697f6ad603af6f1 100644 (file)
@@ -1,76 +1,12 @@
 """Loader a level, using the pygame-zero ResourceLoader infrastructure"""
 
-import os
 import json
 
-from pgzero.loaders import images, ResourceLoader
-import os
-import random
-from pygame.transform import rotate
+from pgzero.loaders import ResourceLoader
 
-class Tile:
-    IMG = None
-    TILESET = None
+from ..gamelib.tiles import Wall, Floor, Tunnel, Underground
+from ..gamelib.level import Level
 
-    @classmethod
-    def image(cls, neighbors):
-        if cls.IMG is None or cls.TILESET is None:
-            raise NotImplementedError()
-        return images.load(os.path.join(cls.TILESET, cls.IMG))
-
-class RandomizedTile(Tile):
-    IMGDIR = None
-    TILESET = None
-    ROTATE = None
-
-    @classmethod
-    def image(cls, neighbors):
-        if cls.IMGDIR is None or cls.TILESET is None:
-            raise NotImplementedError()
-
-        imgdir = os.path.join(os.path.dirname(__file__), '..', 'images',
-                cls.TILESET, cls.IMGDIR)
-        imgpath = os.path.splitext(random.choice(os.listdir(imgdir)))[0]
-        img = images.load(os.path.join(cls.TILESET, cls.IMGDIR, imgpath))
-
-        if cls.ROTATE:
-            img = rotate(img, 90 * random.randint(0, 3))
-
-        return img
-
-class Floor(RandomizedTile):
-    IMGDIR = "floor"
-
-class Wall(RandomizedTile):
-    IMGDIR = "wall"
-
-class Underground(RandomizedTile):
-    IMGDIR = "underground"
-
-class Tunnel(Tile):
-
-    @classmethod
-    def image(cls, neighbors):
-        connections = len([x for x in neighbors if 'walk' in x['behaviour']])
-        # simple cases
-        if connections == 0:
-            # return single point tunnel
-            pass
-        elif connections == 4:
-            # crossroads
-            pass
-        elif connections == 3:
-            # 3 point connector, rotated correctly
-            pass
-        elif connections == 1:
-            # 1 point connector, roatated correctly
-            pass
-        elif connections == 2:
-            # Need to distinguish pass-through or corner, and
-            # rotate correctly
-            pass
-        cls.IMG = os.path.join('tunnel', 'tunnel_none')
-        return super(Tunnel, cls).image(neighbors)
         
 
 TILES = {
@@ -90,33 +26,27 @@ class LevelLoader(ResourceLoader):
         f = open(level_path, 'r')
         level_data = json.load(f)
         f.close()
-        self._height = len(level_data['tiles'])
-        self._width = len(level_data['tiles'][0])
-        self._tiles = level_data['tiles']
-        self._tileset = level_data['tileset']
+        self._level = Level()
+        self._level.height = len(level_data['tiles'])
+        self._level.width = len(level_data['tiles'][0])
+        self._level.tiles = level_data['tiles']
+        self._level.tileset = level_data['tileset']
+        self._level.start_pos = level_data["starting pos"]
         # Consistency check, so we can assume things are correct
         # in the level renderer
-        for row, row_data in enumerate(self._tiles):
-            if len(row_data) != self._width:
+        for row, row_data in enumerate(self._level.tiles):
+            if len(row_data) != self._level.width:
                 raise RuntimeError("Incorrect len for row %d" % row)
         for tile in TILES.values():
-            tile.TILESET = self._tileset
+            tile.TILESET = self._level.tileset
         self._load_tile_images()
-        return level_data
+        return self._level
 
     def _load_tile_images(self):
         """Load all the tile images"""
-        height = len(self._tiles)
-        width = len(self._tiles[0])
-        for y, row_data in enumerate(self._tiles):
+        for y, row_data in enumerate(self._level.tiles):
             for x, tile in enumerate(row_data):
-                # simplist case
-                # 4 -connected neighbors
-                neighborhood = [self._tiles[y][x-1] if x > 0 else None,
-                                self._tiles[y][x+1] if x < width - 1 else None,
-                                self._tiles[y-1][x] if y > 0 else None,
-                                self._tiles[y+1][x] if y < height- 1 else None,
-                               ]
+                neighborhood = self._level.get_neighbors(x, y)
                 for layer in ['floor', 'tunnels']:
                     neighbors = [x[layer] if x else None for x in neighborhood]
                     tile['%s image' % layer] = \