-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFire.java
84 lines (72 loc) · 1.95 KB
/
Fire.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
81
82
83
84
/**
* 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.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import java.awt.*;
/**
* Fire - a sample robot by Mathew Nelson, and maintained.
* <p/>
* Sits still. Spins gun around. Moves when hit.
*
* @author Mathew A. Nelson (original)
* @author Flemming N. Larsen (contributor)
*/
public class Fire extends Robot {
int dist = 50; // distance to move when we're hit
/**
* run: Fire's main run function
*/
public void run() {
// Set colors
setBodyColor(Color.orange);
setGunColor(Color.orange);
setRadarColor(Color.red);
setScanColor(Color.red);
setBulletColor(Color.red);
// Spin the gun around slowly... forever
while (true) {
turnGunRight(5);
}
}
/**
* onScannedRobot: Fire!
*/
public void onScannedRobot(ScannedRobotEvent e) {
// If the other robot is close by, and we have plenty of life,
// fire hard!
if (e.getDistance() < 50 && getEnergy() > 50) {
fire(3);
} // otherwise, fire 1.
else {
fire(1);
}
// Call scan again, before we turn the gun
scan();
}
/**
* onHitByBullet: Turn perpendicular to the bullet, and move a bit.
*/
public void onHitByBullet(HitByBulletEvent e) {
turnRight(normalRelativeAngleDegrees(90 - (getHeading() - e.getHeading())));
ahead(dist);
dist *= -1;
scan();
}
/**
* onHitRobot: Aim at it. Fire Hard!
*/
public void onHitRobot(HitRobotEvent e) {
double turnGunAmt = normalRelativeAngleDegrees(e.getBearing() + getHeading() - getGunHeading());
turnGunRight(turnGunAmt);
fire(3);
}
}