class Parrot {

  // fonction display / animation / vitesse animation /  update /mouvement / variation mouvement / collision / mangerpap( sur pap )  / activer:desactiver

  String[] parrot1NamesL = { "parLeft1.png" , "parLeft3.png", "parLeft2.png" };
  String[] parrot1NamesR = { "parRight1.png"  , "parRight3.png" , "parRight2.png"};
  String[] parrot2NamesL = { "parLeft1bis.png" , "parLeft3bis.png", "parLeft2bis.png" };
  String[] parrot2NamesR = { "parRight1bis.png"  , "parRight3bis.png" , "parRight2bis.png"};
  String[] parrot3NamesL = { "conurLeft3.png" , "conurLeft1.png", "conurLeft2.png" };
  String[] parrot3NamesR = { "conurRight3.png"  , "conurRight1.png" , "conurRight2.png"};
  PImage[] animeParrot1L= new PImage[parrot1NamesL.length];
  PImage[] animeParrot1R= new PImage[parrot1NamesR.length];
  PImage[] animeParrot2L= new PImage[parrot2NamesL.length];
  PImage[] animeParrot2R= new PImage[parrot2NamesR.length];
  PImage[] animeParrot3L= new PImage[parrot3NamesL.length];
  PImage[] animeParrot3R= new PImage[parrot3NamesR.length];
  
  int type;
  int frame = 0;
  int frameMin = 0;
  int frameMax = 2;
  
  float posX, posY;
  float larg, haut;
  
  PVector poss, centerImage;
  
  float perroX = 250;
  int i; 
  float g;
  int n = 1;
  float sens = 1;
  float vitesse = 350;
  
  boolean actif = false, dead = false;;
  
  
  float temp;
  float tempo, tempoA;
  float start, end;
  boolean vole, right;
  boolean mangerOn;
  
  Parrot( float pos_X, float pos_Y , int typ ) {
    
    
    type = typ;

    
    if ( type == 3 ) {
      perroX = 80;
      posX = pos_X + 20;
      posY = pos_Y - 650;
    }
    if ( type == 1 || type == 2 ) {
      posX = pos_X;
      posY = pos_Y - 650;
    }
    
    poss = new PVector ( perroX, 0);
    if ( type == 1 || type == 2 ) { 
      centerImage = new PVector(19, -13);
    }
    if ( type == 3 ) {
      centerImage = new PVector(0, 9);
    }
    
    vole = false;
    
    larg = 20;
    haut = 20;
    
    tempo = 1000;
    tempo = (random(1 , 10)* 1000);
    start = 0;
    end = time + (random(1 , 10)* 1000);
    tempoA = 80;
    
    if ( type == 1 ) {
      for ( int i = 0; i < parrot1NamesL.length; i ++ ) {
      String parrotName = parrot1NamesL[i];
      animeParrot1L[i] = loadImage(parrotName);
      }
      for ( int i = 0; i < parrot1NamesR.length; i ++ ) {
      String parrotName = parrot1NamesR[i];
      animeParrot1R[i] = loadImage(parrotName);
      }
    }
    if ( type == 2 ) {
      for ( int i = 0; i < parrot2NamesL.length; i ++ ) {
      String parrotName = parrot2NamesL[i];
      animeParrot2L[i] = loadImage(parrotName);
      }
      for ( int i = 0; i < parrot2NamesR.length; i ++ ) {
      String parrotName = parrot2NamesR[i];
      animeParrot2R[i] = loadImage(parrotName);
      }
    }
    if ( type == 3 ) {
      for ( int i = 0; i < parrot3NamesL.length; i ++ ) {
      String parrotName = parrot3NamesL[i];
      animeParrot3L[i] = loadImage(parrotName);
      }
      for ( int i = 0; i < parrot3NamesR.length; i ++ ) {
      String parrotName = parrot3NamesR[i];
      animeParrot3R[i] = loadImage(parrotName);
      }
    }
  }
  

