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 is_keypad(self, x, y):
50         for keypad in self.keypads:
51             if (x, y) == keypad.game_pos:
52                 return True
53         return False
54
55     def is_grate(self, x, y):
56         if (x, y) in self.grates:
57             return True
58         return False
59
60     def press_keypad(self, x, y, roaches):
61         for keypad in self.keypads:
62             if (x, y) == keypad.game_pos:
63                 keypad.activate(roaches)
64
65     def get_friends(self):
66         return self._friends[:]
67
68     def friend_at(self, x, y):
69         for f in self.friends:
70             if f.game_pos == (x, y):
71                 return f
72
73     def remove_friend(self, friend):
74         self.friends.remove(friend)
75
76     def item_at(self, x, y):
77         for i in self.items:
78             if i.game_pos == (x, y):
79                 return i
80
81     def remove_item(self, item):
82         self.items.remove(item)
83
84     def is_exit(self, x, y):
85         return self.exit and (x, y) == tuple(self.exit["pos"])
86
87     def get_exit_level(self):
88         return self.exit["next level"]
89
90     def enemy_at(self, x, y):
91         for enemy in self.enemies:
92             if (x, y) == enemy.game_pos:
93                 return True
94         return False
95
96     def get_enemy(self, x, y):
97         for enemy in self.enemies:
98             if (x, y) == enemy.game_pos:
99                 return enemy
100         return None
101
102     def remove_enemy(self, enemy):
103         self.enemies.remove(enemy)
104         for generator in self.enemy_generators:
105             generator.killed(enemy)
106
107     def add_enemy(self, enemy, x, y):
108         """Add an enemy to an empty floor space near x, y"""
109         added = False
110         while not added:
111             if self.can_walk(x, y, 'floor'):
112                 if not self.friend_at(x, y):
113                     if not self.enemy_at(x, y):
114                         added = True
115                         enemy.game_pos = (x, y)
116                         self.enemies.append(enemy)
117                         added = True
118             x += random.randint(-1, 1)
119             y += random.randint(-1, 1)