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 static robocode.util.Utils.normalAbsoluteAngle; |
---|
13 | import static robocode.util.Utils.normalRelativeAngle; |
---|
14 | |
---|
15 | import java.awt.*; |
---|
16 | import java.awt.event.KeyEvent; |
---|
17 | import static java.awt.event.KeyEvent.*; |
---|
18 | import java.awt.event.MouseEvent; |
---|
19 | import java.awt.event.MouseWheelEvent; |
---|
20 | |
---|
21 | |
---|
22 | /** |
---|
23 | * Interactive - a sample robot by Flemming N. Larsen. |
---|
24 | * <p/> |
---|
25 | * This is a robot that is controlled using the arrow keys and mouse only. |
---|
26 | * <p/> |
---|
27 | * Keys: |
---|
28 | * - W or arrow up: Move forward |
---|
29 | * - S or arrow down: Move backward |
---|
30 | * - A or arrow right: Turn right |
---|
31 | * - D or arrow left: Turn left |
---|
32 | * Mouse: |
---|
33 | * - Moving: Moves the aim, which the gun will follow |
---|
34 | * - Wheel up: Move forward |
---|
35 | * - Wheel down: Move backward |
---|
36 | * - Button 1: Fire a bullet with power = 1 |
---|
37 | * - Button 2: Fire a bullet with power = 2 |
---|
38 | * - Button 3: Fire a bullet with power = 3 |
---|
39 | * <p/> |
---|
40 | * The bullet color depends on the fire power: |
---|
41 | * - Power = 1: Yellow |
---|
42 | * - Power = 2: Orange |
---|
43 | * - Power = 3: Red |
---|
44 | * <p/> |
---|
45 | * Note that the robot will continue firing as long as the mouse button is |
---|
46 | * pressed down. |
---|
47 | * <p/> |
---|
48 | * By enabling the "Paint" button on the robot console window for this robot, |
---|
49 | * a cross hair will be painted for the robots current aim (controlled by the |
---|
50 | * mouse). |
---|
51 | * |
---|
52 | * @author Flemming N. Larsen (original) |
---|
53 | * |
---|
54 | * @version 1.2 |
---|
55 | * |
---|
56 | * @since 1.3.4 |
---|
57 | */ |
---|
58 | public class Interactive extends AdvancedRobot { |
---|
59 | |
---|
60 | // Move direction: 1 = move forward, 0 = stand still, -1 = move backward |
---|
61 | int moveDirection; |
---|
62 | |
---|
63 | // Turn direction: 1 = turn right, 0 = no turning, -1 = turn left |
---|
64 | int turnDirection; |
---|
65 | |
---|
66 | // Amount of pixels/units to move |
---|
67 | double moveAmount; |
---|
68 | |
---|
69 | // The coordinate of the aim (x,y) |
---|
70 | int aimX, aimY; |
---|
71 | |
---|
72 | // Fire power, where 0 = don't fire |
---|
73 | int firePower; |
---|
74 | |
---|
75 | // Called when the robot must run |
---|
76 | public void run() { |
---|
77 | |
---|
78 | // Sets the colors of the robot |
---|
79 | // body = black, gun = white, radar = red |
---|
80 | setColors(Color.BLACK, Color.WHITE, Color.RED); |
---|
81 | |
---|
82 | // Loop forever |
---|
83 | for (;;) { |
---|
84 | // Sets the robot to move forward, backward or stop moving depending |
---|
85 | // on the move direction and amount of pixels to move |
---|
86 | setAhead(moveAmount * moveDirection); |
---|
87 | |
---|
88 | // Decrement the amount of pixels to move until we reach 0 pixels |
---|
89 | // This way the robot will automatically stop if the mouse wheel |
---|
90 | // has stopped it's rotation |
---|
91 | moveAmount = Math.max(0, moveAmount - 1); |
---|
92 | |
---|
93 | // Sets the robot to turn right or turn left (at maximum speed) or |
---|
94 | // stop turning depending on the turn direction |
---|
95 | setTurnRight(45 * turnDirection); // degrees |
---|
96 | |
---|
97 | // Turns the gun toward the current aim coordinate (x,y) controlled by |
---|
98 | // the current mouse coordinate |
---|
99 | double angle = normalAbsoluteAngle(Math.atan2(aimX - getX(), aimY - getY())); |
---|
100 | |
---|
101 | setTurnGunRightRadians(normalRelativeAngle(angle - getGunHeadingRadians())); |
---|
102 | |
---|
103 | // Fire the gun with the specified fire power, unless the fire power = 0 |
---|
104 | if (firePower > 0) { |
---|
105 | setFire(firePower); |
---|
106 | } |
---|
107 | |
---|
108 | // Execute all pending set-statements |
---|
109 | execute(); |
---|
110 | |
---|
111 | // Next turn is processed in this loop.. |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | // Called when a key has been pressed |
---|
116 | public void onKeyPressed(KeyEvent e) { |
---|
117 | switch (e.getKeyCode()) { |
---|
118 | case VK_UP: |
---|
119 | case VK_W: |
---|
120 | // Arrow up key: move direction = forward (infinitely) |
---|
121 | moveDirection = 1; |
---|
122 | moveAmount = Double.POSITIVE_INFINITY; |
---|
123 | break; |
---|
124 | |
---|
125 | case VK_DOWN: |
---|
126 | case VK_S: |
---|
127 | // Arrow down key: move direction = backward (infinitely) |
---|
128 | moveDirection = -1; |
---|
129 | moveAmount = Double.POSITIVE_INFINITY; |
---|
130 | break; |
---|
131 | |
---|
132 | case VK_RIGHT: |
---|
133 | case VK_D: |
---|
134 | // Arrow right key: turn direction = right |
---|
135 | turnDirection = 1; |
---|
136 | break; |
---|
137 | |
---|
138 | case VK_LEFT: |
---|
139 | case VK_A: |
---|
140 | // Arrow left key: turn direction = left |
---|
141 | turnDirection = -1; |
---|
142 | break; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | // Called when a key has been released (after being pressed) |
---|
147 | public void onKeyReleased(KeyEvent e) { |
---|
148 | switch (e.getKeyCode()) { |
---|
149 | case VK_UP: |
---|
150 | case VK_W: |
---|
151 | case VK_DOWN: |
---|
152 | case VK_S: |
---|
153 | // Arrow up and down keys: move direction = stand still |
---|
154 | moveDirection = 0; |
---|
155 | moveAmount = 0; |
---|
156 | break; |
---|
157 | |
---|
158 | case VK_RIGHT: |
---|
159 | case VK_D: |
---|
160 | case VK_LEFT: |
---|
161 | case VK_A: |
---|
162 | // Arrow right and left keys: turn direction = stop turning |
---|
163 | turnDirection = 0; |
---|
164 | break; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | // Called when the mouse wheel is rotated |
---|
169 | public void onMouseWheelMoved(MouseWheelEvent e) { |
---|
170 | // If the wheel rotation is negative it means that it is moved forward. |
---|
171 | // Set move direction = forward, if wheel is moved forward. |
---|
172 | // Otherwise, set move direction = backward |
---|
173 | moveDirection = (e.getWheelRotation() < 0) ? 1 : -1; |
---|
174 | |
---|
175 | // Set the amount to move = absolute wheel rotation amount * 5 (speed) |
---|
176 | // Here 5 means 5 pixels per wheel rotation step. The higher value, the |
---|
177 | // more speed |
---|
178 | moveAmount += Math.abs(e.getWheelRotation()) * 5; |
---|
179 | } |
---|
180 | |
---|
181 | // Called when the mouse has been moved |
---|
182 | public void onMouseMoved(MouseEvent e) { |
---|
183 | // Set the aim coordinate = the mouse pointer coordinate |
---|
184 | aimX = e.getX(); |
---|
185 | aimY = e.getY(); |
---|
186 | } |
---|
187 | |
---|
188 | // Called when a mouse button has been pressed |
---|
189 | public void onMousePressed(MouseEvent e) { |
---|
190 | if (e.getButton() == MouseEvent.BUTTON3) { |
---|
191 | // Button 3: fire power = 3 energy points, bullet color = red |
---|
192 | firePower = 3; |
---|
193 | setBulletColor(Color.RED); |
---|
194 | } else if (e.getButton() == MouseEvent.BUTTON2) { |
---|
195 | // Button 2: fire power = 2 energy points, bullet color = orange |
---|
196 | firePower = 2; |
---|
197 | setBulletColor(Color.ORANGE); |
---|
198 | } else { |
---|
199 | // Button 1 or unknown button: |
---|
200 | // fire power = 1 energy points, bullet color = yellow |
---|
201 | firePower = 1; |
---|
202 | setBulletColor(Color.YELLOW); |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | // Called when a mouse button has been released (after being pressed) |
---|
207 | public void onMouseReleased(MouseEvent e) { |
---|
208 | // Fire power = 0, which means "don't fire" |
---|
209 | firePower = 0; |
---|
210 | } |
---|
211 | |
---|
212 | // Called in order to paint graphics for this robot. |
---|
213 | // "Paint" button on the robot console window for this robot must be |
---|
214 | // enabled in order to see the paintings. |
---|
215 | public void onPaint(Graphics2D g) { |
---|
216 | // Draw a red cross hair with the center at the current aim |
---|
217 | // coordinate (x,y) |
---|
218 | g.setColor(Color.RED); |
---|
219 | g.drawOval(aimX - 15, aimY - 15, 30, 30); |
---|
220 | g.drawLine(aimX, aimY - 4, aimX, aimY + 4); |
---|
221 | g.drawLine(aimX - 4, aimY, aimX + 4, aimY); |
---|
222 | } |
---|
223 | } |
---|