/**
  Space by A.H. Schulak (Shoe) October 1996
  Space by A.H. Schulak (Shoe) March 1998
**/

package Space;

public class WarpBehavior extends GP.Behaviors.Perpetual {
  private Ship _ship;
  private GP.Attributes.Position _target;
  private GP.Behaviors.Perpetual _oldBehave;

  private static final int WARP_RADIUS = 45;
  private static final int WARP_VELOCITY = 80;
  private static final int IMPULSE_VELOCITY = 10;

  public WarpBehavior(Ship ship, GP.Attributes.Position target,
		      GP.Behaviors.Perpetual oldBehave) {
    _ship = ship;
    //this is so it does not follow the mouse
    _target = new GP.Attributes.Position(target.GetX(),target.GetY());
    _oldBehave = oldBehave;
  }

  public void Apply(double time) {
    //_ship.SetPosition(_ship.GetVector().MovePositionAlong(_target));
    cs015.SP.Vector targetVect = _ship.GetVector();;
    targetVect = targetVect.CreateDirectionallyConstrainedVector(_ship.GetPosition(),_target);
    
    targetVect.SetVelocity(WARP_VELOCITY);
    _ship.SetVector(targetVect);
    _ship.SetPosition(targetVect.MovePositionAlong(_ship.GetPosition()));
    _ship.SetOrientation(new GP.Attributes.Orientation
                              (targetVect.GetOrientation()));

    GP.Attributes.Position pos = _ship.GetPosition();
    if ((((pos.GetX() - _target.GetX()) < WARP_RADIUS) &&
	 ((pos.GetX() - _target.GetX()) > -WARP_RADIUS)) &&
	(((pos.GetY() - _target.GetY()) < WARP_RADIUS) &&
	 ((pos.GetY() - _target.GetY()) > -WARP_RADIUS))) {

      _ship.GetShape().RemoveBehavior(this);
      _ship.GetShape().AddBehavior(_oldBehave);
      targetVect = targetVect.Copy();
      targetVect.SetVelocity(IMPULSE_VELOCITY);
      _ship.SetVector(targetVect);
  
    }
    
  }

}