  void display() {

    if ( !dead ) {

      if ( time > temp + tempoA ) {
          temp = time;
          frame += n;
      }
      
      push();
      imageMode( CENTER );
      
      if ( !right ) {
        if ( type == 1 ) {
          centerImage.x = 19;
          image(animeParrot1L[ frame ], posX + poss.x + centerImage.x , posY + poss.y + centerImage.y);
        }
        if ( type == 2 ) {
          centerImage.x = 19;
          image(animeParrot2L[ frame ], posX + poss.x + centerImage.x , posY + poss.y + centerImage.y);
        } 
        if ( type == 3 ) {
          centerImage.x = -5;
          image(animeParrot3L[ frame ], posX + poss.x + centerImage.x , posY + poss.y + centerImage.y);
        } 
      }
      if ( right ) {
        if ( type == 1 ) {
          centerImage.x = -19;
          image(animeParrot1R[ frame ], posX + poss.x + centerImage.x , posY + poss.y + centerImage.y);
        }
        if ( type == 2 ) {
          centerImage.x = -19;
          image(animeParrot2R[ frame ], posX + poss.x + centerImage.x , posY + poss.y + centerImage.y);
        }
        if ( type == 3 ) {
          centerImage.x = 5;
          image(animeParrot3R[ frame ], posX + poss.x + centerImage.x , posY + poss.y + centerImage.y);
        }
      }
      pop();
      if ( frame <= frameMin ){
        frame = frameMin;
        n = 1;
      }
      if ( frame >= frameMax ) {
        frame = frameMax;
        n = -1;
      }

    }
    /*
      push();
      rectMode(CENTER);
      noStroke();
      fill(0, 200, 0);
      ellipse(posX + poss.x, posY + poss.y, 3, 3);
      stroke( 0 );
      fill( 170, 30, 30, 100 );
      rect( posX + poss.x , posY + poss.y , larg, haut );
      pop();
      */
  }
  
  
  void update( Map niveau) {
    
   posX +=niveau.move.x;
   posY +=niveau.move.y;
   
   if ( posX < - 300 && posX > width + 300 ) {
     actif = false;
     poss.set( perroX, 0 );
   }
   if ( posX > - 300 && posX < width + 300 ){
     actif = true;
   }
   if ( actif ) {
   
   mouvement();
   }
  }
  
  
    
  void mouvement() {
   
    if ( time > end ) {
     vole = true;
     start = time;
     end = start + tempo;
     
    }
    
   if ( vole ) {
   if ( type == 1 || type == 2 ) { 
      i++;
      poss.rotate(sens * PI / vitesse );
      if ( (sin(poss.heading()) > 0.65)) {
        poss.setMag ( perroX  + ( 770 * ( sin(poss.heading())*sin(poss.heading())*sin(poss.heading()))) );
      }
   }
   if ( type == 3 ) {
      i++;
      poss.rotate(sens * PI / vitesse );
      if ( (sin(poss.heading()) > 0.75)) {
        poss.setMag ( perroX  + ( 1250 * ( sin(poss.heading())*sin(poss.heading())*sin(poss.heading())*sin(poss.heading()) * sin(poss.heading()) * sin(poss.heading()) ) ) );
      }
   }
   if ( i == vitesse ) {
     i = 0;
     sens = sens * -1;
      
    right = !right;
    vole = false;
    mangerOn = false;
   }
    
  start = time;
  end = start + tempo; 
  }
}
  
