import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.Font;
import java.awt.Polygon;
import LaserShot;
import Iridium;
import Zephi;
import Asteroid;
import Explosion;

public class Irue
{
 Iridium    game;
 Image      irueImg;
 Image      spriteImg;
 Image      scaledImg;
 Asteroid[] scrap;
 Zephi[]    zephis;
 int     aWidth;
 int     aHeight;
 int     shield;
 int     cannonX;
 int     cannonY;
 int     upperImg;
 int     leftImg;
 int     scaledLeft;
 int     scaledUpper;
 boolean imgLoaded;
 public int       stadium;
 public Explosion nova;
 public boolean   shown;
 public LaserShot shot;
 public boolean   alive;

 public Irue(int gameWidth, int gameHeight, Iridium iridium)
 {
  alive   = false;
  stadium = 1;
  shown   = false;
  game    = iridium;
  aWidth  = gameWidth;
  aHeight = gameHeight;
  shot    = new LaserShot(new Color(0xee33dd), false);
  nova    = new Explosion();
  scrap   = new Asteroid[25];
  for(int i = 0; i < 25; i++)
  {
   scrap[i] = new Asteroid(aWidth, aHeight);
  }
  imgLoaded = false;
 }

 public void start(Image img, Zephi[] zephis)
 {
  irueImg = img;
  this.zephis = zephis;
  shown   = true;
  shield  = 200;
  load();
  render();
 }

 public void move()
 {
  if(shown)
  {
   most:
   {
    if(stadium < 150)
    {
     if(imgLoaded)
     {
      stadium++;
      if(stadium == 150)
      {
       alive = true;
      }
     }
     render();
    }
    else if(!shot.shooting && alive)
    {
     seeZephi:
     {
      for(int i = 0; i < 15; i++)
      {
       if(zephis[i].shown)
       {
        break seeZephi;
       }
      }
      break most;
     }
     int rand;
     do
     {
      rand = (int)Math.round(Math.random() * (zephis.length-1));
      if(zephis[rand].shown)
      {
       shot.shoot(cannonX, cannonY,
        zephis[rand].sprite.xpoints[6],
        zephis[rand].sprite.ypoints[6]);
       Sounds.play(Sounds.laser);
       break;
      }
     }
     while(true);
    }
   }
   if(nova.stadium > 300)
   {
    shown = false;
   }
  }
  shot.move();
  nova.go();
  for(int i = 0; i < 25; i++)
  {
   scrap[i].move();
  }
 }

 public void crash()
 {
  if(shown && !nova.exploding)
  {
   Sounds.play(Sounds.die);
   alive   = false;
   shield  = 0;
   nova.explode(aWidth/2, aHeight/2, aWidth - 10);
   for(int i = 0; i < 25; i++)
   {
    scrap[i].start(1);
    scrap[i].crash();
   }
  }
 }

 public boolean hit(int impact)
 {
  if(shown)
  {
   shield -= impact;
   if(shield <= 0)
   {
    crash();
    return true;
   }
  }
  return false;
 }

 public void paint(Graphics g)
 {
  if(shown)
  {
   if(stadium == 1)
   {
    g.setColor(Color.lightGray);
    g.setFont(new Font("Sans-Serif", 0, 10));
    g.drawString("Loading Irue.gif", aWidth/2 - 33, aHeight/2 - 4);
   }
   else
   {
    if(!g.drawImage(spriteImg, leftImg, upperImg, game))
    {
     g.drawImage(scaledImg, scaledLeft, scaledUpper, game);
    }
   }
  }
  shot.paint(g);
  nova.paint(g);
  for(int i = 0; i < 25; i++)
  {
   scrap[i].paint(g);
  }
 }

 public void paintBar(Graphics g)
 {
  if(shown && shield != 0 && stadium > 100)
  {
   g.setColor(Color.black);
   g.fillRect(aWidth/2 - 25, aHeight/2 - 60, 50, 3);

   if(shield >= 125)
   {
    g.setColor(new Color(0x00AA00));
   }
   else if(shield >= 50)
   {
    g.setColor(new Color(0xBBBB00));
   }
   else
   {
    g.setColor(new Color(0xBB0000));
   }
   g.fillRect(aWidth/2 - 25, aHeight/2 - 60, shield/4, 3);
   g.setColor(Color.gray);
   g.drawRect(aWidth/2 - 25, aHeight/2 - 60, 50, 3);
  }
 }

 void render()
 {
  if(shown)
  {
   if(stadium == 1)
   {
    load();
   }
   else if(stadium < 100)
   {
    if(stadium % 5 == 2)
    {
     if(stadium != 2)
     {
      scaledUpper = upperImg;
      scaledLeft  = leftImg;
      scaledImg   = spriteImg;
     }
     spriteImg = irueImg.getScaledInstance(stadium, stadium, 0);
     leftImg   = (aWidth - stadium)/2;
     upperImg  = (aHeight - stadium)/2;
     if(stadium == 2)
     {
      scaledUpper = upperImg;
      scaledLeft  = leftImg;
      scaledImg   = spriteImg;
     }
    }
   }
   else
   {
    spriteImg = irueImg;
    leftImg   = aWidth/2 - 50;
    upperImg  = aHeight/2 - 50;
   }
   cannonX   = 65 + leftImg;
   cannonY   = 32 + upperImg;
  }
 }

 public void load()
 {
  if(!imgLoaded)
  {
   imgLoaded = load(irueImg);
  }
 }

 boolean load(Image img)
 {
  Image hidden = game.createImage(1, 1);
  return hidden.getGraphics().drawImage(img, 0, 0, game);
 }

}