-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrackFire.java
80 lines (70 loc) · 2.13 KB
/
TrackFire.java
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
/**
* Copyright (c) 2001-2014 Mathew A. Nelson and Robocode contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://robocode.sourceforge.net/license/epl-v10.html
*/
package sample;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import robocode.WinEvent;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import java.awt.*;
/**
* TrackFire - a sample robot by Mathew Nelson.
* <p/>
* Sits still. Tracks and fires at the nearest robot it sees.
*
* @author Mathew A. Nelson (original)
* @author Flemming N. Larsen (contributor)
*/
public class TrackFire extends Robot {
/**
* TrackFire's run method
*/
public void run() {
// Set colors
setBodyColor(Color.pink);
setGunColor(Color.pink);
setRadarColor(Color.pink);
setScanColor(Color.pink);
setBulletColor(Color.pink);
// Loop forever
while (true) {
turnGunRight(10); // Scans automatically
}
}
/**
* onScannedRobot: We have a target. Go get it.
*/
public void onScannedRobot(ScannedRobotEvent e) {
// Calculate exact location of the robot
double absoluteBearing = getHeading() + e.getBearing();
double bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - getGunHeading());
// If it's close enough, fire!
if (Math.abs(bearingFromGun) <= 3) {
turnGunRight(bearingFromGun);
// We check gun heat here, because calling fire()
// uses a turn, which could cause us to lose track
// of the other robot.
if (getGunHeat() == 0) {
fire(Math.min(3 - Math.abs(bearingFromGun), getEnergy() - .1));
}
} // otherwise just set the gun to turn.
// Note: This will have no effect until we call scan()
else {
turnGunRight(bearingFromGun);
}
// Generates another scan event if we see a robot.
// We only need to call this if the gun (and therefore radar)
// are not turning. Otherwise, scan is called automatically.
if (bearingFromGun == 0) {
scan();
}
}
public void onWin(WinEvent e) {
// Victory dance
turnRight(36000);
}
}