-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.py
173 lines (134 loc) · 5.22 KB
/
controllers.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
from Framework import message
class MoveController:
"""
A controller class for players' movement in the field
"""
def __init__(self, game) -> None:
"""
Initialize MoveController
:param game: the game being played
"""
self.game = game
def receive_message(self, player_message: message.Message) -> None:
"""
Receive a movement message from MessageCenter
:param player_message: the message to execute
:return: None
"""
direction = player_message.data # the direction of movement
robot = self.game.players[player_message.source] # the robot to control
# cannot find robot position
if robot.get_pos() is None:
print("Cannot find the robot's current position")
return
# check robot state
if not robot.get_state("move"):
robot.receive_info("The robot's movement is interrupted!")
return
x, y = robot.get_pos() # the player's current location
if direction == message.UP:
self.move_player(robot, (x, y - 1))
elif direction == message.DOWN:
self.move_player(robot, (x, y + 1))
elif direction == message.LEFT:
self.move_player(robot, (x - 1, y))
elif direction == message.RIGHT:
self.move_player(robot, (x + 1, y))
def move_player(self, robot, target_pos: tuple) -> None:
"""
Move a player from original_pos to target_pos, and store the movement status
into player's information list
:param robot: the robot to move
:param target_pos: the (x, y) coordinates of the player's destinated location
:return: None
"""
field = self.game.battlefield
original_pos = robot.get_pos()
if field.is_blocked(target_pos[0], target_pos[1]):
robot.receive_info("Movement failed, the location has been blocked")
return
# move the robot
field.get_grid(target_pos[0], target_pos[1]).change_occupant(robot)
field.get_grid(original_pos[0], original_pos[1]).change_occupant(None)
robot.set_pos(field.get_grid(target_pos[0], target_pos[1]))
robot.receive_info("Move to (" + str(robot.get_pos()[0]) + ", " + str(robot.get_pos()[1]) + ")")
# generate sound and heat signal
field.generate_sound(target_pos[0], target_pos[1], robot.move_sound)
field.generate_heat(target_pos[0], target_pos[1], robot.move_heat)
class SensorController:
"""
A controller class for sensors
"""
def __init__(self, game) -> None:
"""
Initialize SensorController
:param game: the game being played
"""
self.game = game
def receive_message(self, player_message: message.Message) -> None:
"""
Receive a sensor message from MessageCenter
:param player_message: the player message to be executed
:return: None
"""
sensor = player_message.data # the sensor object
robot = self.game.players[player_message.source] # the robot to control
# check robot state
if not robot.get_state("sensor"):
robot.receive_info("The robot's sensor is interrupted!")
return
# use the sensor
sensor.detect_signal(self.game.sensors, robot)
# update sensor to robot
robot.sensors[player_message.command] = sensor
class WeaponController:
"""
A controller class for weapons
"""
def __init__(self, game):
"""
Initialize WeaponController
:param game: the game being played
"""
self.game = game
def receive_message(self, player_message: message.Message) -> None:
"""
Receive a weapon message from MessageCenter
:param player_message: the player message to be executed
:return: None
"""
weapon = player_message.data # the weapon object
robot = self.game.players[player_message.source] # the robot to control
# check robot state
if not robot.get_state("weapon"):
robot.receive_info("The robot's weapon is interrupted!")
return
weapon.fire_weapon(self.game.weapons, robot)
# update weapon to robot
robot.weapons[player_message.command] = weapon
class GadgetController:
"""
A controller class for gadgets
"""
def __init__(self, game):
"""
Initialize GadgetController
:param game: the game being played
"""
self.game = game
def receive_message(self, player_message: message.Message) -> None:
"""
Receive a gadget message from MessageCenter
:param player_message: the player message to be executed
:return: None
"""
robot = self.game.players[player_message.source] # the robot to control
gadget = player_message.data # the gadget object
# check robot state
if not robot.get_state("gadget"):
robot.receive_info("The robot's gadget is interrupted!")
return
# use gadget
gadget.use_gadget(self.game.gadgets, robot)
# update gadget to robot
robot.gadgets[player_message.command] = gadget