import java.awt.Polygon;
import java.awt.Graphics;
import java.awt.Color;
import Flying;
import Ship;
import Explosion;

public class Missile extends Flying
{
 Polygon   shape;
 Explosion explosion;
 Color     color;
 Ship      ship;
 int       middleX;
 int       middleY;
 int       reached;
 int       maxReach;
 public Polygon sprite;
 public int     zLevel;
 public boolean hitting;
 public boolean shown;

 public Missile(Color color, Ship ship)
 {
  this.color      = color;
  this.shown      = false;
  this.ship       = ship;
  this.hitting    = false;
  this.zLevel     = 0;
  this.explosion  = new Explosion();

  shape = new Polygon();
  shape.addPoint(-1, -3);
  shape.addPoint(1, -3);
  shape.addPoint(3, -1);
  shape.addPoint(3, 1);
  shape.addPoint(1, 3);
  shape.addPoint(-1, 3);
  shape.addPoint(-3, 1);
  shape.addPoint(-3, -1);
 }

 public void shoot(int origX, int origY, int zLevel)
 {
  if(!shown && !explosion.exploding)
  {
   this.middleX  = origX;
   this.middleY  = origY;
   this.reached  = 0;
   this.maxReach = 70;
   this.shown    = true;
   this.hitting  = false;
   this.zLevel   = zLevel;
  }
  render();
 }

 public void move()
 {
  if(shown)
  {
   if(middleX+5 < ship.middleX && middleY+5 < ship.middleY)
   {
    middleX += 5;
    middleY += 5;
   }
   else if(middleX+5 < ship.middleX && middleY-5 > ship.middleY)
   {
    middleX += 5;
    middleY -= 5;
   }
   else if(middleX-5 > ship.middleX && middleY+5 < ship.middleY)
   {
    middleX -= 5;
    middleY += 5;
   }
   else if(middleX-5 > ship.middleX && middleY-5 > ship.middleY)
   {
    middleX -= 5;
    middleY -= 5;
   }
   else
   {
    if(middleX < ship.middleX)
    {
     middleX += 7;
     if(middleX > ship.middleX)
     {
      middleX = ship.middleX;
      hitting = true;
     }
    }
    else
    {
     middleX -= 7;
     if(middleX < ship.middleX)
     {
      middleX = ship.middleX;
      hitting = true;
     }
    }
    if(middleY < ship.middleY)
    {
     middleY += 7;
     if(middleY > ship.middleY)
     {
      middleY = ship.middleY;
      hitting = true;
     }
    }
    else if(middleY > ship.middleY)
    {
     middleY -= 7;
     if(middleY < ship.middleY)
     {
      middleY = ship.middleY;
      hitting = true;
     }
    }
    else
    {
     hitting = true;
    }
   }

   if(zLevel < 100)
   {zLevel  += 1;}
   reached += 1;
   if(reached == maxReach)
   {
    crash();
   }
   else if(ship.sprite.contains(middleX, middleY))
   {
    hitting  = true;
   }
   render();
  }
  explosion.go();
 }

 public void paint(Graphics g)
 {
  if(shown)
  {
   g.setColor(color);
   g.fillPolygon(sprite);
   g.setColor(color.brighter());
   g.drawPolygon(sprite);
  }
  explosion.paint(g);
 }

 public void crash()
 {
  hitting = false;
  shown   = false;
  explosion.explode(middleX, middleY, 14);
 }

 void render()
 {
  sprite = new Polygon();
  for(int i = 0; i < shape.npoints; i++)
  {
   sprite.addPoint(shape.xpoints[i] + middleX,
   shape.ypoints[i] + middleY);
  }
 }

}