Advertisement
Guest User

kittens

a guest
May 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.util.List;
  3.  
  4. import com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl;
  5.  
  6. import ihs.apcs.spacebattle.*;
  7. import ihs.apcs.spacebattle.commands.*;
  8.  
  9. public class MyFirstShip extends BasicSpaceship {
  10.    
  11.     final static int SCANNING = 1;
  12.     final static int HUNTING = 2;
  13.     final static int AIMING = 3;
  14.     final static int FIRING = 4;
  15.  
  16.     private int mode = SCANNING;
  17.     private ObjectStatus target = null;
  18.    
  19.     @Override
  20.     public RegistrationData registerShip(int numImages, int worldWidth, int worldHeight)
  21.     {
  22.         return new RegistrationData("all your base are belong to us", new Color(0, 255, 255), 0);
  23.     }
  24.    
  25.     //Send a radar ping to see what's out there
  26.     private ShipCommand scan() {
  27.         mode = HUNTING;
  28.         return new RadarCommand(4); //Find objects
  29.     }
  30.    
  31.     //Determine which target you'll fire at
  32.     private ShipCommand hunt(BasicEnvironment env) {
  33.         Point myPosition = env.getShipStatus().getPosition();
  34.  
  35.         List<ObjectStatus> asteroids =  env.getRadar().getByType("Asteroid");
  36.         if (asteroids.size() == 0) {
  37.             return new RadarCommand(4); //Scan again if no asteroids have been found
  38.         }
  39.         ObjectStatus closest = asteroids.get(0);
  40.         double closestDistance = myPosition.getDistanceTo(closest.getPosition());
  41.         for (ObjectStatus s : asteroids) {
  42.             double distance = myPosition.getDistanceTo(s.getPosition());
  43.             if (distance < closestDistance) {
  44.                 closest = s;
  45.                 closestDistance = distance;
  46.             }
  47.         }
  48.         target = closest;
  49.         mode = AIMING;
  50.         return new RadarCommand(3, target.getId());
  51.     }
  52.    
  53.     private ShipCommand aim(BasicEnvironment env) {
  54.         Point myPosition = env.getShipStatus().getPosition();
  55.         if (target != null) {
  56.             ObjectStatus targetStatus = env.getRadar().getById(target.getId());
  57.             Point targetPosition = targetStatus.getPosition();
  58.  
  59.             mode = FIRING;
  60.             return new RotateCommand(myPosition.getAngleTo(targetPosition));
  61.         } else {
  62.             return scan();
  63.         }
  64.        
  65.     }
  66.    
  67.     private ShipCommand fire() {
  68.         mode = SCANNING;
  69.         return new FireTorpedoCommand('F');
  70.     }
  71.    
  72.     @Override
  73.     public ShipCommand getNextCommand(BasicEnvironment env)
  74.     {
  75.         Point myPosition = env.getShipStatus().getPosition();
  76.        
  77.         if (mode == SCANNING) {
  78.             return scan();
  79.         }
  80.         else if (mode == HUNTING) {
  81.             return hunt(env);
  82.         } else if (mode == AIMING) {
  83.             return aim(env);
  84.         } else if (mode == FIRING) {
  85.             return fire();
  86.         }
  87.         return scan();  //default behavior
  88.  
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement