#include "./Default_ShotConst.txt"
#include "./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";

//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(60);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(5000000);
	}
}

@Initialize
{
	DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
	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 ~ "Lily White.png";
	ObjPrim_SetTexture(objBoss, img);
	ObjSprite2D_SetSourceRect(objBoss, 0, 0, 64, 58);
	ObjSprite2D_SetDestCenter(objBoss);
}

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

task Shoot
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = 0;
		let objShot = ID_INVALID;
		let objShotII = ID_INVALID;
		loop(5)
		{
			ascent(i in 0..2)
			{
				objShot = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2, angleT+i*36, DS_RICE_M_R_RED, 30);
				objShotII = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2, angleT-i*36, DS_RICE_M_R_RED, 30);
				ObjMove_SetAngularVelocity(objShot, 1.25);
				ObjMove_SetAngularVelocity(objShotII, -1.25);
				ObjShot_SetSpellResist(objShot, true);
				ObjShot_SetSpellResist(objShotII, true);
				GodLovesYou(objShot, objShotII);
			}
			angleT+=360/5;
		}
		PlaySE(shot_soundII);
		wait(999999999999);
	}
}

task GodLovesYou(objShot, objShotII)
{
	let objShotIII = ID_INVALID;
	let objShotIIII = ID_INVALID;
	wait(30);
	loop
	{
		if(!Obj_IsDeleted(objShot))
		{
			objShotIII = CreateShotA1(ObjMove_GetX(objShot), ObjMove_GetY(objShot), 0, ObjMove_GetAngle(objShot)+180, DS_BALL_M_R_RED, 30);
		}
		if(!Obj_IsDeleted(objShotII))
		{
			objShotIIII = CreateShotA1(ObjMove_GetX(objShotII), ObjMove_GetY(objShotII), 0, ObjMove_GetAngle(objShotII)+180, DS_BALL_M_R_RED, 30);
		}
		PlaySE(shot_sound);
		GodHatesYou(objShotIII, objShotIIII);
		wait(25);
	}
}

task GodHatesYou(objShotIII, objShotIIII)
{
	wait(60);
	PlaySE(charm_sound);
	ObjMove_SetSpeed(objShotIII, 0.25);
	ObjMove_SetSpeed(objShotIIII, 0.25);
	wait(20);
	ObjMove_SetSpeed(objShotIII, 0.5);
	ObjMove_SetSpeed(objShotIIII, 0.5);
	wait(20);
	ObjMove_SetSpeed(objShotIII, 1);
	ObjMove_SetSpeed(objShotIIII, 1);
	wait(20);
	ObjMove_SetSpeed(objShotIII, 1.5);
	ObjMove_SetSpeed(objShotIIII, 1.5);
	wait(20);
	ObjMove_SetSpeed(objShotIII, 2);
	ObjMove_SetSpeed(objShotIIII, 2);
}

task Movement
{
	ObjMove_SetDestAtWeight(objBoss, 192, 150, 60, 3);
	wait(3);
}

task StartSpell
{
	loop(2){yield;}
	let sekibanki = GetCurrentScriptDirectory ~ "Lily White Cutin.png";
	ObjCutin_SetSpellcardS4("\"Flower-shaped Meteor Shower\"", sekibanki, MOKOU, 240, 104, 104);
	ObjCutin_LaunchS3(BYAKUREN, sekibanki, "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));
	}
	Obj_Delete(objBoss);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	wait(30);
	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;}
}