#include "script/default_system/Default_ShotConst.txt"

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

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

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(3333);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
}

@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);

	LoadSound(shot_sound);
	LoadSound(charm_sound);

	TDrawLoop;
	TFinalize;
	MainTask;
	MainTaskRing;
}

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;
	wait(60);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleA = GetAngleToPlayer(objBoss);
		CreateShotA1(ObjMove_GetX(objBoss)+rand(-70,70), ObjMove_GetY(objBoss)+rand(-70,70), rand(2,3), 90+rand(-5,5), DS_KUNAI_GREEN, 5);
		CreateShotA1(ObjMove_GetX(objBoss)+rand(-70,70), ObjMove_GetY(objBoss)+rand(-70,70), rand(2,3), 90+rand(-5,5), DS_KUNAI_GREEN, 5);
		CreateShotA1(ObjMove_GetX(objBoss)+rand(-70,70), ObjMove_GetY(objBoss)+rand(-70,70), rand(2,3), 90+rand(-5,5), DS_KUNAI_GREEN, 5);
		CreateShotA1(ObjMove_GetX(objBoss)+rand(-70,70), ObjMove_GetY(objBoss)+rand(-70,70), rand(2,3), 90+rand(-5,5), DS_KUNAI_GREEN, 5);
		PlaySE(shot_sound);
		wait(12);
	}
}

task MainTaskRing
{
	wait(60);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = GetAngleToPlayer(objBoss);
		loop(20)
		{
			ascent(i in 0..4)
			{
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2-i/3, angleT, DS_KUNAI_PURPLE, 10);
			}
		angleT+=360/20;
		}
	PlaySE(charm_sound);
	wait(60);
	}
}

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 TFinalize
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
	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;}
}