#include "./Default_ShotConst.txt"
#include "./Cutin.txt"
#include "./BloomFX.txt"

let objBoss;
let objScene = GetEnemyBossSceneObjectID();
let shot_sound = GetCurrentScriptDirectory ~ "./SFX/se_shot.wav";
let shot_soundII = GetCurrentScriptDirectory ~ "./SFX/se_shot2nd.wav";
let charm_sound = GetCurrentScriptDirectory ~ "./SFX/se_charm.wav";
let boon_sound = GetCurrentScriptDirectory ~ "./SFX/se_boon01.wav";
let spell_sound = GetCurrentScriptDirectory ~ "./SFX/se_UseSpell.wav";
let beam_sound = GetCurrentScriptDirectory ~ "./SFX/se_beam.wav";
let explosion = GetCurrentScriptDirectory ~ "./SFX/se_explosion.wav";

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

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(1000);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(45);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(15000000);
	}
	case(EV_REQUEST_IS_DURABLE_SPELL){
		SetScriptResult(true);
	}
}

@Initialize
{
	DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);

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

	ObjEnemy_SetDamageRate(objBoss, 0, 0);

	LoadSound(shot_sound);
	LoadSound(shot_soundII);
	LoadSound(charm_sound);
	LoadSound(boon_sound);
	LoadSound(spell_sound);
	LoadSound(beam_sound);
	LoadSound(explosion);

	TDrawLoop;
	TFinalize;
	TaskManager;
}

task TDrawLoop
{
	let img = GetCurrentScriptDirectory ~ "Wriggle.png";
	ObjPrim_SetTexture(objBoss, img);
	ObjSprite2D_SetSourceRect(objBoss, 0, 0, 40, 60);
	ObjSprite2D_SetDestCenter(objBoss);
	wait(120);
	ObjRender_SetAlpha(objBoss, 110);
	img = GetCurrentScriptDirectory ~ "Wriggle White.png";
	ObjPrim_SetTexture(objBoss, img);
	ObjSprite2D_SetSourceRect(objBoss, 0, 0, 200, 60);
	ObjSprite2D_SetDestCenter(objBoss);
}

task TaskManager
{
	Movement;
	wait(270);
	StartSpell;
	ObjEnemyBossScene_StartSpell(objScene);
	PlaySE(spell_sound);
	wait(230);
	PlaySE(spell_sound);
	wait(180);
	Shoot;
	ShootII;
	wait(500);
	ObjEnemy_SetDamageRate(objBoss, 20, 20);
}

task Shoot
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = GetAngleToPlayer(objBoss);
		loop(5)
		{
			loop(4)
			{
				ascent(i in 0..1)
				{
					CreateShotA1(GetPlayerX+cos(angleT)*125, GetPlayerY+sin(angleT)*125, -1.2, angleT, DS_BALL_BS_YELLOW, 60);
				}
				angleT+=360/4;
			}
			PlaySE(shot_soundII);
			wait(120);
		}
		wait(1);
		loop(5)
		{
			loop(6)
			{
				ascent(i in 0..1)
				{
					CreateShotA1(GetPlayerX+cos(angleT)*125, GetPlayerY+sin(angleT)*125, -1.2, angleT, DS_BALL_BS_YELLOW, 60);
				}
				angleT+=360/6;
			}
			PlaySE(shot_soundII);
			wait(120);
		}
		wait(1);
		loop(5)
		{
			loop(8)
			{
				ascent(i in 0..1)
				{
					CreateShotA1(GetPlayerX+cos(angleT)*125, GetPlayerY+sin(angleT)*125, -1.2, angleT, DS_BALL_BS_YELLOW, 60);
				}
				angleT+=360/8;
			}
			PlaySE(shot_soundII);
			wait(120);
		}
		wait(99999);
	}
}

task ShootII
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = 0;
		let spiral = 0;
		let dist = 200;
		loop(60)
		{	
			ascent(i in 0..6)
			{
				CreateShotA1(192+cos(angleT+spiral)*dist, 244+sin(angleT+spiral)*dist, -1, angleT+spiral, DS_ICE_RED+i, 30);
				spiral+=1;
				angleT+=360/6;
				PlaySE(shot_sound);
				yield;
	  		}
			yield;
		}
		wait(1);
	}
}

task Movement
{
	ObjMove_SetDestAtWeight(objBoss, 192, -70, 60, 3);
	wait(150);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		ObjMove_SetDestAtWeight(objBoss, 192, 244, 60, 3);
		wait(3);
	}
}

task StartSpell
{
	loop(2){yield;}
	let sekibanki = GetCurrentScriptDirectory ~ "Wriggle Cutin.png";
	ObjCutin_SetSpellcardS4("\"Firefly Path - Bright Path\"", sekibanki, MOKOU, 240, 104, 104);
	ObjCutin_LaunchS3(BYAKUREN, sekibanki, "Test");
	wait(230);
	let kyouko = GetCurrentScriptDirectory ~ "Lily White Cutin.png";
	ObjCutin_SetSpellcardS4("\"Spring Lane - Colorful Path\"", kyouko, MOKOU, 240, 104, 104);
	ObjCutin_LaunchS3(BYAKUREN, kyouko, "Test");
}

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));
	}
	StartSlow(TARGET_ALL,30);
	BFX_ExplosionA1(ObjMove_GetX(objBoss)+75, ObjMove_GetY(objBoss), 350, GetCurrentScriptDirectory~"Leaf.png", 120);
	BFX_ExplosionA1(ObjMove_GetX(objBoss)-75, ObjMove_GetY(objBoss), 350, GetCurrentScriptDirectory~"Leaf.png", 120);
	PlaySE(explosion);
	loop(1)
	{
		wait(120);
		PlaySE(explosion);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		BFX_ExplosionA2(ObjMove_GetX(objBoss)+75, ObjMove_GetY(objBoss), 350, GetCurrentScriptDirectory~"Leaf.png", GetCurrentScriptDirectory~"Ring.png", 60);
		BFX_ExplosionA2(ObjMove_GetX(objBoss)-75, ObjMove_GetY(objBoss), 350, GetCurrentScriptDirectory~"Leaf.png", GetCurrentScriptDirectory~"Ring.png", 60);
		Obj_Delete(objBoss);
		wait(160);
		StopSlow(TARGET_ALL);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		SetAutoDeleteObject(true);
		CloseScript(GetOwnScriptID());
	}
}

@MainLoop
{
        yield;
}

@Finalize
{
	RemoveSound(shot_sound);
	RemoveSound(shot_soundII);
	RemoveSound(charm_sound);
	RemoveSound(boon_sound);
	RemoveSound(beam_sound);
	RemoveSound(spell_sound);
	RemoveSound(explosion);
}

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