package MissileCommand;





public class GoodMissile extends GP.Animator implements MCConstants {

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

 

  public GoodMissile(GP.Container frame, Turret attackFrom, Holder holder, double tx, double ty) {
    super(frame);

    //System.err.println("Constructed goodmissile");

    _tx=tx;
    _ty=ty;
    _holder=holder;
    _mouse=_holder.mouse();
    _frame=frame;
    
    _line=new GP.Graphics.Line(frame);
    _line.SetColor(new GP.Attributes.Colors.Green());


    _x=attackFrom.GetPosition().GetX();
    _y=attackFrom.GetPosition().GetY()-40;
    
    _dx=java.lang.Math.abs(_x-_tx);
    _dy=java.lang.Math.abs(_y-_ty);

    _slope=_dy/_dx;
    _theta=Math.atan(_slope);

    _speed=50;

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

    _xstart=_x;
    _ystart=_y;


  }



  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 (_y <= _ty) {
      //System.err.println("GoodMissile stopped!");
      this.Stop();
      GoodExplosion ge=new GoodExplosion(_frame, _holder, _line, _x, _y);
      ge.Start();
    }

  }



}
   