  void collision( PVector pos, PVector hitBox ) {
    if ( pos.x + hitBox.x/2 > posX + poss.x - larg/2 && pos.x - hitBox.x/2 < posX + poss.x + larg/2 && pos.y - hitBox.y/2 > posY + poss.y - hitBox.y && pos.y + hitBox.y/2 < posY + poss.y + haut + hitBox.y ) {
      if ( !dead ) {
      if ( !liz.dead ) {
          sonOver.play();
        }
      liz.actif = false;
      liz.dead = true;
      liz.pos.x = posX + poss.x;
      liz.pos.y = posY + poss.y;
      }
    }
    if ( pos.x - hitBox.x/2 > posX + poss.x + larg/2 && pos.x - hitBox.x/2 < posX + poss.x + larg/2 && pos.y - hitBox.y/2 > posY + poss.y - hitBox.y && pos.y + hitBox.y/2 < posY + poss.y + haut + hitBox.y ) {
      if ( !dead ) {
      if ( !liz.dead ) {
          sonOver.play();
        }
      liz.actif = false;
      liz.dead = true;
      liz.pos.x = posX + poss.x;
      liz.pos.y = posY + poss.y;
      }
    }
    if ( pos.y + hitBox.y/2 > posY + poss.y && pos.y + hitBox.y/2 < posY + poss.y + haut/2 && pos.x - hitBox.x/2 > posX + poss.x - hitBox.x && pos.x + hitBox.x /2 < posX + poss.x + larg + hitBox.x) {
      if ( !dead ) {
      if ( !liz.dead ) {
          sonOver.play();
        }
      liz.actif = false;
      liz.dead = true;
      liz.pos.x = posX + poss.x;
      liz.pos.y = posY + poss.y;
      }
    }
    if ( pos.y - hitBox.y/2 > posY + poss.y + haut/2 && pos.y - hitBox.y/2 < posY + poss.y + haut && pos.x - hitBox.x/2 > posX + poss.x - hitBox.x && pos.x + hitBox.x /2 < posX + poss.x + larg + hitBox.x) {
      if ( !dead ) {
        if ( !liz.dead ) {
          sonOver.play();
        }
      liz.actif = false;
      liz.dead = true;
      liz.pos.x = posX + poss.x;
      liz.pos.y = posY + poss.y;
      }
    }
  }
void mange( PVector pos, PVector hitBox , Pap pap) {
    if ( pos.x + hitBox.x/2 > posX + poss.x - larg/2 && pos.x - hitBox.x/2 < posX + poss.x + larg/2 && pos.y - hitBox.y/2 > posY + poss.y - hitBox.y && pos.y + hitBox.y/2 < posY + poss.y + haut + hitBox.y ) {
      if ( !pap.dead && !mangerOn) {
      pap.actif = false;
      pap.dead = true;
      sonMazon.play();
      mangerOn = true;
      }
    }
    if ( pos.x - hitBox.x/2 > posX + poss.x + larg/2 && pos.x - hitBox.x/2 < posX + poss.x + larg/2 && pos.y - hitBox.y/2 > posY + poss.y - hitBox.y && pos.y + hitBox.y/2 < posY + poss.y + haut + hitBox.y ) {
      if ( !pap.dead  && !mangerOn) {
      pap.actif = false;
      pap.dead = true;
      sonMazon.play();
      mangerOn = true;
      }
    }
    if ( pos.y + hitBox.y/2 > posY + poss.y && pos.y + hitBox.y/2 < posY + poss.y + haut/2 && pos.x - hitBox.x/2 > posX + poss.x - hitBox.x && pos.x + hitBox.x /2 < posX + poss.x + larg + hitBox.x) {
      if ( !pap.dead  && !mangerOn) {
      pap.actif = false;
      pap.dead = true;
      sonMazon.play();
      mangerOn = true;
      }
    }
    if ( pos.y - hitBox.y/2 > posY + poss.y + haut/2 && pos.y - hitBox.y/2 < posY + poss.y + haut && pos.x - hitBox.x/2 > posX + poss.x - hitBox.x && pos.x + hitBox.x /2 < posX + poss.x + larg + hitBox.x) {
      if ( !pap.dead  && !mangerOn) {
      pap.actif = false;
      pap.dead = true;
      sonMazon.play();
      mangerOn = true;
      }
    }
  }
  
}
