Add placeholder underground images. Start work on proper tunnel image loading
[koperkapel.git] / koperkapel / loaders / levelloader.py
index a8aaa376efba9be55b7af1a7f7c70fab8ef8df18..6820b157598ebf42edefde880791327655c4035b 100644 (file)
@@ -13,7 +13,7 @@ class Tile:
     TILESET = None
 
     @classmethod
-    def image(cls):
+    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))
@@ -24,7 +24,7 @@ class RandomizedTile(Tile):
     ROTATE = None
 
     @classmethod
-    def image(cls):
+    def image(cls, neighbors):
         if cls.IMGDIR is None or cls.TILESET is None:
             raise NotImplementedError()
 
@@ -45,10 +45,33 @@ class Wall(RandomizedTile):
     IMGDIR = "wall"
 
 class Underground(RandomizedTile):
-    IMGDIR = "wall"
+    IMGDIR = "underground"
 
-class Tunnel(RandomizedTile):
-    IMGDIR = "floor"
+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
@@ -83,10 +106,21 @@ class LevelLoader(ResourceLoader):
 
     def _load_tile_images(self):
         """Load all the tile images"""
-        for row_data in self._tiles:
-            for tile in row_data:
+        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']:
-                    tile['%s image' % layer] = TILES[tile[layer]['base']].image()
+                    neighbors = [x[layer] if x else None for x in neighborhood]
+                    tile['%s image' % layer] = \
+                            TILES[tile[layer]['base']].image(neighbors)
 
 
 levels = LevelLoader('levels')