sample.RamFire Maven / Gradle / Ivy
/*
* Copyright (c) 2001-2023 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
* https://robocode.sourceforge.io/license/epl-v10.html
*/
package sample;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import java.awt.*;
/**
* RamFire - a sample robot by Mathew Nelson.
*
* Drives at robots trying to ram them.
* Fires when it hits them.
*
* @author Mathew A. Nelson (original)
* @author Flemming N. Larsen (contributor)
*/
public class RamFire extends Robot {
int turnDirection = 1; // Clockwise or counterclockwise
/**
* run: Spin around looking for a target
*/
public void run() {
// Set colors
setBodyColor(Color.lightGray);
setGunColor(Color.gray);
setRadarColor(Color.darkGray);
while (true) {
turnRight(5 * turnDirection);
}
}
/**
* onScannedRobot: We have a target. Go get it.
*/
public void onScannedRobot(ScannedRobotEvent e) {
if (e.getBearing() >= 0) {
turnDirection = 1;
} else {
turnDirection = -1;
}
turnRight(e.getBearing());
ahead(e.getDistance() + 5);
scan(); // Might want to move ahead again!
}
/**
* onHitRobot: Turn to face robot, fire hard, and ram him again!
*/
public void onHitRobot(HitRobotEvent e) {
if (e.getBearing() >= 0) {
turnDirection = 1;
} else {
turnDirection = -1;
}
turnRight(e.getBearing());
// Determine a shot that won't kill the robot...
// We want to ram him instead for bonus points
if (e.getEnergy() > 16) {
fire(3);
} else if (e.getEnergy() > 10) {
fire(2);
} else if (e.getEnergy() > 4) {
fire(1);
} else if (e.getEnergy() > 2) {
fire(.5);
} else if (e.getEnergy() > .4) {
fire(.1);
}
ahead(40); // Ram him again!
}
}