1 | /** |
---|
2 | * Copyright (c) 2001-2014 Mathew A. Nelson and Robocode contributors |
---|
3 | * All rights reserved. This program and the accompanying materials |
---|
4 | * are made available under the terms of the Eclipse Public License v1.0 |
---|
5 | * which accompanies this distribution, and is available at |
---|
6 | * http://robocode.sourceforge.net/license/epl-v10.html |
---|
7 | */ |
---|
8 | package sample; |
---|
9 | |
---|
10 | |
---|
11 | import robocode.HitByBulletEvent; |
---|
12 | import robocode.HitRobotEvent; |
---|
13 | import robocode.Robot; |
---|
14 | import robocode.ScannedRobotEvent; |
---|
15 | import static robocode.util.Utils.normalRelativeAngleDegrees; |
---|
16 | |
---|
17 | import java.awt.*; |
---|
18 | |
---|
19 | |
---|
20 | /** |
---|
21 | * Fire - a sample robot by Mathew Nelson, and maintained. |
---|
22 | * <p/> |
---|
23 | * Sits still. Spins gun around. Moves when hit. |
---|
24 | * |
---|
25 | * @author Mathew A. Nelson (original) |
---|
26 | * @author Flemming N. Larsen (contributor) |
---|
27 | */ |
---|
28 | public class Fire extends Robot { |
---|
29 | int dist = 50; // distance to move when we're hit |
---|
30 | |
---|
31 | /** |
---|
32 | * run: Fire's main run function |
---|
33 | */ |
---|
34 | public void run() { |
---|
35 | // Set colors |
---|
36 | setBodyColor(Color.orange); |
---|
37 | setGunColor(Color.orange); |
---|
38 | setRadarColor(Color.red); |
---|
39 | setScanColor(Color.red); |
---|
40 | setBulletColor(Color.red); |
---|
41 | |
---|
42 | // Spin the gun around slowly... forever |
---|
43 | while (true) { |
---|
44 | turnGunRight(5); |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * onScannedRobot: Fire! |
---|
50 | */ |
---|
51 | public void onScannedRobot(ScannedRobotEvent e) { |
---|
52 | // If the other robot is close by, and we have plenty of life, |
---|
53 | // fire hard! |
---|
54 | if (e.getDistance() < 50 && getEnergy() > 50) { |
---|
55 | fire(3); |
---|
56 | } // otherwise, fire 1. |
---|
57 | else { |
---|
58 | fire(1); |
---|
59 | } |
---|
60 | // Call scan again, before we turn the gun |
---|
61 | scan(); |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * onHitByBullet: Turn perpendicular to the bullet, and move a bit. |
---|
66 | */ |
---|
67 | public void onHitByBullet(HitByBulletEvent e) { |
---|
68 | turnRight(normalRelativeAngleDegrees(90 - (getHeading() - e.getHeading()))); |
---|
69 | |
---|
70 | ahead(dist); |
---|
71 | dist *= -1; |
---|
72 | scan(); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * onHitRobot: Aim at it. Fire Hard! |
---|
77 | */ |
---|
78 | public void onHitRobot(HitRobotEvent e) { |
---|
79 | double turnGunAmt = normalRelativeAngleDegrees(e.getBearing() + getHeading() - getGunHeading()); |
---|
80 | |
---|
81 | turnGunRight(turnGunAmt); |
---|
82 | fire(3); |
---|
83 | } |
---|
84 | } |
---|