The first steps towards enemy generators
[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         self.exit = None
18         self.enemies = []
19         self.enemy_generators = []
20         self.friends = []
21
22     def get_neighbors(self, x, y):
23         # 4 -connected neighbors
24         return [self.tiles[y][x-1] if x > 0 else None,
25                 self.tiles[y][x+1] if x < self.width - 1 else None,
26                 self.tiles[y-1][x] if y > 0 else None,
27                 self.tiles[y+1][x] if y < self.height- 1 else None,
28                ]
29
30     def can_walk(self, x, y, layer):
31         if 'walk' in self.tiles[y][x][layer]['behaviour']:
32             # check doors
33             for door in self.doors:
34                 if (x, y) == door.game_pos and door.is_closed():
35                     return False
36             return True
37         return False
38
39     def can_fly(self, x, y, layer):
40         if 'fly' in self.tiles[y][x][layer]['behaviour']:
41             for door in self.doors:
42                 if (x, y) == door.game_pos and door.is_closed():
43                     return False
44             return True
45
46         return False
47
48     def can_crawl(self, x, y, layer):
49         return 'crawl' in self.tiles[y][x][layer]['behaviour']
50
51     def is_keypad(self, x, y):
52         for keypad in self.keypads:
53             if (x, y) == keypad.game_pos:
54                 return True
55         return False
56
57     def is_grate(self, x, y):
58         if (x, y) in self.grates:
59             return True
60         return False
61
62     def press_keypad(self, x, y, roaches):
63         for keypad in self.keypads:
64             if (x, y) == keypad.game_pos:
65                 keypad.activate(roaches)
66
67     def get_friends(self):
68         return self._friends[:]
69
70     def is_on_friend(self, x, y):
71         return (x, y) in [x.game_pos for x in self.friends]
72
73     def remove_friend(self, x, y):
74         for friend in self.friends[:]:
75             if friend.game_pos == (x, y):
76                 self.friends.remove(friend)
77                 return friend
78         return None
79
80     def is_exit(self, x, y):
81         return self.exit and (x, y) == tuple(self.exit["pos"])
82
83     def get_exit_level(self):
84         return self.exit["next level"]