Enemies appear
[koperkapel.git] / koperkapel / loaders / levelloader.py
1 """Loader a level, using the pygame-zero ResourceLoader infrastructure"""
2
3 import json
4
5 from pgzero.loaders import ResourceLoader
6
7 from ..gamelib.tiles import Wall, Floor, Tunnel, Underground, Grate, Exit
8 from ..gamelib.level import Level
9 from ..gamelib.door import Door
10 from ..gamelib.keypad import Keypad
11 from ..gamelib.friendlyroach import get_friendly_roach
12 from ..gamelib.items import get_item
13 from ..gamelib.enemy_generator import EnemyGenerator
14
15
16 TILES = {
17     "cwall": Wall,  # rename this everywhere
18     "floor": Floor,
19     "tunnel": Tunnel,
20     "underground": Underground,
21 }
22
23
24 class LevelLoader(ResourceLoader):
25     """ Level loader. """
26
27     EXTNS = ['json']
28     TYPE = 'level'
29
30     def _load(self, level_path):
31         f = open(level_path, 'r')
32         level_data = json.load(f)
33         f.close()
34         self._level = Level()
35         self._level.height = len(level_data['tiles'])
36         self._level.width = len(level_data['tiles'][0])
37         self._level.tiles = level_data['tiles']
38         self._level.tileset = level_data['tileset']
39         self._level.start_pos = level_data["starting pos"]
40         self._level.exit = level_data["exit"]
41         self._level.enemy_generators = []
42         self._level.friends = []
43         self._level.items = []
44         # Consistency check, so we can assume things are correct
45         # in the level renderer
46         for row, row_data in enumerate(self._level.tiles):
47             if len(row_data) != self._level.width:
48                 raise RuntimeError("Incorrect len for row %d" % row)
49         for tile in TILES.values():
50             tile.TILESET = self._level.tileset
51         self._load_tile_images()
52         for door_info in level_data['door_info']:
53             # Create the doors first
54             doors = []
55             for door in door_info['doors']:
56                 x, y = door
57                 # is this the right check
58                 if self._level.can_walk(x + 1, y, 'floor') and self._level.can_walk(x - 1, y, 'floor'):
59                     # vertical door
60                     angle = 90
61                 else:
62                     angle = 0
63                 new_door = Door(x, y, angle)
64                 self._level.doors.append(new_door)
65                 doors.append(new_door)
66             # Add the keypads
67             for keypad in door_info['keypads']:
68                 new_keypad = Keypad(keypad[0], keypad[1], doors)
69                 self._level.keypads.append(new_keypad)
70         for pos in level_data['friendly roaches']:
71             roach = get_friendly_roach(pos[0], pos[1])
72             self._level.friends.append(roach)
73         for item in level_data['items']:
74             self._level.items.append(get_item(item))
75         for generator in level_data['enemy generators']:
76             enemy = EnemyGenerator(generator, self._level)
77             self._level.enemy_generators.append(enemy)
78         return self._level
79
80     def _load_tile_images(self):
81         """Load all the tile images"""
82         for y, row_data in enumerate(self._level.tiles):
83             for x, tile in enumerate(row_data):
84                 neighborhood = self._level.get_neighbors(x, y)
85                 for layer in ['floor', 'tunnels']:
86                     neighbors = [x[layer] if x else None for x in neighborhood]
87                     tile['%s image' % layer] = (
88                             TILES[tile[layer]['base']].image(neighbors))
89                     # Hack this for now
90                     if self._level.is_exit(x, y):
91                         tile['floor image'] = Exit.image(neighbors)
92                     elif layer == 'floor' and 'crawl' in tile[layer]['behaviour']:
93                         tile['floor image'] = Grate.image(neighbors)
94                         self._level.grates.append((x, y))
95
96
97 levels = LevelLoader('levels')