#include "./shot/AllStarShot_Const.txt"
#include "./resources/Cutin.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;
let objShotII;
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(350);
	}
	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);
	wait(180);
	loop(60)
	{
		ObjRender_SetScaleX(objBoss, -1);
		wait(30);
		ObjRender_SetScaleX(objBoss, 1);
		wait(30);
		ObjRender_SetScaleX(objBoss, -1);
		wait(30);
		ObjRender_SetScaleX(objBoss, 1);
		wait(180);
	}
}

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

task Shoot
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		ascent(i in 0..3)
		{
			ascent(i in 0..10)
			{
				objShot = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, ARROWHEAD_BLUE, 5);
				ObjMove_SetDestAtFrame(objShot, GetPlayerX, GetPlayerY-40, 30);
				PlaySE(shot_sound);
				SurpriseNeedle(objShot);
				yield;
			}
			wait(30);
		}
		ShootII;
		wait(9999999999);
	}
}

task ShootII
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleA = GetAngleToPlayer(objBoss);
		ascent(i in 0..1)
		{
			objShotII = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, ARROWHEAD_PURPLE, 5);
			ObjMove_SetDestAtFrame(objShotII, GetPlayerX, GetPlayerY-40, 30);
			PlaySE(shot_sound);
			wait(30);
			NeedleSurprise(objShotII);
		}
		wait(120);
		Shoot;
		wait(9999999999);
	}
}

task Movement
{
	LmaoMovement;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		ObjMove_SetDestAtWeight(objBoss, x, y, 30, 6);
		wait(3);
	}
	ObjRender_SetScaleX(objBoss, -1);
	ObjMove_SetDestAtWeight(objBoss, 160, 30, 120, 5);
}

task LmaoMovement
{
	x = 192;
	y = 120;
	wait(180);
	loop(60)
	{
		x = 30;
		y = 100;
		wait(30);
		x = 354;
		y = 100;
		wait(30);
		x = GetPlayerX;
		y = 150;
		wait(30);
		x = 192;
		y = 100;
		wait(180);
	}
}

task SurpriseNeedle(objShot)
{
	wait(30);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleA = GetAngleToPlayer(objBoss);
		ascent(i in 0..6)
		{
			if(!Obj_IsDeleted(objShot))
			{
				CreateShotA1(ObjMove_GetX(objShot), ObjMove_GetY(objShot), 2, angleA, ARROWHEAD_RED, 5);
				angleA+=360/6;
			}
		}
		Obj_Delete(objShot);
		yield;
	}
}

task NeedleSurprise(objShotII)
{
	wait(30);
	PlaySE(boon_sound);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		ascent(i in 0..3)
		{
			let angleA = rand(0,360);
			ascent(i in 0..15)
			{
				if(!Obj_IsDeleted(objShotII))
				{
					CreateShotA1(ObjMove_GetX(objShotII)+cos(angleA)*60, ObjMove_GetY(objShotII)+sin(angleA)*60, 3-i*0.25, angleA, BL_SMALL_MAGENTA, 90);
					angleA+=360/15;
				}
			}
			wait(30);
		}
		Obj_Delete(objShotII);
		yield;
	}
}

task StartSpell
{
	loop(2){yield;}
	let sekibanki = GetCurrentScriptDirectory ~ "./img/Kogasa Cutin.png";
	ObjCutin_SetSpellcardS4("Surprise Sign \"Surprise Needles\"", 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));
	}
	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;
}

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