class Pap {
  
  
  String[] papNames = { "fly1.png", "fly2.png" };
  PImage[] animePap = new PImage[ papNames.length ];
  int frame = 0;
  int frameMin = 0;
  int frameMax = 1;
  
  float posX, posY;
  float larg, haut;
  
  PVector poss, hitBox;
  PVector pos;
  int i;
  float sens = 1;
  float sense;
  float papX = 70;
  
  boolean actif = true, dead = false;
  boolean getHp;

  float temp;
  float vitesse, tempoA;
  
  
 Pap( float pos_X, float pos_Y ) {
    
    
    
    posX = pos_X;
    posY = pos_Y;
    pos = new PVector ( posX, posY );
    
    larg = 12;
    haut = 12;
    hitBox = new PVector ( larg, haut );
    vitesse = 100;
    temp = random ( 100, 500 );
    tempoA = 40;
    sense = random ( - 1, 1 );
    vitesse = random ( 140, 400 );
    if (sense < 0 ) {
      sens = -1;
    } else sens = 1;
    papX = random ( 30, 200 );
    
    poss = new PVector ( papX, 0);
    
    for ( int i = 0; i < papNames.length; i ++ ) {
      String papName = papNames[i];
      animePap[i] = loadImage(papName);
    }
  }
 
 void display() {
    
    if ( !dead ) {
      if ( frame < frameMin || frame >frameMax ) {
        frame = frameMin;
      }
      if ( time > temp + tempoA ) {
        temp = time;
        frame++;
        if ( frame < frameMin || frame > frameMax ) {
          frame = frameMin;
        }
      }
      push();
      imageMode( CENTER );
      //if ( !liz.mangeOn ) {
        image(animePap[ frame ], posX + poss.x , posY + poss.y );
      //}
      pop();
    }
    
    
    /*
      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 mouvement() {
    
   i++;
   if ( ( sin(poss.heading()) * sin(poss.heading()) ) < 0.18 ) { 
     i++;
     poss.rotate(sens * PI/vitesse);
   }
   if ( ( sin(poss.heading()) * sin(poss.heading()) ) < 0.1 ) { 
     i++;
     poss.rotate(sens * PI/vitesse);
     i++;
     poss.rotate(sens * PI/vitesse);
   }
   poss.rotate(sens * PI/vitesse);
   poss.setMag( papX * sin(poss.heading()) * sin(poss.heading()) );

   if ( i >= vitesse ) {
      i = 0;
      sens = sens * -1;
      poss.rotate(PI);
    }
  }
  
  //--------------
  
  void update( Map niveau) {
   pos.x = 0;
   pos.y = 0;
   
   posX +=niveau.move.x;
   posY +=niveau.move.y;

   mouvement();
   pos.x = posX + poss.x;
   pos.y = posY + poss.y;
  }
  
  //--------------
  
  void manger( PVector pos, PVector hitBox ) {   //// revient a collision peut faire plus simple
    
    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 ) {
      mort();
    }
    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 ) {
      mort();
    }
    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) {
      mort();
    }
    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) {
      mort();
    }
    
  }

  void mort() {
    if ( actif ) {
      if ( !dead ) {
        actif = false;
        liz.mangeOn = true;
        dead = true;
        sonP.play();
        if ( dead && liz.mangeOn) { 
          if ( !getHp ) {
            life.satiety+= 3;
            getHp = true;
          }
        }
      }
    }
  }
  
  
  
}
  
 
