#include "./shot/AllStarShot_Const.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(3500);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(45);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
}

@Initialize
{
	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 ~ "./img/Kogasa.png";
	ObjPrim_SetTexture(objBoss, img);
	ObjSprite2D_SetSourceRect(objBoss, 0, 0, 60, 84);
	ObjSprite2D_SetDestCenter(objBoss);
	wait(240);
	ObjRender_SetScaleX(objBoss, -1);
	wait(120);
	loop(60)
	{
		ObjRender_SetScaleX(objBoss, -1);
		wait(240);
		ObjRender_SetScaleX(objBoss, 1);
		wait(240);
	}
}

task TaskManager
{
	Movement;
	wait(120);
	Shoot;
	ObjEnemy_SetDamageRate(objBoss, 100, 100);
}

task Shoot
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = 0;
		let angleR = 180;
		let spiral = 0;
		loop
		{	
			loop(1)
			{
				ascent(i in 0..60)
				{
					CreateShotA1(ObjMove_GetX(objBoss)+cos(angleT+spiral)*15, ObjMove_GetY(objBoss)+sin(angleT+spiral)*15, 3.5, angleT+spiral, BL_SMALL_MAGENTA, 15);
					CreateShotA1(ObjMove_GetX(objBoss)+cos(angleT+spiral)*15, ObjMove_GetY(objBoss)+sin(angleT+spiral)*15, 2.5, angleT+spiral, BL_SMALL_MAGENTA, 15);
					CreateShotA1(ObjMove_GetX(objBoss)+cos(angleT+spiral)*15, ObjMove_GetY(objBoss)+sin(angleT+spiral)*15, 1.5, angleT+spiral, BL_SMALL_MAGENTA, 15);
					CreateShotA1(ObjMove_GetX(objBoss)+cos(angleR+spiral)*15, ObjMove_GetY(objBoss)+sin(angleR+spiral)*15, 3, angleR+spiral, BL_SMALL_RED, 15);
					CreateShotA1(ObjMove_GetX(objBoss)+cos(angleR+spiral)*15, ObjMove_GetY(objBoss)+sin(angleR+spiral)*15, 2, angleR+spiral, BL_SMALL_RED, 15);
					CreateShotA1(ObjMove_GetX(objBoss)+cos(angleR+spiral)*15, ObjMove_GetY(objBoss)+sin(angleR+spiral)*15, 1, angleR+spiral, BL_SMALL_RED, 15);
					angleT+=360;
					spiral+=12;
					PlaySE(shot_sound);
					yield;
				}
	  		}
			wait(180);
		}
	}
}

task Movement
{
	ObjMove_SetDestAtWeight(objBoss, 192, 150, 60, 3);
	wait(240);
	ObjMove_SetDestAtWeight(objBoss, 60, 100, 60, 3);
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		ObjMove_SetDestAtWeight(objBoss, 60, 100, 60, 3);
		wait(240);
		ObjMove_SetDestAtWeight(objBoss, 330, 100, 60, 3);
		wait(240);
	}
}

task TFinalize
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
	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;}
}