#include "./shot/AllStarShot_Const.txt"
#include "./resources/BloomFX.txt"
#include "./resources/Gizmo's Sprite Library/GizmoSpriteLibrary.txt"
#include "./DLS's System.txt"

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

let pattern = 0;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(3000);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(43);
	}
}

@Initialize
{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);

	if(GetCommonData("mode", 1) == 1)
	{
		CallWeather;
	}
	if(GetCommonData("mode", 1) == 2)
	{
		CallWeatherBackground(1);
	}

	ObjEnemy_SetDamageRate(objBoss, 0, 0);

	renderYuukaPoFV(objBoss, 1);

	LoadSound(shot_sound);
	LoadSound(charm_sound);

	TFinalize;
	TaskManager;
}

task TaskManager
{
	ObjMove_SetDestAtWeight(objBoss, 320, 160, 15, 7);
	RenderSpellBreak;
	BFX_ExplosionA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 350, GetCurrentScriptDirectory~"./img/Leaf.png", GetCurrentScriptDirectory~"./img/Ring.png", 10);
	Timeout;
	wait(180);
	Movement;
}

task Shoot
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = rand(0,18);
		let spiral = 22.5;
		loop(8)
		{
			ascent(i in 0..10)
			{
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3-i/6, angleT+i*2, KUNAI_RED, 30);
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3-i/6, angleT-i*2, KUNAI_RED, 30);
			}
			angleT+=360/8;
		}
		loop(8)
		{
			ascent(i in 0..8)
			{
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2.5-i/6, angleT+i*2, KUNAI_ORANGE, 30);
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2.5-i/6, angleT-i*2, KUNAI_ORANGE, 30);
			}
			angleT+=360/8;
		}
		loop(8)
		{
			ascent(i in 0..6)
			{
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2-i/6, angleT+i*2, KUNAI_YELLOW, 30);
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2-i/6, angleT-i*2, KUNAI_YELLOW, 30);
			}
			angleT+=360/8;
		}
		loop(8)
		{
			ascent(i in 0..4)
			{
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 1.5-i/6, angleT+i*2, KUNAI_GREEN, 30);
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 1.5-i/6, angleT-i*2, KUNAI_GREEN, 30);
			}
			angleT+=360/8;
		}
		loop(8)
		{
			ascent(i in 0..2)
			{
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 1-i/6, angleT+i*2, KUNAI_AQUA, 30);
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 1-i/6, angleT-i*2, KUNAI_AQUA, 30);
			}
			angleT+=360/8;
		}
		loop(60)
		{
			loop(8)
			{
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, angleT+spiral, BALL_LARGE_BLUE, 30);
				angleT+=360/8;
			}
			PlaySE(shot_sound);
			wait(2);
		}
		if(pattern == 1)
		{
			loop(30)
			{
				loop(8)
				{
					CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, angleT+spiral, BALL_LARGE_BLUE, 30);
					angleT+=360/8;
				}
				spiral+=7;
				PlaySE(shot_sound);
				wait(2);
			}
		}
		if(pattern == 2)
		{
			loop(30)
			{
				loop(8)
				{
					CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, angleT-spiral, BALL_LARGE_BLUE, 30);
					angleT+=360/8;
				}
				spiral+=7;
				PlaySE(shot_sound);
				wait(2);
			}
		}
		wait(999999);
	}
}

task Movement
{
	wait(120);
	loop
	{
		ObjMove_SetDestAtWeight(objBoss, rand(100,540), rand(60,120), 15, 7);
		pattern+=1;
		if(pattern == 3)
		{
			pattern = 1;
		}
		wait(120);
		Shoot;
		wait(180);
	}
}

task TFinalize
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
	Obj_Delete(objBoss);
	loop(90)
	{
		DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
		yield;
	}
	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);
	if(GetPlayerLife == -1)
	{
		RenderKnockout;
	}
        yield;
}

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