class Candle extends Object{
  float aimX, aimY;
  float z;
  float speed;
  float angle;
  boolean flying, lit, activated;
  int flyTime;
  int timer;
  
  Candle(float x, float y, float aimX, float aimY){
    this.x = x;
    this.y = y;
    this.aimX = aimX;
    this.aimY = aimY;
    z = 0;
    float distance = dist(x, y, aimX, aimY);
    flyTime = 0;
    this.speed = distance / candleAirTime;
    this.angle = atan2(aimY - y, aimX - x);
    flying = true;
    lit = true;
    
    spriteOffsetX = -candleOn[0].width/2 + 1;
    spriteOffsetY = -44;
    
    hitBox = new HitBox (-6, -4, 12, 8);
  }
    
  void render(){
    
    fill(0);
    noStroke();
    ellipseMode(CENTER);
    ellipse(x,y,10,6);
    
    if(!lit){
      tint(200);
      image(candleOff[0],x + spriteOffsetX, y + spriteOffsetY - z);
      noTint();
    }
    else if (activated) image(candlePower[ animation % 4 ],x + spriteOffsetX, y + spriteOffsetY - z);
    else image(candleOn[ animation/10 % 4 ],x + spriteOffsetX, y + spriteOffsetY - z);
  }
  
  void update(){
    
    if (flying){
      x+=cos(angle)*speed;
      y+=sin(angle)*speed;
      
      if (x > level.rightBorder) {
        x = level.rightBorder;
      }
      if (x < level.leftBorder) {
        x = level.leftBorder;
      }
      if (y > level.bottomBorder) {
        y = level.bottomBorder;
      }
      if (y < level.topBorder) {
        y = level.topBorder;
      }
      
      flyTime++;
      
      float a = -4 * candleMaxHeight / pow(candleAirTime, 2);
      float b = 4 * candleMaxHeight / candleAirTime;
      z = a * pow (flyTime, 2) + b * flyTime;
      
      if (z < 0) {
        z = 0;
        flying = false;
      }
    }
    
    if (activated) {
      timer--;
      if (timer == 0) lit = false;
    }
    
    super.update();
  }
  
  void activate(){
    activated = true;
    timer = damageAreaTime;
  }
}