home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Robocode / robocode-setup-1.0.7.jar / extract.jar / robots / sampleteam / MyFirstLeader.java < prev    next >
Encoding:
Java Source  |  2005-02-18  |  1.6 KB  |  62 lines

  1. package sampleteam;
  2. import robocode.*;
  3. import java.awt.Color;
  4. import java.io.*;
  5.  
  6. /**
  7.  * MyFirstLeader - a sample team robot by Mathew Nelson
  8.  * 
  9.  * Looks around for enemies, and orders teammates to fire
  10.  */
  11. public class MyFirstLeader extends TeamRobot
  12. {
  13.     /**
  14.      * run: Leader's default behavior
  15.      */
  16.     public void run() {
  17.         // After trying out your robot, try uncommenting the import at the top,
  18.         // and the next line:
  19.         setColors(Color.red,Color.red,Color.red);
  20.         try {
  21.             // Send RobotColors object to our entire team
  22.             broadcastMessage(new RobotColors(Color.red,Color.red,Color.red));
  23.         } catch (IOException e) {
  24.         }
  25.         // Normal behavior
  26.         while(true) {
  27.             setTurnRadarRight(10000);
  28.             ahead(100);
  29.             back(100);
  30.         }
  31.     }
  32.  
  33.     /**
  34.      * onScannedRobot: What to do when you see another robot
  35.      */
  36.     public void onScannedRobot(ScannedRobotEvent e) {
  37.         // Don't fire on teammates
  38.         if (isTeammate(e.getName()))
  39.         {
  40.             return;
  41.         }
  42.         // Calculate enemy bearing
  43.         double enemyBearing = this.getHeading() + e.getBearing();
  44.         // Calculate enemy's position
  45.         double enemyX = getX() + e.getDistance() * Math.sin(Math.toRadians(enemyBearing));
  46.         double enemyY = getY() + e.getDistance() * Math.cos(Math.toRadians(enemyBearing));
  47.         try {
  48.             // Send enemy position to teammates
  49.             broadcastMessage(new Point(enemyX,enemyY));
  50.         } catch (IOException ex) {
  51.             System.out.println("Unable to send order: " + ex);
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * onHitByBullet: Turn perpendicular to bullet path
  57.      */
  58.     public void onHitByBullet(HitByBulletEvent e) {
  59.         turnLeft(90 - e.getBearing());
  60.     }
  61. }
  62.