#include_function ".\lib\animation.dnh"
#include_function ".\lib\common.dnh"
#include_function ".\lib\move.dnh"

let imgRedFairy = GetCurrentScriptDirectory ~ "lib\img\redfairy.png";
let imgBlueFairy = GetCurrentScriptDirectory ~ "lib\img\bluefairy.png";
let imgGreenFairy = GetCurrentScriptDirectory ~ "lib\img\greenfairy.png";
let imgYellowFairy = GetCurrentScriptDirectory ~ "lib\img\yellowfairy.png";
let imgBigFairy = GetCurrentScriptDirectory ~ "lib\img\bigfairy.png";
let FairyFlip = "right";

function InitializeFairyAnimation(graphic){
  LoadGraphic(graphic);

  if(graphic != imgBigFairy){
    CreateAnimation("fairy", "idle", 4);
    ascent(i in 0..4){
      SetAnimationFrame("fairy", "idle", i, graphic, 8, i*32, 0, (i+1)*32, 32);
    }
    
    CreateAnimation("fairy", "start move", 4);
    ascent(i in 4..8){
      SetAnimationFrame("fairy", "start move", i-4, graphic, 4, i*32, 0, (i+1)*32, 32);
    }
    
    CreateAnimation("fairy", "moving", 4);
    ascent(i in 8..12){
      SetAnimationFrame("fairy", "moving", i-8, graphic, 8, i*32, 0, (i+1)*32, 32);
    }
    
    CreateAnimation("fairy", "stop move", 3);
    ascent(i in 4..7){
      SetAnimationFrame("fairy", "stop move", i-4, graphic, 4, (10-i)*32, 0, (11-i)*32, 32);
    }
  }
  else {
    CreateAnimation("fairy", "idle", 4);
    ascent(i in 0..4){
      SetAnimationFrame("fairy", "idle", i, graphic, 8, i*64, 0, (i+1)*64, 64);
    }
    
    CreateAnimation("fairy", "start move", 4);
    ascent(i in 0..4){
      SetAnimationFrame("fairy", "start move", i, graphic, 4, i*64, 64, (i+1)*64, 128);
    }
    
    CreateAnimation("fairy", "moving", 4);
    ascent(i in 0..4){
      SetAnimationFrame("fairy", "moving", i, graphic, 8, i*64, 128, (i+1)*64, 192);
    }
    
    CreateAnimation("fairy", "stop move", 3);
    ascent(i in 0..3){
      SetAnimationFrame("fairy", "stop move", i, graphic, 4, (2-i)*64, 64, (3-i)*64, 128);
    }
  }
  
  FairyAnimation;
}

task FairyAnimation {
  Animate("fairy", "idle", true);
  loop {
    if(GetWrapSpeedX >= 0.1){
      FairyFlip = "right";
      Animate("fairy", "start move", false);
      Wait(GetAnimationTotalTime("fairy", "start move"));
      Animate("fairy", "moving", true);
      while(GetWrapSpeedX >= 0.1){ yield ; }
      Animate("fairy", "stop move", false);
      Wait(GetAnimationTotalTime("fairy", "stop move"));
      Animate("fairy", "idle", true);
    }
    if(GetWrapSpeedX <= -0.1){
      FairyFlip = "left";
      Animate("fairy", "start move", false);
      Wait(GetAnimationTotalTime("fairy", "start move"));
      Animate("fairy", "moving", true);
      while(GetWrapSpeedX <= -0.1){ yield ; }
      Animate("fairy", "stop move", false);
      Wait(GetAnimationTotalTime("fairy", "stop move"));
      Animate("fairy", "idle", true);
    }
    yield ;
  }
}

sub DrawFairy {
  if(FairyFlip == "right"){SetGraphicAngle(0, 0, 0);}
  if(FairyFlip == "left"){SetGraphicAngle(180, 0, 0);}
  DrawAnimatedSprite("fairy", GetX, GetY);
}