Merge branch 'master' of ctpug.org.za:koperkapel
[koperkapel.git] / koperkapel / gamelib / level.py
1 """ Class holding the level info """
2
3 import random
4
5
6 class Level(object):
7
8     def __init__(self):
9         self.width = self.height = 0
10         self.tiles = []
11         self.keypads = []
12         self.doors = []
13         self.grates = []
14         self.tileset = None
15         self.start_pos = (0, 0)
16         self.exit = None
17         self.enemies = []
18         self.enemy_generators = []
19         self.friends = []
20
21     def get_neighbors(self, x, y):
22         # 4 -connected neighbors
23         return [self.tiles[y][x-1] if x > 0 else None,
24                 self.tiles[y][x+1] if x < self.width - 1 else None,
25                 self.tiles[y-1][x] if y > 0 else None,
26                 self.tiles[y+1][x] if y < self.height - 1 else None]
27
28     def can_walk(self, x, y, layer):
29         if 'walk' in self.tiles[y][x][layer]['behaviour']:
30             # check doors
31             for door in self.doors:
32                 if (x, y) == door.game_pos and door.is_closed():
33                     return False
34             return True
35         return False
36
37     def can_fly(self, x, y, layer):
38         if 'fly' in self.tiles[y][x][layer]['behaviour']:
39             for door in self.doors:
40                 if (x, y) == door.game_pos and door.is_closed():
41                     return False
42             return True
43
44         return False
45
46     def can_crawl(self, x, y, layer):
47         return 'crawl' in self.tiles[y][x][layer]['behaviour']
48
49     def can_bullet(self, x, y, layer):
50         if self.tiles[y][x][layer]['behaviour']:
51             for door in self.doors:
52                 if (x, y) == door.game_pos and door.is_closed():
53                     return False
54             return True
55         return False
56
57     def is_keypad(self, x, y):
58         for keypad in self.keypads:
59             if (x, y) == keypad.game_pos:
60                 return True
61         return False
62
63     def is_grate(self, x, y):
64         if (x, y) in self.grates:
65             return True
66         return False
67
68     def press_keypad(self, x, y, roaches):
69         for keypad in self.keypads:
70             if (x, y) == keypad.game_pos:
71                 keypad.activate(roaches)
72
73     def get_friends(self):
74         return self._friends[:]
75
76     def friend_at(self, x, y):
77         for f in self.friends:
78             if f.game_pos == (x, y):
79                 return f
80
81     def remove_friend(self, friend):
82         self.friends.remove(friend)
83
84     def item_at(self, x, y):
85         for i in self.items:
86             if i.game_pos == (x, y):
87                 return i
88
89     def remove_item(self, item):
90         self.items.remove(item)
91
92     def is_exit(self, x, y):
93         return self.exit and (x, y) == tuple(self.exit["pos"])
94
95     def get_exit_level(self):
96         return self.exit["next level"]
97
98     def enemy_at(self, x, y):
99         for enemy in self.enemies:
100             if (x, y) == enemy.game_pos:
101                 return True
102         return False
103
104     def get_enemy(self, x, y):
105         for enemy in self.enemies:
106             if (x, y) == enemy.game_pos:
107                 return enemy
108         return None
109
110     def remove_enemy(self, enemy):
111         self.enemies.remove(enemy)
112         for generator in self.enemy_generators:
113             generator.killed(enemy)
114
115     def add_enemy(self, enemy, x, y):
116         """Add an enemy to an empty floor space near x, y"""
117         added = False
118         while not added:
119             if self.can_walk(x, y, 'floor'):
120                 if not self.friend_at(x, y):
121                     if not self.enemy_at(x, y):
122                         added = True
123                         enemy.game_pos = (x, y)
124                         self.enemies.append(enemy)
125                         added = True
126             x += random.randint(-1, 1)
127             y += random.randint(-1, 1)