Add placeholder underground images. Start work on proper tunnel image loading
[koperkapel.git] / koperkapel / loaders / levelloader.py
index ff18fc7b8e0548678dc172dd65db893fe33853fc..6820b157598ebf42edefde880791327655c4035b 100644 (file)
@@ -1,5 +1,6 @@
 """Loader a level, using the pygame-zero ResourceLoader infrastructure"""
 
+import os
 import json
 
 from pgzero.loaders import images, ResourceLoader
@@ -9,26 +10,28 @@ from pygame.transform import rotate
 
 class Tile:
     IMG = None
+    TILESET = None
 
     @classmethod
-    def image(cls):
-        if cls.IMG is None:
+    def image(cls, neighbors):
+        if cls.IMG is None or cls.TILESET is None:
             raise NotImplementedError()
-
-        return images.load(cls.IMG)
+        return images.load(os.path.join(cls.TILESET, cls.IMG))
 
 class RandomizedTile(Tile):
     IMGDIR = None
-    ROTATE = True
+    TILESET = None
+    ROTATE = None
 
     @classmethod
-    def image(cls):
-        if cls.IMGDIR is None:
+    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.IMGDIR)
+        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.IMGDIR, imgpath))
+        img = images.load(os.path.join(cls.TILESET, cls.IMGDIR, imgpath))
 
         if cls.ROTATE:
             img = rotate(img, 90 * random.randint(0, 3))
@@ -41,9 +44,40 @@ class Floor(RandomizedTile):
 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 = {
     "cwall": Wall, # rename this everywhere
     "floor": Floor,
+    "tunnel": Tunnel,
+    "underground": Underground,
 }
 
 class LevelLoader(ResourceLoader):
@@ -59,19 +93,34 @@ class LevelLoader(ResourceLoader):
         self._height = len(level_data['tiles'])
         self._width = len(level_data['tiles'][0])
         self._tiles = level_data['tiles']
+        self._tileset = level_data['tileset']
         # 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:
                 raise RuntimeError("Incorrect len for row %d" % row)
+        for tile in TILES.values():
+            tile.TILESET = self._tileset
         self._load_tile_images()
         return level_data
 
     def _load_tile_images(self):
         """Load all the tile images"""
-        for row_data in self._tiles:
-            for tile in row_data:
-                tile['image'] = TILES[tile['base']].image()
+        height = len(self._tiles)
+        width = len(self._tiles[0])
+        for y, row_data in enumerate(self._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,
+                               ]
+                for layer in ['floor', 'tunnels']:
+                    neighbors = [x[layer] if x else None for x in neighborhood]
+                    tile['%s image' % layer] = \
+                            TILES[tile[layer]['base']].image(neighbors)
 
 
 levels = LevelLoader('levels')