#include "./Default_ShotConst.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
{
	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 ~ "Lily White.png";
	ObjPrim_SetTexture(objBoss, img);
	ObjSprite2D_SetSourceRect(objBoss, 0, 0, 64, 58);
	ObjSprite2D_SetDestCenter(objBoss);
	wait(120);
	img = GetCurrentScriptDirectory ~ "Wriggle.png";
	ObjPrim_SetTexture(objBoss, img);
	ObjSprite2D_SetSourceRect(objBoss, 0, 0, 40, 60);
	ObjSprite2D_SetDestCenter(objBoss);
}

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

task Shoot
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = 156;
		let objShot = ID_INVALID;
		loop(20)
		{
			ascent(i in 0..1)
			{
				objShot = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2.5, angleT, DS_BALL_M_SKY, 30);
				SlowlySpawning(objShot);
			}
			angleT+=360/20;
		}
		PlaySE(shot_soundII);
		wait(60);
		angleT = -24;
		let objShotII = ID_INVALID;
		loop(30)
		{
			ascent(i in 0..1)
			{
				objShotII = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2.5, angleT, DS_BALL_M_YELLOW, 30);
				SlowlySpawn(objShotII);
			}
			angleT+=360/30;
		}
		PlaySE(shot_soundII);
		wait(300);
	}
}

task ShootII
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = 0;
		let spiral = 0;
		let objShotIII = ID_INVALID;
		let objShotIV = ID_INVALID;
		loop
		{
			loop(20)
			{	
				loop(4)
				{
					objShotIII = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, angleT+spiral, DS_BALL_M_GREEN, 30);
					objShotIV = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, angleT-spiral, DS_BALL_M_GREEN, 30);
					angleT+=360/4;
					SlowSpawn(objShotIII, objShotIV);
	  			}
				spiral+=5;
				PlaySE(shot_sound);
				wait(7);
			}
			wait(180);
		}
	}
}

task SlowlySpawning(objShot)
{
	wait(60);
	ObjMove_SetSpeed(objShot, 1);
	wait(20);
	ObjMove_SetSpeed(objShot, 0.5);
	wait(20);
	ObjMove_SetSpeed(objShot, 0.25);
	wait(20);
	ObjMove_SetSpeed(objShot, 0);
	wait(60);
	PlaySE(charm_sound);
	if(!Obj_IsDeleted(objShot))
	{
		CreateShotA1(ObjMove_GetX(objShot), ObjMove_GetY(objShot), 1.5, ObjMove_GetAngle(objShot)+140, DS_RICE_S_SKY, 30);
		CreateShotA1(ObjMove_GetX(objShot), ObjMove_GetY(objShot), 1.5, ObjMove_GetAngle(objShot)-140, DS_RICE_S_SKY, 30);
	}
	Obj_Delete(objShot);
}

task SlowlySpawn(objShotII)
{
	wait(60);
	ObjMove_SetSpeed(objShotII, 1);
	wait(20);
	ObjMove_SetSpeed(objShotII, 0.5);
	wait(20);
	ObjMove_SetSpeed(objShotII, 0.25);
	wait(20);
	ObjMove_SetSpeed(objShotII, 0);
	wait(60);
	PlaySE(charm_sound);
	if(!Obj_IsDeleted(objShotII))
	{
		CreateShotA1(ObjMove_GetX(objShotII), ObjMove_GetY(objShotII), 2.5, ObjMove_GetAngle(objShotII)+160, DS_RICE_S_YELLOW, 30);
		CreateShotA1(ObjMove_GetX(objShotII), ObjMove_GetY(objShotII), 2, ObjMove_GetAngle(objShotII)-160, DS_RICE_S_YELLOW, 30);
	}
	Obj_Delete(objShotII);
}

task SlowSpawn(objShotIII, objShotIV)
{
	wait(60);
	ObjMove_SetSpeed(objShotIII, 1);
	ObjMove_SetSpeed(objShotIV, 1);
	wait(20);
	ObjMove_SetSpeed(objShotIII, 0.5);
	ObjMove_SetSpeed(objShotIV, 0.5);
	wait(20);
	ObjMove_SetSpeed(objShotIII, 0.25);
	ObjMove_SetSpeed(objShotIV, 0.25);
	wait(20);
	ObjMove_SetSpeed(objShotIII, 0);
	ObjMove_SetSpeed(objShotIV, 0);
	wait(60);
	PlaySE(charm_sound);
	if(!Obj_IsDeleted(objShotIII))
	{
		CreateShotA1(ObjMove_GetX(objShotIII), ObjMove_GetY(objShotIII), 1.5, ObjMove_GetAngle(objShotIII)+90, DS_RICE_S_GREEN, 30);
		CreateShotA1(ObjMove_GetX(objShotIII), ObjMove_GetY(objShotIII), 1.5, ObjMove_GetAngle(objShotIII)-90, DS_RICE_S_GREEN, 30);
	}
	if(!Obj_IsDeleted(objShotIV))
	{
		CreateShotA1(ObjMove_GetX(objShotIV), ObjMove_GetY(objShotIV), 1.5, ObjMove_GetAngle(objShotIV)+90, DS_RICE_S_GREEN, 30);
		CreateShotA1(ObjMove_GetX(objShotIV), ObjMove_GetY(objShotIV), 1.5, ObjMove_GetAngle(objShotIV)-90, DS_RICE_S_GREEN, 30);
	}
	Obj_Delete(objShotIII);
	Obj_Delete(objShotIV);
}

task Movement
{
	ObjMove_SetDestAtWeight(objBoss, 192, -120, 60, 3);
	wait(120);
	loop(60)
	{
		ObjMove_SetDestAtWeight(objBoss, 192, 140, 60, 3);
		wait(300);
	}
}

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