package MissileCommand;




public class BadMissile extends GP.Animator implements MCConstants  {


  private GP.Graphics.Line _line;
  private Turret _toAttack;
  private double _tx, _ty;
  private double _x, _y, _dx, _dy;
  private int _xstart, _ystart;
  private boolean _isNeg;
  private Holder _holder;
  private double _slope, _theta;
  private double _speed;
  private GP.Container _frame;


  public BadMissile(GP.Container frame, Turret toAttack, Holder holder) {
    super(frame);

    _toAttack=toAttack;
    _holder=holder;
    _frame=frame;

    _line=new GP.Graphics.Line(frame);
  
    _line.SetColor(new GP.Attributes.Colors.White());

    _tx=toAttack.GetPosition().GetX(); // get x and y coordinates of target
    _ty=toAttack.GetPosition().GetY();


    _xstart=cs015.SP.RandomNumberFactory.RandomNumber(0,FRAME_SIZE_X);
    _ystart=0;

    _x=_xstart;
    _y=_ystart;

    _isNeg=false;
    if (_x < _tx)
      _isNeg=true;

    _speed=SPEEDBASE;

    _dx=Math.abs(_xstart-_tx); // slope variables
    _dy=Math.abs(_ystart-_ty);

    _slope=_dy/_dx;

    _theta=Math.atan(_slope);

    //_t=0;

  }



  public void Action() { 

    double newx=_x;
    double newy=_y;

    double dx=_speed*Math.cos(_theta);
    double dy=_speed*Math.sin(_theta);
 
    if (_isNeg) { 
      if (newx < _tx) newx = _x+dx;
    }
    else {
      if (newx > _tx) newx = _x-dx;
    }

    newy = _y+dy;// y is always incremented 

    _line.SetEndPoints(new GP.Attributes.Position(_xstart,_ystart), new GP.Attributes.Position(newx,newy));

    _x=newx;
    _y=newy;

    if (_holder.hitExplosion(_line, newx,newy)) {
      this.Stop();
      _line.Hide();
      _holder.removeMissile(this);
      _holder.updateScore(100);
    }

    if (_y > _ty-10) {
      this.Stop();
      BadExplosion be=new BadExplosion(_frame, _line, _toAttack, _x,_y);
      be.Start();
      _holder.removeMissile(this);
      _holder.updateScore(-200);
    }


  }














  

}
