import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;



float virtualscreenwidth;
float virtualscreenleft;
float virtualscreenright;


ArrayList chasers;

player p;
int planeslost;

String  grade;

void  killplayer(){
    p = new player();   
    planeslost++;
    playerdelay = (int)frameRate;
    
   if( planeslost >= 1 && planeslost <= 2){
      grade = "AAA"; 
   }
   if( planeslost >= 3&& planeslost <= 4){
      grade = "AA"; 
   }
   if( planeslost >= 5 && planeslost <= 7){
      grade = "A"; 
   }
   if( planeslost >= 8 && planeslost <= 9){
      grade = "B"; 
   }
   if( planeslost >= 10 && planeslost <= 11){
      grade = "C"; 
   }
   if( planeslost >= 12 && planeslost <= 14){
      grade = "D"; 
   }
   if( planeslost >= 15){
      grade = "F"; 
   }

    
 }

AudioSnippet boom;

void goboom(){
 boom.rewind(); 
 boom.play(); 
}


void setup(){
  size(500,500); 
    Minim.start(this);
boom  =  Minim.loadSnippet("BOOM2.WAV");

  frameRate(30);
  ellipseMode(CENTER);

  textFont(loadFont("Arial-BoldMT-20.vlw"));

  virtualscreenwidth = width+50;
  virtualscreenleft = (width - virtualscreenwidth)/2;
  virtualscreenright = width - virtualscreenleft;
  gamemode = BEFORE;
  gamediff = DIFFNORMAL;
   
  startgame();
}

void stop()
{
  boom.close();
Minim.stop();
  super.stop();
}


int gamemode;
int gamediff;

int BEFORE = 1;
int DURING = 2;
int AFTER = 3;
int DIFFNORMAL =1;
int DIFFHARD = 2;

void keyPressed(){
   if(key=='n' || key == 'N'){
      gamediff = DIFFNORMAL;
      gamemode = DURING;
     startgame();
   } 
   if(key=='d' || key == 'D'){
      gamediff = DIFFHARD;
      gamemode = DURING;
     startgame();
   } 
   if(key=='q' || key=='Q'){
     //chasers.removeAll();
     chasers = new ArrayList();
     gamemode = AFTER;
   }
}

int   launchdelay;

void startgame(){
  p = new player();
  chasers = new ArrayList();
  for(int i = 0; i < 10; i++){
    chaser c = new chaser(100+(i*35),gamediff);
    chasers.add(c);
  }
  explosions = new HashSet();
  launchdelay  = (int)frameRate ;
  planeslost = 0;
  grade="S";
}

HashSet explosions;
int playerdelay;
float groundheight = 100;
void draw(){
  background(200,200,255);

  if(playerdelay < 0){

  launchdelay--; 
  if(launchdelay == 0){
    launch();
    // launchdelay  = (int)frameRate ;
  } 

  } else {
   playerdelay--; 
  }

  noStroke();
  fill(128,255,128);
  rect(0,height-groundheight, width,groundheight);

if(gamemode != BEFORE && playerdelay < 0){
  if(keyPressed == true){
    if(key == 'a' ||  key == 'A'){
      p.turn(-1.0); 
    } 
    if(key == 'z' ||  key == 'Z'){
      p.turn(1.0); 
    } 
  }
}
  p.move();
  p.draw();
  ArrayList tokill = new ArrayList();
  Iterator i = chasers.iterator();
  while(i.hasNext()){
    chaser c = (chaser)i.next(); 
    c.move();
    c.draw();
    //p.hit(c);

    Iterator j = chasers.iterator();
    while(j.hasNext()){
      chaser d = (chaser)j.next(); 
      if(c != d && c.hit(d)){
        tokill.add(c);
        tokill.add(d);
      }
    }

    if(c.hitground()){
      tokill.add(c); 
    }
    if(c.hit(p)){
       tokill.add(c); 
       killplayer();
    }
  }

  i = tokill.iterator() ;
  while(i.hasNext()){
    chaser c = (chaser)i.next(); 
    if(chasers.contains(c)){
      chasers.remove(c);
      if(chasers.size() == 0){
        gamemode = AFTER;
      }
      explosions.add(new explosion(c.x,c.y));
      goboom();
    }
    if(c.active){
      launchdelay =(int)frameRate; 
    }
  }

  i = explosions.iterator() ;
  while(i.hasNext()){
    explosion e = (explosion)i.next(); 
    e.draw();
    if(e.done()){
      i.remove(); 
    }

  }


  if(p.hitground()){
    explosions.add(new explosion(p.x,p.y));
          goboom();
    killplayer(); 
  }
if(gamemode != DURING){
  drawText();
}



}

float red = 0;

void drawText(){
  red -= 20;
  if(red < 0) red = 250;
  fill(red,0,0);
   text("H  E  A  T  S  E  E  K  E  R",40,80); 

  if(gamemode == AFTER){
    text("RANK:"+grade,360,190);
  }


  fill(0);

   text("(based on a game by jeff wolverton in",140,100); 
   text(" compute!'s gazette, march 1985)",140,120); 
   
  if(gamemode == BEFORE){
   text("welcome to the proving grounds",40,160); 
  } else {
   text("10 missiles destroyed. "+planeslost+" planes lost",40,160);
  }
  
   text("A climbs, Z descends",140,220); 
   text("press:",200,300); 
   text("N for normal",200,330); 
   text("D for difficult",200,360); 
}


void launch(){
  Iterator i = chasers.iterator();
  boolean launched = false;
  while(i.hasNext()){
    chaser c = (chaser)i.next(); 
    if((! launched) && (! c.killed) && (! c.active)){
      c.active = true; 
      launched = true;
    }
  }

}