-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchild_classes.py
195 lines (139 loc) · 5.64 KB
/
child_classes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/python3
from logic import *
import random
from queue import Queue
LONG_RANGE_EXPLOSION_RADIUS = 5
class SimpleMonster(Monster):
def __init__(self):
super().__init__()
self.object_types = [Block, Bomb, Bonus]
self.direction = None
def move(self, coordinates: 'Point', old_map: 'Map'):
if self.direction and self._can_move(old_map,
coordinates +
self.direction.value):
return Move(self.direction)
directions = [Direction.Up, Direction.Down,
Direction.Left, Direction.Right]
random.shuffle(directions)
for direction in directions:
if self._can_move(old_map, coordinates + direction.value):
self.direction = direction
return Move(direction)
self.direction = None
return Move(Direction.Stand)
def can_move(self, collisions, old_collisions):
return self._correct_collisions_to_move(collisions)
def _can_move(self, game_map, location):
collisions = game_map.get_collisions(location)
return self._correct_collisions_to_move(collisions)
def _correct_collisions_to_move(self, collisions):
return all(not isinstance(collision, type_)
for collision in collisions
for type_ in self.object_types)
class CleverMonster(SimpleMonster):
VISION_RANGE = 10
def __init__(self):
super().__init__()
self._next_point = None
self._player_position = None
def in_vision_range(self, my_coordinates, object_coordinates):
return abs(my_coordinates.x - object_coordinates.x) < \
self.VISION_RANGE * CELL_SIZE and \
abs(my_coordinates.y - object_coordinates.y) < \
self.VISION_RANGE * CELL_SIZE
def move(self, coordinates: 'Point', old_map: 'Map'):
if self._next_point is not None and self._next_point != coordinates:
return self._switch_action(self._next_point - coordinates)
visited = set()
rounded_coordinates = Map.round_point(coordinates, CELL_SIZE)
queue = Queue()
queue.put(rounded_coordinates)
track = {}
player_position = None
while not queue.empty():
if player_position is not None:
break
node = queue.get()
for direction in (Direction.Down, Direction.Up,
Direction.Left, Direction.Right):
new_node = node + (direction.value * CELL_SIZE)
collisions = old_map.get_collisions(new_node)
if new_node not in visited and \
self.in_vision_range(coordinates, new_node) and \
self._correct_collisions_to_move(collisions):
visited.add(new_node)
queue.put(new_node)
track[new_node] = node
if any(isinstance(collision, Player)
for collision in collisions):
player_position = new_node
break
if player_position is None:
self._next_point = None
return Move(Direction.Stand)
else:
next_point = player_position
points = []
while track[next_point] != rounded_coordinates:
next_point = track[next_point]
points.append(next_point)
direction = next_point - coordinates
self._next_point = next_point
self._player_position = player_position
return self._switch_action(direction)
def _switch_action(self, direction):
if direction.x > 0:
return Move(Direction.Right)
elif direction.x < 0:
return Move(Direction.Left)
elif direction.y > 0:
return Move(Direction.Up)
elif direction.y < 0:
return Move(Direction.Down)
else:
return Move(Direction.Stand)
class StrongMonster(SimpleMonster):
def solve_collision(self, other_objects):
if any(isinstance(object_, HighPoweredExplosion)
for object_ in other_objects):
self._is_dead = True
class UnbreakableBlock(Block):
pass
class FortifiedBlock(Block):
def solve_collision(self, other_objects):
if any(isinstance(object_, HighPoweredExplosion)
for object_ in other_objects):
self._is_dead = True
class DestroyableBlock(Block):
def solve_collision(self, other_objects):
if any(isinstance(object_, ExplosionBlock)
for object_ in other_objects):
self._is_dead = True
class SimpleBomb(Bomb):
pass
class HighPowerBomb(Bomb):
def __init__(self, tick_delay, explosion_radius):
super().__init__(tick_delay, explosion_radius)
self._explosion_type = HighPoweredExplosion
class HighPoweredExplosion(ExplosionBlock):
pass
class HighBombBonus(Bonus):
def add_bonus(self, player):
player.set_bomb_type(HighPowerBomb)
class LongExplosionBonus(Bonus):
def add_bonus(self, player):
player.add_buff(LongRangeExplosionBuff())
class ImmuneBonus(Bonus):
def add_bonus(self, player):
player.add_buff(ImmuneBuff())
class ImmuneBuff(Buff):
def start(self, player):
player.immune = True
def end(self, player):
player.immune = False
class LongRangeExplosionBuff(Buff):
def start(self, player):
player.set_bomb_radius(LONG_RANGE_EXPLOSION_RADIUS)
def end(self, player):
player.set_bomb_radius(START_EXPLOSION_RADIUS)