Reorganise tiles so we can potentially create tilesets
[koperkapel.git] / koperkapel / loaders / levelloader.py
index 640d9b168ada4e59073559058cb8654d72492fb5..839c171e8939bcfe3bc450cb2791a6067f9dfd87 100644 (file)
@@ -1,10 +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
 
 class Tile:
     IMG = None
@@ -18,6 +20,7 @@ class Tile:
 
 class RandomizedTile(Tile):
     IMGDIR = None
+    ROTATE = True
 
     @classmethod
     def image(cls):
@@ -25,9 +28,13 @@ class RandomizedTile(Tile):
             raise NotImplementedError()
 
         imgdir = os.path.join(os.path.dirname(__file__), '..', 'images', cls.IMGDIR)
-        img = os.path.splitext(random.choice(os.listdir(imgdir)))[0]
+        imgpath = os.path.splitext(random.choice(os.listdir(imgdir)))[0]
+        img = images.load(os.path.join(cls.IMGDIR, imgpath))
+
+        if cls.ROTATE:
+            img = rotate(img, 90 * random.randint(0, 3))
 
-        return images.load(os.path.join(cls.IMGDIR, img))
+        return img
 
 class Floor(RandomizedTile):
     IMGDIR = "floor"
@@ -53,6 +60,7 @@ 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):