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 / sample / Fire.java < prev    next >
Encoding:
Java Source  |  2005-02-18  |  1.6 KB  |  72 lines

  1. package sample;
  2. import robocode.*;
  3. /**
  4.  * Fire - a sample robot by Mathew Nelson
  5.  * 
  6.  * Sits still.  Spins gun around.  Moves when hit.
  7.  */
  8. public class Fire extends Robot
  9. {
  10.     int dist = 50;                // distance to move when we're hit
  11.     
  12.     /**
  13.      * run: Fire's main run function
  14.      */    
  15.     public void run() {
  16.         // Spin the gun around slowly... forever
  17.         while (true)
  18.         {
  19.             turnGunRight(5);
  20.         }
  21.     }
  22.     
  23.     /**
  24.      * onScannedRobot: Fire!
  25.      */    
  26.     public void onScannedRobot(ScannedRobotEvent e) {
  27.         // If the other robot is close by, and we have plenty of life,
  28.         // fire hard!
  29.         if (e.getDistance() < 50 && getEnergy() > 50)
  30.             fire(3);
  31.         // otherwise, fire 1.
  32.         else
  33.             fire(1);
  34.         // Call scan again, before we turn the gun
  35.         scan();
  36.     }
  37.     
  38.     /**
  39.      * onHitByBullet:  Turn perpendicular to the bullet, and move a bit.
  40.      */    
  41.     public void onHitByBullet(HitByBulletEvent e) {
  42.         turnRight( normalRelativeAngle(90 - (getHeading() - e.getHeading())));
  43.         
  44.         ahead(dist);
  45.         dist *= -1;
  46.         scan();
  47.     }
  48.     
  49.     /**
  50.      * onHitRobot:  Aim at it.  Fire Hard!
  51.      */    
  52.     public void onHitRobot(HitRobotEvent e) {
  53.         double turnGunAmt = normalRelativeAngle(e.getBearing() + getHeading() - getGunHeading());
  54.         turnGunRight(turnGunAmt);
  55.         fire(3);
  56.     }
  57.     
  58.     /**
  59.      * normalRelativeAngle:  returns angle such that -180<angle<=180
  60.      */
  61.     public double normalRelativeAngle(double angle) {
  62.         if (angle > -180 && angle <= 180)
  63.             return angle;
  64.         double fixedAngle = angle;
  65.         while (fixedAngle <= -180)
  66.             fixedAngle += 360;
  67.         while (fixedAngle > 180)
  68.             fixedAngle -= 360;
  69.         return fixedAngle;
  70.     }
  71.  
  72. }