import java.awt.Graphics;
import java.awt.Color;
import java.awt.Polygon;
import Explosion;
import LaserShot;
import Iridium;

public class Ship extends Flying
{
 public Polygon sprite;
 public int     shield;
 public int     zLevel;
 public boolean shown;
 Iridium game;
 int     aWidth;
 int     aHeight;
 public Explosion   explosion;
 public Explosion[] shotExplosions;
 public LaserShot[] shots;
 public Polygon     shape;
 public int         middleX;
 public int         middleY;
 public int         targetX;
 public int         targetY;
 public int         targetMoveX;
 public int         targetMoveY;
 public int         shieldCounter;
 public int         autoDownSpeed;
 public int         landingStadium;
 public double      angle;
 public double      goToAngle;
 public boolean     usingShield;
 public boolean     alive;
 public boolean     wonTheGame;

 public Ship(int gameWidth, int gameHeight, Iridium iridium)
 {
  wonTheGame = false;
  zLevel     = 100;
  game       = iridium;
  aWidth     = gameWidth;
  aHeight    = gameHeight;
  shown      = true;
  autoDownSpeed  = 3;
  shots          = new LaserShot[20];
  shotExplosions = new Explosion[20];
  for(int i = 0; i < 20; i++)
  {
   shots[i] = new LaserShot(Color.red, true);
   shotExplosions[i] = new Explosion();
  }
  shape = new Polygon();
  shape.addPoint(0, -14);
  shape.addPoint(10, -7);
  shape.addPoint(40, -15);
  shape.addPoint(10, 0);
  shape.addPoint(40, 15);
  shape.addPoint(10, 7);
  shape.addPoint(0, 14);
  shape.addPoint(-10, 7);
  shape.addPoint(-40, 15);
  shape.addPoint(-10, 0);
  shape.addPoint(-40, -15);
  shape.addPoint(-10, -7);
  middleX = aWidth / 2;
  middleY = -45;
  targetX = aWidth / 2;
  targetY = aHeight / 2;
  angle     = 0;
  goToAngle = 0;
  shield    = 100;
  alive     = true;
  sprite    = new Polygon();
  for(int i = 0; i < shape.npoints; i++)
  {
   sprite.addPoint(shape.xpoints[i]+middleX, shape.ypoints[i]+middleY);
  }
  explosion      = new Explosion();
  usingShield    = false;
  shieldCounter  = 0;
  landingStadium = 0;
 }

 public void paint(Graphics g)
 {
  if(shown)
  {
   for(int i = 0; i < 20; i++)
   {
    shots[i].paint(g);
    shotExplosions[i].paint(g);
   }
   if(alive && !wonTheGame)
   {
    g.setColor(Color.red);
    g.drawLine(targetX - 1, targetY, targetX - 2, targetY);
    g.drawLine(targetX + 1, targetY, targetX + 2, targetY);
    g.drawLine(targetX, targetY - 1, targetX, targetY - 2);
    g.drawLine(targetX, targetY + 1, targetX, targetY + 2);
   }
   if(usingShield)
   {
    g.setColor(new Color(0x809080));
   }
   else
   {
    g.setColor(Color.gray);
   }
   g.fillPolygon(sprite);
   if(usingShield)
   {
    g.setColor(Color.green);
   }
   else
   {
    g.setColor(Color.lightGray);
   }
   g.drawPolygon(sprite);
   if(game.levelWait == 0)
   {
    g.setColor(new Color(0xDDAA44));
   }
   else
   {
    g.setColor(new Color(0xCC9933));
   }
   if(landingStadium == 0)
   {
    g.fillOval(middleX - 7, middleY - 7, 14, 14);
   }
   else if(landingStadium < 95)
   {
    g.fillOval(middleX - 7*(101-landingStadium)/100,
     middleY - 7*(101-landingStadium)/100,
     14*(101-landingStadium)/100, 14*(101-landingStadium)/100);
   }

   explosion.paint(g);
  }
 }

 public boolean inside(int x, int y)
 {
  return (x > sprite.xpoints[10] || x > sprite.xpoints[8])
   &&(y > sprite.ypoints[10] || y > sprite.ypoints[2])
   &&(x < sprite.xpoints[2] || x < sprite.xpoints[4])
   &&(y < sprite.ypoints[4] || y < sprite.ypoints[8]);
 }

}