class Player extends Object {
  float speed = 5;
  float angle;
  int vectorX, vectorY;
  int hp, maxHP = 3;
  int facingDirection = 1;
  
  int throwCoolDown;
  
  int flinch, takingDamage;
  
  //String state;
  
  Player() {
    x = width/2;
    y = level.bottomBorder - 60;
    //state = "teste";
    hp = maxHP;
    
    spriteOffsetX = -redIdle[0].width/2;
    spriteOffsetY = -redIdle[0].height;

    hitBox = new HitBox (-30, -15, 60, 30);
  }
  
  void update(){   
    if (takingDamage > 0) {
      x -= facingDirection * 5;
      takingDamage --;
      if (takingDamage == 0 && hp <= 0){
        fade.start(120, color(211, 32, 32), "gameOver");
      }
    }
    else {
      //switch (state){
      //  case "searching":
      //    break;
      //  case "following":
      //    break; 
      //}
      facingDirection = -1;
      if (x < mouseX) facingDirection = 1;
      move();
      if (input.mouseLeft.justPressed){
        if (candles.size() < candleMax && throwCoolDown <= 0) {
          throwCoolDown = 6;
          SE.trigger(SE.flick);
        }
        else{
          SE.trigger(SE.noCandles);
        }
      }
      if (throwCoolDown > 0){
        throwCoolDown --;
        if (throwCoolDown == 0){
          throwCandle();
        }
      }
    }
    if (flinch > 0){
      flinch--;
    }
    
    super.update();
      
  }
  
  void move(){
    vectorX = 0;
    vectorY = 0;
    
    if (input.up.isDown) vectorY = -1;
    if (input.down.isDown) vectorY = +1;
    if (input.left.isDown) vectorX = -1;
    if (input.right.isDown) vectorX = +1;
    
    angle = atan2(vectorY, vectorX);
    
    if (vectorX != 0 || vectorY != 0){
      x+=cos(angle)*speed;
      y+=sin(angle)*speed;
    }
  }
  
  void throwCandle(){
    
    float throwVectorX = mouseX - x;
    float throwVectorY = mouseY - y;
    float throwAngle = atan2(throwVectorY, throwVectorX);
    
    float throwDistance = min(maxDist, dist(x, y, mouseX, mouseY));
    
    float targetX = x + throwDistance * cos(throwAngle);
    float targetY = y + throwDistance * sin(throwAngle);
    
    Candle c = new Candle(x, y, targetX, targetY);

    candles.add(c);
    //fill(255,0,0);
    //rect(targetX, targetY, 40, 40);
  }
  
  void takeDamage(int d){
    SE.trigger(SE.damagePlayer);
    hp -=d;
    if (hp < 0) hp = 0;
    takingDamage = 8;
    flinch = playerFlinchTime;
    throwCoolDown = 0;
  }
  
  void render(){
    
    if (takingDamage > 0) tint(255,200,200);
    if (flinch <= 0 || takingDamage > 0 || animation % 2 == 0){
      if (throwCoolDown > 0) imageX(redThrow[0],x + spriteOffsetX, y + spriteOffsetY, (facingDirection == 1));
      else if (vectorX != 0 || vectorY != 0) imageX(redWalk[animation/10 % 4],x + spriteOffsetX, y + spriteOffsetY, (facingDirection == 1));
      else imageX(redIdle[ animation/10 % 4 ],x + spriteOffsetX, y + spriteOffsetY, (facingDirection == 1));
      rect(x,y,1,1);
    }
    tint(255);
    
    //hitBox.render();
    
    imageMode(CENTER);
    if (candles.size() >= candleMax) {
      tint(211, 32, 32);
    }
    image(cursorImg, mouseX, mouseY);
    noTint();
    imageMode(CORNER);
    
    //rectMode(CENTER);
    //fill(255);
    //rect(mouseX, mouseY, 4, 4);
  }
  
  //float getAimAngle(){
  //  float a = atan2(mouseY - y, mouseX - x);
  //  return a;
  //}
}

void renderHUD(){
  image(hudHeader,10,10);
  for (int i = 0; i < player.maxHP; i++) {
    if (i +1 > player.hp) tint (20);
    image(hudHeart, 15 + i * 30, 45);
    noTint();
  }
  for (int i = 0; i < candleMax; i++) {
    if (i + 1 > candleMax - candles.size()) tint (20);
    image(hudCandle, width -25 - i * 20, 15);
    noTint();
  }
  fill(255);
  text("Floor "+(currentRoom + 1),20, height-20);
}