#include "script/default_system/Default_ShotConst.txt"
#include "./Cutin.txt"

let objBoss;
let objScene = GetEnemyBossSceneObjectID();
let shot_sound = GetCurrentScriptDirectory ~ "./SFX/se_shot.wav";
let charm_sound = GetCurrentScriptDirectory ~ "./SFX/se_charm.wav";
let spell_sound = GetCurrentScriptDirectory ~ "./SFX/se_UseSpell.wav";

//Default dimension of Danmakufu's playing field is 384 * 448 pixels.

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(5000);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(2000000);
	}
}

@Initialize
{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	let img = GetCurrentScriptDirectory ~ "Yukari.png";
	ObjPrim_SetTexture(objBoss, img);
	ObjSprite2D_SetSourceRect(objBoss, 0, 5, 65, 93);
	ObjSprite2D_SetDestCenter(objBoss);
	StartSpell;

	LoadSound(shot_sound);
	LoadSound(charm_sound);
	LoadSound(spell_sound);

	ObjEnemyBossScene_StartSpell(objScene);

	TDrawLoop;
	TFinalize;
	MainTask;
	MainTaskRing;
	MainTaskIllusion;
}

task TDrawLoop
{
	let img = GetCurrentScriptDirectory ~ "Yukari.png";
	ObjPrim_SetTexture(objBoss, img);
	ObjSprite2D_SetSourceRect(objBoss, 170, 95, 225, 193);
	ObjSprite2D_SetDestCenter(objBoss);
	wait(60);
	ObjSprite2D_SetSourceRect(objBoss, 85, 95, 145, 193);
	ObjSprite2D_SetDestCenter(objBoss);
	wait(7);
	ObjSprite2D_SetSourceRect(objBoss, 0, 95, 60, 193);
	ObjSprite2D_SetDestCenter(objBoss);
	wait(7);
	ObjSprite2D_SetSourceRect(objBoss, 0, 5, 65, 93);
	ObjSprite2D_SetDestCenter(objBoss);
	wait(106);
	loop(60)
	{
		ObjSprite2D_SetSourceRect(objBoss, 160, 195, 225, 283);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(60);
		ObjSprite2D_SetSourceRect(objBoss, 85, 195, 145, 283);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(7);
		ObjSprite2D_SetSourceRect(objBoss, 0, 195, 65, 283);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(7);
		ObjSprite2D_SetSourceRect(objBoss, 0, 5, 65, 93);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(106);
		ObjSprite2D_SetSourceRect(objBoss, 170, 95, 225, 193);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(60);
		ObjSprite2D_SetSourceRect(objBoss, 85, 95, 145, 193);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(7);
		ObjSprite2D_SetSourceRect(objBoss, 0, 95, 60, 193);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(7);
		ObjSprite2D_SetSourceRect(objBoss, 0, 5, 65, 93);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(106);
		ObjSprite2D_SetSourceRect(objBoss, 160, 195, 225, 283);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(60);
		ObjSprite2D_SetSourceRect(objBoss, 85, 195, 145, 283);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(7);
		ObjSprite2D_SetSourceRect(objBoss, 0, 195, 65, 283);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(7);
		ObjSprite2D_SetSourceRect(objBoss, 0, 5, 65, 93);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(106);
	}
}

task MainTask
{
	Movement;
	PlaySE(spell_sound);
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = 0;
		let spiral = 0;
		loop
		{	
			loop(10)
			{
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 210, angleT+spiral, DS_BALL_L_BLUE, 10);
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 210, angleT-spiral, DS_BALL_L_RED, 10);
				angleT+=360/10;
	  		}
			spiral+=1;
			wait(2);
		}
	}
}

task MainTaskRing
{
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = GetAngleToPlayer(objBoss);
		loop(15)
		{
			ascent(i in 0..3)
			{
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2.5-i/3, angleT, DS_KUNAI_RED, 10);
			}
		angleT+=360/15;
		}
	PlaySE(shot_sound);
	wait(30);
	}
}

task MainTaskIllusion
{
	wait(240);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = GetAngleToPlayer(objBoss);
		loop(50)
		{
			ascent(i in 0..8)
			{
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2.5+i/3, angleT, DS_KUNAI_BLUE, 10);
			}
		angleT+=360/50;
		}
	PlaySE(shot_sound);
	wait(240);
	}
}

task Movement
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		ObjMove_SetDestAtFrame(objBoss, 192, 150, 60);
		wait(180);
		ObjMove_SetDestAtFrame(objBoss, rand(30,192), rand(50,150), 60);
		wait(180);
		ObjMove_SetDestAtFrame(objBoss, rand(192,350), rand(50,150), 60);
		wait(180);
        }
}

task StartSpell
{
	loop(2){yield;}
	let sekibanki = GetCurrentScriptDirectory ~ "Yukari Cutin.png";
	ObjCutin_SetSpellcardS4("Boundary \"Illusions of Speed\"", sekibanki, MOKOU, 240, 104, 104);
	ObjCutin_LaunchS3(BYAKUREN, sekibanki, "Hard");
}

task TFinalize
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT)
	      +ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
	  AddScore(ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE));
	}
	Obj_Delete(objBoss);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	SetAutoDeleteObject(true);
	CloseScript(GetOwnScriptID());
	return;
}

@MainLoop
{
        ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
        ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24);
        yield;
}

function GetCenterX
{
	return GetStgFrameWidth/2;
}
function GetCenterY
{
	return GetStgFrameHeight/2;
}
function wait(n)
{
	loop(n){yield;}
}