#include_function ".\lib\common.dnh"
#include_function ".\lib\shot.dnh"
#include_function ".\lib\move.dnh"
#include_function ".\lib\cutin\function_cutin.dnh"

let Spawned = false;

let sfxCardGet = GCD ~ "lib\sfx\cardget.wav";
let sfxCharge = GCD ~ "lib\sfx\charge.wav";
let sfxDeath = GCD ~ "lib\sfx\death.wav";
let sfxShot = GCD ~ "lib\sfx\shot.wav";
let sfxSpellcard = GCD ~ "lib\sfx\spellcard.wav";
let sfxSun = GCD ~ "lib\sfx\sun.wav";
let sfxTimeout1 = GCD ~ "lib\sfx\timeout1.wav";
let sfxTimeout2 = GCD ~ "lib\sfx\timeout2.wav";
let sfxWarning = GCD ~ "lib\sfx\warning.wav";
  
sub InitializeEnemy {
  LoadUserShotData(GCD ~ "lib\supershot.dnh");
  ManagePosition;
  ManageSpawning;
  ManageMovement;
  ManageTimer;
  
  SetCommonDataEx("temp", "enemy num", GetCommonDataDefaultEx("temp", "enemy num", 0)+1);
  
  task ManageTimer {
    descent(i in 4..11){
      if(GetTimer > i){
        while(GetTimer > i){ yield; }
        PlaySE(sfxTimeout1);
      }
    }
    descent(i in 0..3){
      if(GetTimer > i){
        while(GetTimer > i){ yield; }
        PlaySE(sfxTimeout2);
      }
    }
  }
  
  task ManageSpawning {
    if(!Spawned){
      Concentration01(60);
      Wait(60);
      Spawned = true;
    }
  }

  task ManagePosition {
    if(!Spawned){
      SetX(GetPlayerX+GetX);
      SetY(GetPlayerY+GetY);
    }
    loop {
      SetX(GetX-GetCommonDataEx("temp", "xDisplace"));
      SetY(GetY-GetCommonDataEx("temp", "yDisplace"));
      if(GetX > GetCenterX+320){
        SetX(GetX-640);
      }
      if(GetX < GetCenterX-320){
        SetX(GetX+640);
      }
      if(GetY > GetCenterY+320){
        SetY(GetY-640);
      }
      if(GetY < GetCenterY-320){
        SetY(GetY+640);
      }
      yield;
    }
  }
}

sub FinalizeEnemy {
  SetCommonDataEx("temp", "enemy num", GetCommonDataDefaultEx("temp", "enemy num", 0)-1);
}

task ManageDeath {
  while(GetLife>0){ yield; }
  PlaySE(sfxDeath);
  SetWrapSpeed(1);
  SetWrapAngle(160);
  Wait(75);
  SetCommonDataEx("temp", "death point", [GetX, GetY]);
  VanishEnemy;
}