The first steps towards enemy generators
[koperkapel.git] / koperkapel / loaders / levelloader.py
index a4b5b6f8dcaa12d7b6d0efeccc75168dd9949df8..a775838dc54b1b02c18734ab69709d4a34c16230 100644 (file)
 """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
+from pgzero.loaders import ResourceLoader
 
-class Tile:
-    IMG = None
-    TILESET = None
+from ..gamelib.tiles import Wall, Floor, Tunnel, Underground, Grate, Exit
+from ..gamelib.level import Level
+from ..gamelib.door import Door
+from ..gamelib.keypad import Keypad
+from ..gamelib.friendlyroach import get_friendly_roach
+from ..gamelib.items import get_item
+from ..gamelib.enemy_generator import EnemyGenerator
 
-    @classmethod
-    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))
-
-class OrientatedTile(Tile):
-    ANGLE = None
-
-    @classmethod
-    def image(cls, neighbors):
-        if cls.IMG is None or cls.TILESET is None:
-            raise NotImplementedError()
-        img = images.load(os.path.join(cls.TILESET, cls.IMG))
-        if cls.ANGLE:
-            img = rotate(img, cls.ANGLE)
-        return img
-
-
-class RandomizedTile(Tile):
-    IMGDIR = None
-    TILESET = None
-    ROTATE = True
-
-    @classmethod
-    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.TILESET, cls.IMGDIR)
-        imgpath = os.path.splitext(random.choice(os.listdir(imgdir)))[0]
-        img = images.load(os.path.join(cls.TILESET, cls.IMGDIR, imgpath))
-
-        if cls.ROTATE:
-            img = rotate(img, 90 * random.randint(0, 3))
-
-        return img
-
-class Floor(RandomizedTile):
-    IMGDIR = "floor"
-
-class Wall(RandomizedTile):
-    IMGDIR = "wall"
-
-class Underground(RandomizedTile):
-    IMGDIR = "underground"
-
-class Tunnel(OrientatedTile):
-
-    @classmethod
-    def image(cls, neighbors):
-        connections = [True if 'walk' in x['behaviour'] else False for x in neighbors]
-        conn_count = connections.count(True)
-        # simple cases
-        cls.ANGLE = 0
-        if conn_count == 0:
-            # return single point tunnel
-            cls.IMG = os.path.join('tunnel', 'tunnel_none')
-        elif conn_count == 4:
-            # crossroads
-            cls.IMG = os.path.join('tunnel', 'tunnel_crossroads')
-        elif conn_count == 1:
-            # 1 point connector, roatated correctly
-            cls.IMG = os.path.join('tunnel', 'tunnel_1way')
-            # because of the ordering of neighbors, we use this formulation
-            for x, angle in zip(connections, (90, 270, 0, 180)):
-                if x:
-                    cls.ANGLE = angle
-                    break
-        elif conn_count == 3:
-            # 3 point connector, rotated correctly
-            cls.IMG = os.path.join('tunnel', 'tunnel_3way')
-            # find the missing connection.
-            for x, angle in zip(connections, (0, 180, 270, 90)):
-                if not x:
-                    cls.ANGLE = angle
-                    break
-        elif conn_count == 2:
-            # Need to distinguish pass-through or corner, and
-            # rotate correctly
-            # neighbors is left, right then up, down
-            if connections[0] == connections[1]:
-                cls.IMG = os.path.join('tunnel', 'tunnel_passthrough')
-                if connections[0]:
-                    cls.ANGLE = 90
-            else:
-                cls.IMG = os.path.join('tunnel', 'tunnel_corner')
-                if connections[0]:
-                    if connections[2]:
-                        # left, up
-                        cls.ANGLE = 90
-                    else:
-                        # left, down
-                        cls.ANGLE = 180
-                else:
-                    if connections[2]:
-                        # right, up
-                        cls.ANGLE = 0
-                    else:
-                        # right, down
-                        cls.ANGLE = 270
-
-        return super(Tunnel, cls).image(neighbors)
-        
 
 TILES = {
-    "cwall": Wall, # rename this everywhere
+    "cwall": Wall,  # rename this everywhere
     "floor": Floor,
     "tunnel": Tunnel,
     "underground": Underground,
 }
 
+
 class LevelLoader(ResourceLoader):
     """ Level loader. """
 
@@ -135,37 +31,67 @@ class LevelLoader(ResourceLoader):
         f = open(level_path, 'r')
         level_data = json.load(f)
         f.close()
-        self._height = len(level_data['tiles'])
-        self._width = len(level_data['tiles'][0])
-        self._tiles = level_data['tiles']
-        self._tileset = level_data['tileset']
+        self._level = Level()
+        self._level.height = len(level_data['tiles'])
+        self._level.width = len(level_data['tiles'][0])
+        self._level.tiles = level_data['tiles']
+        self._level.tileset = level_data['tileset']
+        self._level.start_pos = level_data["starting pos"]
+        self._level.exit = level_data["exit"]
+        self._level.enemy_generators = []
+        self._level.friends = []
+        self._level.items = []
         # 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:
+        for row, row_data in enumerate(self._level.tiles):
+            if len(row_data) != self._level.width:
                 raise RuntimeError("Incorrect len for row %d" % row)
         for tile in TILES.values():
-            tile.TILESET = self._tileset
+            tile.TILESET = self._level.tileset
         self._load_tile_images()
-        return level_data
+        for door_info in level_data['door_info']:
+            # Create the doors first
+            doors = []
+            for door in door_info['doors']:
+                x, y = door
+                # is this the right check
+                if self._level.can_walk(x + 1, y, 'floor') and self._level.can_walk(x - 1, y, 'floor'):
+                    # vertical door
+                    angle = 90
+                else:
+                    angle = 0
+                new_door = Door(x, y, angle)
+                self._level.doors.append(new_door)
+                doors.append(new_door)
+            # Add the keypads
+            for keypad in door_info['keypads']:
+                new_keypad = Keypad(keypad[0], keypad[1], doors)
+                self._level.keypads.append(new_keypad)
+        for pos in level_data['friendly roaches']:
+            roach = get_friendly_roach(pos[0], pos[1])
+            self._level.friends.append(roach)
+        for item in level_data['items']:
+            self._level.items.append(get_item(item))
+        for generator in level_data['enemy generators']:
+            enemy = EnemyGenerator(generator)
+            self._level.enemy_generators.append(enemy)
+        return self._level
 
     def _load_tile_images(self):
         """Load all the tile images"""
-        height = len(self._tiles)
-        width = len(self._tiles[0])
-        for y, row_data in enumerate(self._tiles):
+        for y, row_data in enumerate(self._level.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,
-                               ]
+                neighborhood = self._level.get_neighbors(x, y)
                 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)
+                    tile['%s image' % layer] = (
+                            TILES[tile[layer]['base']].image(neighbors))
+                    # Hack this for now
+                    if self._level.is_exit(x, y):
+                        tile['floor image'] = Exit.image(neighbors)
+                    elif layer == 'floor' and 'crawl' in tile[layer]['behaviour']:
+                        tile['floor image'] = Grate.image(neighbors)
+                        self._level.grates.append((x, y))
 
 
 levels = LevelLoader('levels')