Merge branch 'master' of ctpug.org.za:koperkapel
[koperkapel.git] / koperkapel / gamelib / level.py
1 """ Class holding the level info """
2
3 from .keypad import Keypad
4 from .door import Door
5
6
7 class Level(object):
8
9     def __init__(self):
10         self.width = self.height = 0
11         self.tiles = []
12         self.keypads = []
13         self.doors = []
14         self.grates = []
15         self.tileset = None
16         self.start_pos = (0, 0)
17
18     def get_neighbors(self, x, y):
19         # 4 -connected neighbors
20         return [self.tiles[y][x-1] if x > 0 else None,
21                 self.tiles[y][x+1] if x < self.width - 1 else None,
22                 self.tiles[y-1][x] if y > 0 else None,
23                 self.tiles[y+1][x] if y < self.height- 1 else None,
24                ]
25
26     def can_walk(self, x, y, layer):
27         if 'walk' in self.tiles[y][x][layer]['behaviour']:
28             # check doors
29             for door in self.doors:
30                 if (x, y) == door.game_pos and door.is_closed():
31                     return False
32             return True
33         return False
34
35     def can_fly(self, x, y, layer):
36         if 'fly' in self.tiles[y][x][layer]['behaviour']:
37             for door in self.doors:
38                 if (x, y) == door.game_pos and door.is_closed():
39                     return False
40             return True
41
42         return False
43
44     def can_crawl(self, x, y, layer):
45         return 'crawl' in self.tiles[y][x][layer]['behaviour']
46
47     def is_keypad(self, x, y):
48         for keypad in self.keypads:
49             if (x, y) == keypad.game_pos:
50                 return True
51         return False
52
53     def is_grate(self, x, y):
54         if (x, y) in self.grates:
55             return True
56         return False
57
58     def press_keypad(self, x, y, roaches):
59         for keypad in self.keypads:
60             if (x, y) == keypad.game_pos:
61                 keypad.activate(roaches)