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 sampleteam;
9 
10 
11 import robocode.Droid;
12 import robocode.MessageEvent;
13 import robocode.TeamRobot;
14 import static robocode.util.Utils.normalRelativeAngleDegrees;
15 
16 
17 /**
18  * SimpleDroid - a sample robot by Mathew Nelson.
19  * <p/>
20  * Follows orders of team leader.
21  *
22  * @author Mathew A. Nelson (original)
23  * @author Flemming N. Larsen (contributor)
24  */
25 public class MyFirstDroid extends TeamRobot implements Droid {
26 
27 	/**
28 	 * run:  Droid's default behavior
29 	 */
run()30 	public void run() {
31 		out.println("MyFirstDroid ready.");
32 	}
33 
34 	/**
35 	 * onMessageReceived:  What to do when our leader sends a message
36 	 */
onMessageReceived(MessageEvent e)37 	public void onMessageReceived(MessageEvent e) {
38 		// Fire at a point
39 		if (e.getMessage() instanceof Point) {
40 			Point p = (Point) e.getMessage();
41 			// Calculate x and y to target
42 			double dx = p.getX() - this.getX();
43 			double dy = p.getY() - this.getY();
44 			// Calculate angle to target
45 			double theta = Math.toDegrees(Math.atan2(dx, dy));
46 
47 			// Turn gun to target
48 			turnGunRight(normalRelativeAngleDegrees(theta - getGunHeading()));
49 			// Fire hard!
50 			fire(3);
51 		} // Set our colors
52 		else if (e.getMessage() instanceof RobotColors) {
53 			RobotColors c = (RobotColors) e.getMessage();
54 
55 			setBodyColor(c.bodyColor);
56 			setGunColor(c.gunColor);
57 			setRadarColor(c.radarColor);
58 			setScanColor(c.scanColor);
59 			setBulletColor(c.bulletColor);
60 		}
61 	}
62 }
63