-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRadar.java
More file actions
71 lines (56 loc) · 1.5 KB
/
Radar.java
File metadata and controls
71 lines (56 loc) · 1.5 KB
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
package ubots;
import robocode.AdvancedRobot;
import robocode.RobotDeathEvent;
import robocode.ScannedRobotEvent;
public class Radar {
private static final int TIMES_TO_SPIN = 30;
private static final int ANGLE = 30;
private byte direction = 1;
private AdvancedRobot robot;
private EnemyInfo enemy;
public void init() {
robot.setAdjustRadarForGunTurn(true);
}
public Radar(AdvancedRobot robot, EnemyInfo enemy) {
this.robot = robot;
this.enemy = enemy;
}
public void scan() {
if (enemy.isNotScanned()) {
robot.setTurnRadarRight(360 * TIMES_TO_SPIN);
} else {
robot.setTurnRadarRight(getAngleToTurn());
changeDirection();
}
}
public void onScannedRobot(ScannedRobotEvent e) {
if (shouldTrack(e)) {
enemy.update(e, robot);
}
}
public void onRobotDeath(RobotDeathEvent e) {
if (wasTracking(e)) {
enemy.reset();
}
}
public boolean shouldTrack(ScannedRobotEvent e) {
return (enemy.isNotScanned() || isNear(e) || isCurrentTarget(e));
}
private boolean isCurrentTarget(ScannedRobotEvent e) {
return e.getName().equals(enemy.getName());
}
private boolean isNear(ScannedRobotEvent e) {
return e.getDistance() < enemy.getDistance() - 70;
}
public boolean wasTracking(RobotDeathEvent e) {
return (e.getName().equals(enemy.getName()));
}
private void changeDirection() {
direction *= -1;
}
private double getAngleToTurn() {
double turn = robot.getHeading() - robot.getRadarHeading() + enemy.getBearing();
turn += ANGLE * direction;
return turn;
}
}