X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=koperkapel%2Floaders%2Flevelloader.py;h=a8aaa376efba9be55b7af1a7f7c70fab8ef8df18;hb=80836416c2f1ff13a7824d26f2e6b805b2107950;hp=ff18fc7b8e0548678dc172dd65db893fe33853fc;hpb=815ef3fcfed6bb2e23d5d61c02542535f2d3c303;p=koperkapel.git diff --git a/koperkapel/loaders/levelloader.py b/koperkapel/loaders/levelloader.py index ff18fc7..a8aaa37 100644 --- a/koperkapel/loaders/levelloader.py +++ b/koperkapel/loaders/levelloader.py @@ -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: + 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: + 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,17 @@ class Floor(RandomizedTile): class Wall(RandomizedTile): IMGDIR = "wall" +class Underground(RandomizedTile): + IMGDIR = "wall" + +class Tunnel(RandomizedTile): + IMGDIR = "floor" + TILES = { "cwall": Wall, # rename this everywhere "floor": Floor, + "tunnel": Tunnel, + "underground": Underground, } class LevelLoader(ResourceLoader): @@ -59,11 +70,14 @@ 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 @@ -71,7 +85,8 @@ class LevelLoader(ResourceLoader): """Load all the tile images""" for row_data in self._tiles: for tile in row_data: - tile['image'] = TILES[tile['base']].image() + for layer in ['floor', 'tunnels']: + tile['%s image' % layer] = TILES[tile[layer]['base']].image() levels = LevelLoader('levels')