#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 pattern = 1;

let shot_sound = GetCurrentScriptDirectory ~ "./SFX/se_shot.wav";
let charm_sound = GetCurrentScriptDirectory ~ "./SFX/se_charm.wav";
let spell_sound = GetCurrentScriptDirectory ~ "./SFX/se_UseSpell.wav";
let explosion = GetCurrentScriptDirectory ~ "./SFX/se_explosion.wav";

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(6000);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(75);
	}
}

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

	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);

	ObjEnemy_SetDamageRate(objBoss, 0, 0);

	renderYuukaPoFV(objBoss, 1);

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

	TFinalize;
	TaskManager;
}

task TaskManager
{
	ObjMove_SetDestAtWeight(objBoss, 320, 160, 15, 6);
	RenderDown;
	BFX_ExplosionA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 350, GetCurrentScriptDirectory~"./img/Leaf.png", GetCurrentScriptDirectory~"./img/Ring.png", 10);
	wait(91);
	DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
	wait(89);
	SetSpellName("&quot;Memories of Sixty Years&quot;");
	SpellBG;
	PlaySE(spell_sound);
	wait(180);
	Shoot;
	Movement;
}

task Shoot
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = rand(0,20);
		let obj;
		let wow = 1.2;
		if(pattern == 1)
		{
			loop(32)
			{
				obj = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), wow, angleT, -0.125, 0, YUUKA_FLOWER, 0);
				ObjShot_SetIntersectionEnable(obj, false);
				ObjShot_SetSpellResist(obj, true);
				Explode(obj, 8, 2, false);
				angleT+=360/16;
				wow+=0.2;
				PlaySE(shot_sound);
				yield;
			}
		}
		if(pattern == 2)
		{
			loop(16)
			{
				obj = CreateShotA2(GetPlayerX, GetPlayerY, 7, angleT, -0.25, 0, YUUKA_FLOWER, 0);
				ObjShot_SetIntersectionEnable(obj, false);
				ObjShot_SetSpellResist(obj, true);
				Explode(obj, 8, 2, true);
				angleT+=360/16;
				PlaySE(shot_sound);
			}
		}
		if(pattern == 3)
		{
			loop(5)
			{
				loop(16)
				{
					obj = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), wow, angleT, -0.125, 0, YUUKA_FLOWER, 0);
					ObjShot_SetIntersectionEnable(obj, false);
					ObjShot_SetSpellResist(obj, true);
					Explode(obj, 8, 3, true);
					angleT+=360/16;
					PlaySE(shot_sound);
				}
				wow+=1.25;
				wait(7);
			}
		}
		if(pattern == 4)
		{
			loop(12)
			{
				obj = CreateShotA2(0, 0, 5, angleT, -0.125, 0, YUUKA_FLOWER, 0);
				ObjShot_SetIntersectionEnable(obj, false);
				ObjShot_SetSpellResist(obj, true);
				Explode(obj, 6, 3, false);
				angleT+=360/32;
				PlaySE(shot_sound);
				wait(1);
				obj = CreateShotA2(640, 0, 5, angleT, -0.125, 0, YUUKA_FLOWER, 0);
				ObjShot_SetIntersectionEnable(obj, false);
				ObjShot_SetSpellResist(obj, true);
				Explode(obj, 6, 3, false);
				angleT+=360/32;
				PlaySE(shot_sound);
				wait(1);
				obj = CreateShotA2(0, 480, 5, angleT, -0.125, 0, YUUKA_FLOWER, 0);
				ObjShot_SetIntersectionEnable(obj, false);
				ObjShot_SetSpellResist(obj, true);
				Explode(obj, 6, 3, false);
				angleT+=360/32;
				PlaySE(shot_sound);
				wait(1);
				obj = CreateShotA2(640, 480, 5, angleT, -0.125, 0, YUUKA_FLOWER, 0);
				ObjShot_SetIntersectionEnable(obj, false);
				ObjShot_SetSpellResist(obj, true);
				Explode(obj, 6, 3, false);
				angleT+=360/32;
				PlaySE(shot_sound);
			}
		}
		wait(999999);
	}
}

task Explode(obj, density, speed, aimed_bool)
{
	let angleT = rand(0, 11);
	ascent(i in 0..60)
	{
		ObjRender_SetAlpha(obj, 255-(i*4.5));
		yield;
	}
	wait(360);
	if(!Obj_IsDeleted(obj))
	{
		BFX_FocusA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 60, GetCurrentScriptDirectory ~ "./img/Leaf.png", GetCurrentScriptDirectory ~ "./img/Spellbg THREE.png", 30);
	}
	wait(30);
	if(aimed_bool == true)
	{
		angleT = GetAngleToPlayer(obj);
	}
	loop(density)
	{
		if(!Obj_IsDeleted(obj))
		{
			if(aimed_bool == false)
			{
				CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 5, angleT, -0.25, speed, BL_SMALL_YELLOW, 30);
			}
			if(aimed_bool == true)
			{
				CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 5, angleT, -0.25, speed, BL_SMALL_RED, 30);
			}
			angleT+=360/density;
		}
	}
	PlaySE(charm_sound);
	Obj_Delete(obj);
}

task Movement
{
	wait(120);
	loop
	{
		ObjMove_SetDestAtWeight(objBoss, rand(100,540), rand(160,200), 15, 7);
		pattern = round(rand(0.6, 4.4));
		wait(120);
		Shoot;
		wait(90);
	}
}

task TFinalize
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
	BFX_ExplosionA3(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 350, GetCurrentScriptDirectory~"./img/Leaf.png", 7, 160);
	loop(20)
	{
		ObjMove_SetSpeed(objBoss, 0);
		PlaySE(shot_sound);
		wait(7);
	}
	loop(1)
	{
		RenderKnockout;
		PlaySE(explosion);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		RemoveSound(shot_sound);
		RemoveSound(charm_sound);
		loop(2)
		{
			BFX_ExplosionA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 350, GetCurrentScriptDirectory~"./img/Leaf.png", GetCurrentScriptDirectory~"./img/Ring.png", 60);
		}
		Obj_Delete(objBoss);
		loop(161)
		{
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
		}
		SetAutoDeleteObject(false);
		CloseScript(GetOwnScriptID());
	}
}

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