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.AdvancedRobot;
12 import robocode.Condition;
13 import robocode.CustomEvent;
14 
15 import java.awt.*;
16 
17 
18 /**
19  * Target - a sample robot by Mathew Nelson.
20  * <p/>
21  * Sits still. Moves every time energy drops by 20.
22  * This Robot demonstrates custom events.
23  *
24  * @author Mathew A. Nelson (original)
25  * @author Flemming N. Larsen (contributor)
26  */
27 public class Target extends AdvancedRobot {
28 
29 	int trigger; // Keeps track of when to move
30 
31 	/**
32 	 * TrackFire's run method
33 	 */
run()34 	public void run() {
35 		// Set colors
36 		setBodyColor(Color.white);
37 		setGunColor(Color.white);
38 		setRadarColor(Color.white);
39 
40 		// Initially, we'll move when life hits 80
41 		trigger = 80;
42 		// Add a custom event named "trigger hit",
43 		addCustomEvent(new Condition("triggerhit") {
44 			public boolean test() {
45 				return (getEnergy() <= trigger);
46 			}
47 		});
48 	}
49 
50 	/**
51 	 * onCustomEvent handler
52 	 */
onCustomEvent(CustomEvent e)53 	public void onCustomEvent(CustomEvent e) {
54 		// If our custom event "triggerhit" went off,
55 		if (e.getCondition().getName().equals("triggerhit")) {
56 			// Adjust the trigger value, or
57 			// else the event will fire again and again and again...
58 			trigger -= 20;
59 			out.println("Ouch, down to " + (int) (getEnergy() + .5) + " energy.");
60 			// move around a bit.
61 			turnLeft(65);
62 			ahead(100);
63 		}
64 	}
65 }
66