#include_function ".\AnimationLib.dnh"
#include_function ".\CommonFunctions.dnh"

let imgYoumu = GCD ~ "img\youmu.png";
let YoumuAngle = 270;

sub InitializeYoumuAnimation {
  LoadGraphic(imgYoumu);
  
  CreateAnimation("youmu", "idle", 8);
  ascent(i in 0..8){
    SetAnimationFrame("youmu", "idle", i, imgYoumu, 6, i*32, 0, (i+1)*32, 48);
  }
  
  Animate("youmu", "idle", true);
  UpdateYoumuAngle;
}

task UpdateYoumuAngle {
  let targetAngle = 270;
  loop {
    let playerAngle = GetPlayerMoveAngle;
    if(playerAngle != NULL){
      targetAngle = playerAngle;
    }
    let angdist = AngularDistance(YoumuAngle, targetAngle);
    if(absolute(angdist) < 18){
      YoumuAngle = targetAngle;
    }
    else if(angdist < 0){
      YoumuAngle -= 18;
    }
    else {
      YoumuAngle += 18;
    }
    yield;
  }
}

sub DrawYoumu {
  SetGraphicAngle(0, 0, YoumuAngle+90);
  DrawAnimatedSprite("youmu", GetPlayerX, GetPlayerY);
}