void showTitle(){
  
  fill(GREEN);
      textAlign(CENTER,CENTER);
      textFont(smallFont,48);
      text("SREDAVNI",0,0,width,height/4);
      
      fill(WHITE);
      textFont(smallFont,24);
      text("HOW TO PLAY\n\nuse the left and right arrows to move.\n\npress space to drop bomb.",10,height/4,width-10,height/2);

    fill(GREEN);
      text("PRESS SPACE TO START",10,height*3/4,width-10,height/4);
      
     
}

void startPostlude(){
       gamestate = POSTLUDE;
      pauseBg(); 
      playDone();
      
}

void showPostlude(){
    fill(GREEN);
      textAlign(CENTER,CENTER);
      textFont(smallFont,48);
      text("GAME OVER",0,height/4,width,height/4);
    fill(WHITE);      
      textFont(smallFont,24);

      text("PRESS SPACE",0,height/2-40,width,height/4);
  
}



void startGameover(){

  gamestate = GAMEOVER;
  clearWorld();
        setupResults();
}




boolean allDeadShown;
interface Gallery {
   void hide();
   void reveal();
  boolean isHidden(); 
}

void galleryRevealOne(){
      allDeadShown = true;
  for(Gallery g : gallery){
      if(g.isHidden()){
     allDeadShown = false; 
        g.reveal();
        return; 
      }
   } 
}

ArrayList<Gallery>gallery;
ArrayList<Invader> invadersLived;
ArrayList<InvaderAshes> ashes;
void setupResults(){
    allDeadShown = false;
  gallery = new ArrayList<Gallery>();
  invadersLived = new ArrayList<Invader>();
  
  ashes = new ArrayList<InvaderAshes>();


  invadersLived.addAll(f.invaders);
   
   float x = INVADER_OFFSET_X; float y = 57 ;
   for(Invader i : invadersLived){
       i.hide();
       gallery.add(i);
       
       i.x = x; i.y = y;
       x += INVADER_WIDTH + (INVADER_OFFSET_X * 2);
       if(x + INVADER_WIDTH > width){
          y += INVADER_HEIGHT + (INVADER_OFFSET_Y*2); 
          x = INVADER_OFFSET_X;
       }
   }

   for(Building b : buildings){
      if(!b.hasDamage()){
         b.moveUp();
         gallery.add(b);
      }
   }



   
   x = INVADER_OFFSET_X; y = (height/2)+ 50  ;
   for(int i = 0; i < deadInvaderCount; i++){
     InvaderAshes a = new InvaderAshes(x,y);
     gallery.add(a);
      ashes.add(a);
       x += INVADER_WIDTH + (INVADER_OFFSET_X);
       if(x + INVADER_WIDTH > width){
          y += INVADER_HEIGHT + (INVADER_OFFSET_Y*2); 
          x = INVADER_OFFSET_X;
       }
   }
   for(Building b : buildings){
      if(b.hasDamage()){
         b.moveDown();
         gallery.add(b);
      }
   }
   
   
      
   
}

int gallerytimer;
int GALLERYTICK = 20;

void showResults(){
  gallerytimer++;
  if(gallerytimer >= GALLERYTICK || isKeyDown(83) ){
     gallerytimer = 0;
    galleryRevealOne(); 
  }
  
  
  

  fill(WHITE);
      fill(WHITE);
      textFont(smallFont,24);
      text("ALIVE",10,0,width/4,height/8);

   for(Invader i : invadersLived){
      i.draw();
   }

  // if(ashes != null) print(ashes.size()+" "); 
  for(InvaderAshes iA : ashes){
     iA.draw(); 

  }


      text("DEAD",10,height*2/4,width/4,height/8);
 
       for(Building b: buildings){
         b.draw();
       }
       
  
}



class InvaderAshes implements Gallery{
  
  
  
    ArrayList<Particle> particles  = new ArrayList<Particle>();
    InvaderAshes(float px, float py){
      for(int i = 0; i < 10  ; i++){
        particles.add(new Particle(px,py));    
      }
    }
  
  
  boolean galleryHide = true;
  void hide(){
     galleryHide = true; 
  }
  void reveal(){
     galleryHide = false; 
  }
  boolean isHidden(){
     return galleryHide; 
  }
  
  
    void draw(){
      if(galleryHide) return; 
      for(Particle p:particles){
          p.draw();
       } 
    }
    
}


int preludecounter = 0;
boolean hidePreludeText;
void startPrelude(){
  gamestate = PRELUDE; 
     bricks = new HashSet<Brick>();
  addBuildings();
hidePreludeText = false;
  
}

void showPrelude(){
  if(hidePreludeText) return;
      textAlign(CENTER,CENTER);
      fill(WHITE);      
      textFont(smallFont,24);

      text("alas, our planet is no more.",0,50,width,24);
      //blank
      text("we have traveled far",0,100,width,24);
      text("to find a new home.",0,125,width,24);
      //blank
      text("fuel is running low;",0,180,width,24);
      text("we must land now.",0,205,width,24);


        fill(GREEN);
      text("PRESS SPACE",0,255,width,24);

  
}