-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot_gadgets.py
86 lines (72 loc) · 3.16 KB
/
robot_gadgets.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
"""
Handle gadget use
"""
import Framework.message as message
import barricade
from Items import gadgets
from Framework.event import Event
class RobotGadgets:
def __init__(self, game) -> None:
self.game = game
self.battlefield = game.battlefield
self.event_handler = game.event_handler
def deploy_gadget(self, x: int, y: int, gadget: gadgets.DeployableGadget) -> str:
"""
Deploy a deployable gadget at a certain direction from (x, y)
:param x: the x-coordinate of the deployment point
:param y: the y-coordinate of the deployment point
:param gadget: the hard barricade to deploy
:return: result on deployment
"""
px, py = x, y
if gadget.direction == message.UP:
py -= 1
elif gadget.direction == message.DOWN:
py += 1
elif gadget.direction == message.LEFT:
px -= 1
elif gadget.direction == message.RIGHT:
px += 1
return gadget.config.execution_function(gadget, self.battlefield, px, py)
def throw_projectile_gadget(self, x: int, y: int, gadget: gadgets.ProjectileGadget) -> []:
"""
Throw an EMP bomb at a certain direction and range from (x, y)
:param x: the x-coordinate of the starting point
:param y: the y-coordinate of the starting point
:param gadget: the projectile gadget to throw
:return: result on throwing
"""
px, py = x, y
if gadget.direction == message.UP:
py -= gadget.range
elif gadget.direction == message.DOWN:
py += gadget.range
elif gadget.direction == message.LEFT:
px -= gadget.range
elif gadget.direction == message.RIGHT:
px += gadget.range
# generate sound and heat at starting position
self.battlefield.generate_sound(x, y, gadget.config.sound_emission)
self.battlefield.generate_heat(x, y, gadget.config.heat_emission)
# find the targets in range
targets = []
from robot import Robot # temporarily import robot
for i in range(max(0, px - gadget.config.impact_radius), min(len(self.battlefield.field[0]), px + gadget.config.impact_radius + 1)):
for j in range(max(0, py - gadget.config.impact_radius), min(len(self.battlefield.field), py + gadget.config.impact_radius + 1)):
if (i - px) ** 2 + (j - py) ** 2 <= gadget.config.impact_radius ** 2:
occupant = self.battlefield.get_grid(i, j).get_occupant()
if isinstance(occupant, Robot):
targets.append(f"{gadget.config.name} hit " + occupant.get_name() + '!')
# execute the effect of gadget
gadget.execution_function(occupant)
return targets
def use_repair_kit(self, robot, gadget: gadgets.RepairKit) -> str:
"""
Repair the robot
:param robot: the robot to repair
:param gadget: the repair kit to use
:return: result on using
"""
robot.recovery_HP(gadget.config.HP)
robot.recovery_armor(gadget.config.armor)
return "Repair kit used!"