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

let imgAya = GetCurrentScriptDirectory ~ "img\aya.png";

let xDisplacement = 0;
let yDisplacement = 0;

sub InitializeAyaAnimation {
  LoadGraphic(imgAya);
  
  CreateAnimation("aya", "idle", 8);
  ascent(i in 0..8){
    SetAnimationFrame("aya", "idle", i, imgAya, 6, i*32, 0, (i+1)*32, 48);
  }
  
  CreateAnimation("aya", "start left", 8);
  ascent(i in 0..8){
    SetAnimationFrame("aya", "start left", i, imgAya, 1, i*32, 48, (i+1)*32, 96);
  }
  
  CreateAnimation("aya", "stop left", 7);
  ascent(i in 0..7){
    SetAnimationFrame("aya", "stop left", i, imgAya, 1, (6-i)*32, 48, (7-i)*32, 96);
  }
  
  CreateAnimation("aya", "start right", 8);
  ascent(i in 0..8){
    SetAnimationFrame("aya", "start right", i, imgAya, 1, i*32, 96, (i+1)*32, 144);
  }
  
  CreateAnimation("aya", "stop right", 7);
  ascent(i in 0..7){
    SetAnimationFrame("aya", "stop right", i, imgAya, 1, (6-i)*32, 96, (7-i)*32, 144);
  }
  
  AyaAnimation;
}

task AyaAnimation {
  let LastX = GetPlayerX;
  let LastY = GetPlayerY;
  UpdateLogic;
  Animate("aya", "idle", true);
  loop {
    if(GetPlayerMoveAngle==135 || GetPlayerMoveAngle==180 || GetPlayerMoveAngle==225){
      Animate("aya", "start left", false);
      Wait(GetAnimationTotalTime("aya", "start left"));
      while(GetPlayerMoveAngle==135 || GetPlayerMoveAngle==180 || GetPlayerMoveAngle==225){ yield ; }
      Animate("aya", "stop left", false);
      Wait(GetAnimationTotalTime("aya", "stop left"));
      Animate("aya", "idle", true);
    }
    if(GetPlayerMoveAngle==315 || GetPlayerMoveAngle==0 || GetPlayerMoveAngle==45){
      Animate("aya", "start right", false);
      Wait(GetAnimationTotalTime("aya", "start right"));
      while(GetPlayerMoveAngle==315 || GetPlayerMoveAngle==0 || GetPlayerMoveAngle==45){ yield ; }
      Animate("aya", "stop right", false);
      Wait(GetAnimationTotalTime("aya", "stop right"));
      Animate("aya", "idle", true);
    }
    yield ;
  }
  
  task UpdateLogic {
    loop {
      yield ;
      xDisplacement = GetPlayerX-LastX;
      yDisplacement = GetPlayerY-LastY;
      LastX = GetPlayerX;
      LastY = GetPlayerY;
    }
  }
}

sub DrawAya {
  SetGraphicAngle(0, 0, 0);
  DrawAnimatedSprite("aya", GetPlayerX, GetPlayerY);
}