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

let imgUtsuho = GetCurrentScriptDirectory ~ "lib\img\utsuho.png";

sub InitializeUtsuhoAnimation {
  LoadGraphic(imgUtsuho);

  CreateAnimation("utsuho", "idle", 4);
  ascent(i in 0..4){
    SetAnimationFrame("utsuho", "idle", i, imgUtsuho, 6, i*96, 0, (i+1)*96, 96);
  }
  
  CreateAnimation("utsuho", "start right", 3);
  ascent(i in 0..3){
    SetAnimationFrame("utsuho", "start right", i, imgUtsuho, 4, i*96, 96, (i+1)*96, 192);
  }
  
  CreateAnimation("utsuho", "stop right", 2);
  ascent(i in 0..2){
    SetAnimationFrame("utsuho", "stop right", i, imgUtsuho, 4, (1-i)*96, 96, (2-i)*96, 192);
  }
  
  CreateAnimation("utsuho", "start left", 3);
  ascent(i in 0..3){
    SetAnimationFrame("utsuho", "start left", i, imgUtsuho, 4, i*96, 192, (i+1)*96, 288);
  }
  
  CreateAnimation("utsuho", "stop left", 2);
  ascent(i in 0..2){
    SetAnimationFrame("utsuho", "stop left", i, imgUtsuho, 4, (1-i)*96, 192, (2-i)*96, 288);
  }
  
  CreateAnimation("utsuho", "start cast", 4);
  ascent(i in 0..4){
    SetAnimationFrame("utsuho", "start cast", i, imgUtsuho, 4, i*96, 288, (i+1)*96, 384);
  }
  
  CreateAnimation("utsuho", "stop cast", 3);
  ascent(i in 0..3){
    SetAnimationFrame("utsuho", "stop cast", i, imgUtsuho, 4, (2-i)*96, 288, (3-i)*96, 384);
  }
  
  UtsuhoAnimation;
}

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

sub DrawUtsuho {
  SetGraphicAngle(0, 0, 0);
  DrawAnimatedSprite("utsuho", GetX, GetY);
}