PFont smallFont;

int gamestate;

int TITLE = 1;
int PRELUDE = 2;
int PLAYING = 3;
int POSTLUDE = 4;
int GAMEOVER = 5;


int ending;
//int 


int deadInvaderCount;

import ddf.minim.*;

Minim minim;
AudioSample kick;



HashSet<Invader> invadersAttacking;// = new HashSet<Invader>();
HashSet<Invader> invadersToKill = new HashSet<Invader>();

HashSet<Brick> bricks;// = new HashSet<Brick>();
HashSet<Brick> bricksToKill = new HashSet<Brick>();


HashSet<Tank> tanks;// = new HashSet<Tank>();
HashSet<Tank> tanksToKill = new HashSet<Tank>();

HashSet<Particle>particles;
HashSet<Particle>particlesToKill = new HashSet<Particle>();
HashSet<Particle>particlesToAdd = new HashSet<Particle>();

color GREEN = color(102,204,0);
color WHITE = color(255);


PImage imgArrowLeft;
PImage imgArrowRight;

Fleet f;

void sendInFleet(){
  f = new Fleet();
  hidePreludeText = true;
    tanks = new HashSet<Tank>();
  for(int i = 0; i < 1; i++) {
    tanks.add(new Tank(i));
  }

  
}

void startGame() {
  gamestate = PLAYING;
    
   invadersAttacking = new HashSet<Invader>();
 
   deadInvaderCount = 0;

    
    
  playBg();

}

void clearWorld(){
   bricksToKill.addAll(bricks);
   particlesToKill.addAll(particles); 
}


void setup() {
  loadProps();
  
  
  particles = new HashSet<Particle>();

   bricks = new HashSet<Brick>();
 invadersAttacking = new HashSet<Invader>();
   


  gamestate=TITLE;


  smallFont = loadFont("pixelmix-24.vlw");
  imgArrowLeft = loadImage("arrow-left.png");
  imgArrowRight = loadImage("arrow-right.png");  

  minim = new Minim(this);
  startupFX();
  startupMusic();
  playOpen();

  size(480,432);
}


void draw() {

  background(0);


  if(gamestate == TITLE){
     showTitle();
  }


  if(gamestate == PLAYING){
     if(f.invaders.size() == 0 &&  invadersAttacking.size() == 0){
        startPostlude();
     }
     if(f.landed()){
        startPostlude();
     }

  }

  if(gamestate == GAMEOVER){
     showResults();
  }

if(gamestate == PRELUDE || gamestate == PLAYING || gamestate == POSTLUDE){
    drawHorizon();
  }
  

if(gamestate == PRELUDE){
   showPrelude(); 
   if(f != null){
      f.march(); 
      f.draw();
   
     if(f.onDeck){
        startGame();    
     }
 }
   
   if(tanks != null){
    for(Tank t:tanks){
      t.move();
      t.draw();
    
    }
   }
   
}

  
  

  for(Brick b : bricks){
    b.draw(); 
 }
 
 
  if(gamestate == POSTLUDE){
     showPostlude(); 
       f.draw();
  }
  
 
  if(gamestate == PLAYING){
    
    if(f.needToTouch == LEFT){ //might need to not be when hit bottom
       showArrows(LEFT); 
    } else {
       showArrows(RIGHT); 
    }
    
    
    f.move();
    f.draw();
    
    
    
  
    for(Tank t:tanks){
      t.move();
      t.draw();
      t.doBullet();
    }
    for(Invader i : invadersAttacking){
        i.move();
        i.checkHitTank();
        i.draw();
    }
  }
  
  
  for(Particle p : particles){
    p.move();
    p.draw(); 
  }

//  print(f.invaders.size()+":");
  if(f != null) f.removeAll(invadersToKill); 
  invadersAttacking.removeAll(invadersToKill);
  deadInvaderCount += invadersToKill.size(); 
  invadersToKill.clear();
  
  
  bricks.removeAll(bricksToKill); bricksToKill.clear();
  if(tanks != null){
  tanks.removeAll(tanksToKill); tanksToKill.clear();
  }
   particles.removeAll(particlesToKill); particlesToKill.clear();
   particles.addAll(particlesToAdd); particlesToAdd.clear();
    
}





void mousePressed(){
if(true) return;
  if(f == null) return;
gamestate = POSTLUDE;
   for(Invader i : f.invaders){
      if(i.hit(mouseX,mouseY)){
          if(DEBUG==1){
             invadersToKill.add(i); 
             //f.launchInvader(i);
          }
      } 
   }
}




void stop(){
  // always close Minim audio classes when you are done with them :-)
      shutdownFX();
  shutdownMusic();
  minim.stop();

  super.stop();
}


void drawHorizon(){
  
  strokeWeight(2);
  stroke(GREEN);
  
   int incr = 1;
  for(float hor = HORIZON; hor < height; hor += incr) {
    line(0,hor,width,hor);
    incr += 1;
  }
 
}



int arrowcounter;

void showArrows(int dir){
    arrowcounter++;
    if(arrowcounter >= 60){
       arrowcounter = 0; 
    }
    if(arrowcounter > 30){
       return;
    }
  
   if(dir == RIGHT){
         for(float y = 10; y < height*3/4 - 40; y += 60){
            image(imgArrowRight, width -  15,y+20);
         }
   }
   if(dir == LEFT){
         for(float y = 10; y < height*3/4 - 40; y += 60){
            image(imgArrowLeft, 0,y+20);
         }
   }

}