#include "./shot/AllStarShot_Const.txt"
#include "./resources/Cutin.txt"
#include "./resources/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";
let objShot = ID_INVALID;
let objShotII = ID_INVALID;
let x = ID_INVALID;
let y = ID_INVALID;

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

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(600);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(33);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(2000000);
	}
}

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

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

	ObjEnemyBossScene_StartSpell(objScene);

	TDrawLoop;
	TFinalize;
	TaskManager;
}

task TDrawLoop
{
	let img = GetCurrentScriptDirectory ~ "./img/Kogasa.png";
	ObjPrim_SetTexture(objBoss, img);
	ObjSprite2D_SetSourceRect(objBoss, 0, 0, 60, 84);
	ObjSprite2D_SetDestCenter(objBoss);
	yield;
}

task TaskManager
{
	Movement;
	PlaySE(spell_sound);
	wait(90);
	Shoot;
	wait(90);
	ObjEnemy_SetDamageRate(objBoss, 20, 20);
}

task Shoot
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = 0;
		let angleR = 180;
		let spiral = 0;
		loop
		{	
			loop(1)
			{
				if(!Obj_IsDeleted(objBoss))
				{
					objShot = CreateShotA1(ObjMove_GetX(objBoss)+cos(angleT+spiral)*30, ObjMove_GetY(objBoss)+sin(angleT+spiral)*30, 6, angleT+spiral, BL_SMALL_RED+500, 15);
					objShotII = CreateShotA1(ObjMove_GetX(objBoss)+cos(angleT+spiral+180)*30, ObjMove_GetY(objBoss)+sin(angleT+spiral+180)*30, 6, angleT+spiral+180, BL_SMALL_MAGENTA+500, 15);
					CreateShotA1(ObjMove_GetX(objBoss)+cos(angleT-spiral+180)*15, ObjMove_GetY(objBoss)+sin(angleT-spiral+180)*15, 3.5, angleT-spiral+180, BL_SMALL_RED, 90);
					CreateShotA1(ObjMove_GetX(objBoss)+cos(angleT-spiral)*15, ObjMove_GetY(objBoss)+sin(angleT-spiral)*15, 3.5, angleT-spiral, BL_SMALL_MAGENTA, 90);
					Curvature(objShot, objShotII);
					angleT+=360;
					spiral+=17;
				}
				PlaySE(shot_sound);
				wait(2);
	  		}
		}
	}
}

task Curvature(objShot, objShotII)
{
	wait(15);
	ObjMove_SetSpeed(objShot, 4);
	ObjMove_SetSpeed(objShotII, 4);
	wait(15);
	ObjMove_SetSpeed(objShot, 2);
	ObjMove_SetSpeed(objShotII, 2);
	wait(15);
	ObjMove_SetSpeed(objShot, 1);
	ObjMove_SetSpeed(objShotII, 1);
	wait(15);
	ObjMove_SetSpeed(objShot, 0.5);
	ObjMove_SetSpeed(objShotII, 0.5);
	wait(15);
	ObjMove_SetSpeed(objShot, 0);
	ObjMove_SetSpeed(objShotII, 0);
	wait(15);
	PlaySE(charm_sound);
	wait(15);
	ObjMove_SetSpeed(objShot, 1.5);
	ObjMove_SetSpeed(objShotII, 1.5);
	ObjMove_SetAngularVelocity(objShot, 0.375);
	ObjMove_SetAngularVelocity(objShotII, -0.375);
}

task Movement
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		ObjMove_SetDestAtWeight(objBoss, 192, 100, 30, 6);
		wait(3);
	}
	ObjRender_SetScaleX(objBoss, -1);
	ObjMove_SetDestAtWeight(objBoss, 170, 180, 60, 10);
}

task StartSpell
{
	loop(2){yield;}
	let sekibanki = GetCurrentScriptDirectory ~ "./img/Kogasa Cutin.png";
	ObjCutin_SetSpellcardS4("Festival \"Carnival Night\"", sekibanki, MOKOU, 240, 104, 104);
	ObjCutin_LaunchS3(KANAKO, sekibanki, "Quick");
}

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

task AutoDelete
{
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	RemoveSound(shot_sound);
	RemoveSound(shot_soundII);
	RemoveSound(charm_sound);
	RemoveSound(boon_sound);
	RemoveSound(beam_sound);
	RemoveSound(spell_sound);
	yield;
}

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

@Finalize
{
	RemoveSound(explosion);
}

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