/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */
package simulation;

import ilog.views.maps.defense.symbology.app6a.IlvApp6aSymbol;
import ilog.views.sdm.IlvSDMEngine;

/**
 * Class for missiles to fire on enemy units
 * 
 * @see Target
 * 
 */
public class Missile extends Mobile {
  Target target;
  Missile(double lon0, double lat0, IlvSDMEngine sym, Target target) {
    super("Missile", sym, "SFAPWMSA----USA"); //$NON-NLS-1$ //$NON-NLS-2$
    symbol.setLocation(null, lon0, lat0);
    setHorizontalSpeed(((int) (SimulationContext.random() * 40) + 100) * 10.0);
    setVerticalSpeed(25);
    setSymbolProperty(IlvApp6aSymbol.MODIFIER_ALTITUDE_OR_DEPTH, SimulationController.FS.format(0), false);
    this.target = target;
    updateLocation();
  }
  /**
   * @see simulation.Mobile#updateLocation()
   */
  public void updateLocation() {
    if (isWrecked())
      return;
    if (target.isWrecked()) {
      wreck();
      return;
    }
    double olat = target.getSymbol().getLatitude();
    double olon = target.getSymbol().getLongitude();
    double mlat = getSymbol().getLatitude();
    double mlon = getSymbol().getLongitude();
    setDirection(Math.PI / 2 - Math.atan2(olat - mlat, olon - mlon));
    double dist = Math.sqrt((olat - mlat) * (olat - mlat) + (olon - mlon) * (olon - mlon));
    if (dist < 0.0005) {
      contact();
    } else {
      super.updateLocation();
    }
  }
  /**
   * @see simulation.Mobile#getMinAltitude()
   */
  public double getMinAltitude() {
    if (target != null)
      return target.getAltitude();
    return super.getMinAltitude();
  }
  /**
   * @see simulation.Mobile#getMaxAltitude()
   */
  public double getMaxAltitude() {
    if (target != null)
      return target.getAltitude();
    return super.getMaxAltitude();
  }
  private void contact() {
    target.wreck();
    this.wreck();
  }
}