decoupled tiles from tile images
authoradrianna <adrianna.pinska@gmail.com>
Wed, 2 Mar 2016 22:09:21 +0000 (00:09 +0200)
committeradrianna <adrianna.pinska@gmail.com>
Wed, 2 Mar 2016 22:09:30 +0000 (00:09 +0200)
koperkapel/loaders/levelloader.py

index 124161f37d12fe93dd9681471bf140cced3a3ef5..640d9b168ada4e59073559058cb8654d72492fb5 100644 (file)
@@ -3,7 +3,42 @@
 import json
 
 from pgzero.loaders import images, ResourceLoader
+import os
+import random
 
+class Tile:
+    IMG = None
+
+    @classmethod
+    def image(cls):
+        if cls.IMG is None:
+            raise NotImplementedError()
+
+        return images.load(cls.IMG)
+
+class RandomizedTile(Tile):
+    IMGDIR = None
+
+    @classmethod
+    def image(cls):
+        if cls.IMGDIR is None:
+            raise NotImplementedError()
+
+        imgdir = os.path.join(os.path.dirname(__file__), '..', 'images', cls.IMGDIR)
+        img = os.path.splitext(random.choice(os.listdir(imgdir)))[0]
+
+        return images.load(os.path.join(cls.IMGDIR, img))
+
+class Floor(RandomizedTile):
+    IMGDIR = "floor"
+
+class Wall(RandomizedTile):
+    IMGDIR = "wall"
+
+TILES = {
+    "cwall": Wall, # rename this everywhere
+    "floor": Floor,
+}
 
 class LevelLoader(ResourceLoader):
     """ Level loader. """
@@ -30,7 +65,7 @@ class LevelLoader(ResourceLoader):
         """Load all the tile images"""
         for row_data in self._tiles:
             for tile in row_data:
-                tile['image'] = images.load(tile['base'])
+                tile['image'] = TILES[tile['base']].image()
 
 
 levels = LevelLoader('levels')