class Invader extends Boxish implements Gallery{
  int row, col;
  Fleet f;
  
  boolean attacking = false;
boolean hurt = false;
 PImage img[] = new PImage[2];
  
  int frame;
  
  Invader(float px, float py, Fleet pf, int prow, int pcol){
    super(px,py,INVADER_WIDTH,INVADER_HEIGHT);    
    row = prow;
    col = pcol;
    f = pf;
    c = color(200,150,150);
    
    int type = ceil(random(7));
    
    img[0] = loadImage("aliens/Alien"+type+"_Reg.png");
    img[1] = loadImage("aliens/Alien"+type+"_Reg2.png");
    
    frame = (col + row) % 2;
    
  }
   void showOutline(){
      noFill();
     rect(x,y,w,h); 
   }
  
  
  void advanceFrame(){
     frame++;
    frame = frame % 2; 
  }
   
  void identify(){
     print("R"+row+"C"+col+" ");
  } 
   
  void checkHitTank(){
    for(Tank t : tanks){
       if(t.hit(this)){
          tanksToKill.add(t);
          doParticles(false);
          invadersToKill.add(this);
          t.doParticles();
       } 
    }  
  }  
    
  boolean galleryHide = false;
  void hide(){
     galleryHide = true; 
  }
  void reveal(){
     galleryHide = false; 
  }
  boolean isHidden(){
     return galleryHide; 
  }
  
    
  void draw(){
    if(! galleryHide){
      noStroke();
      image(img[frame],x-INVADER_OFFSET_X,y-INVADER_OFFSET_Y);

    if(DEBUG==1){
    strokeWeight(1);
    stroke(255,0,0);
    noFill();
      rect(x,y,w,h);
    }
    }
  }
  
  void doOffset(){
    if(attacking) return; 
    x = f.x + (col * FLEET_COLWIDTH);
     y = f.y + (row * FLEET_ROWHEIGHT);
  }
  
  void move(){
    if(! attacking) return;
     y  += INVADER_ATTACKSPEED;
    checkFallingCollisions();

  }
  
  
  void checkFallingCollisions(){

         for(Brick b : bricks){
          if(b.brickhit(this)){
            b.damage(this);
           //  invadersToKill.add(this);
          }
       } 
       
  /*     if(hurt && y > HORIZON){
         println("OUCHIE OUCHIE OUCHIE");
        invadersToKill.add(this); 
        doParticles(false);
         return;
       }*/
       
      if(y > TANK_STARTY ){   
        invadersToKill.add(this); 
        doParticles(false);
        
     }
  }
  
  
  
  
void attack(){
  attacking = true;
}


void doParticles(boolean building){
  if(! hurt){
     fxImpact(); 
  }
  hurt = true;
  for(int c = 0; c < 10; c++){
   particlesToAdd.add(new Particle(this,building)); 
  }
}

}