#include "script/default_system/Default_ShotConst.txt"
#include "./Cutin.txt"
#include "./BloomFX.txt"

let objBoss;
let objScene = GetEnemyBossSceneObjectID();
let shot_sound = GetCurrentScriptDirectory ~ "./SFX/se_shot.wav";
let charm_sound = GetCurrentScriptDirectory ~ "./SFX/se_charm.wav";
let spell_sound = GetCurrentScriptDirectory ~ "./SFX/se_UseSpell.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(67);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(4000000);
	}
}

@Initialize
{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	ObjMove_SetDestAtFrame(objBoss, 90, 60, 60);
	StartSpell;

	LoadSound(shot_sound);
	LoadSound(charm_sound);
	LoadSound(spell_sound);
	LoadSound(explosion);

	ObjEnemyBossScene_StartSpell(objScene);
	ObjEnemy_SetDamageRate(objBoss, 20, 5);

	TDrawLoop;
	TFinalize;
	MainTask;
	MainTaskCoat;
}

task TDrawLoop
{
	let img = GetCurrentScriptDirectory ~ "Cirno.png";
	ObjPrim_SetTexture(objBoss, img);
	ObjSprite2D_SetSourceRect(objBoss, 0, 0, 31, 48);
	ObjSprite2D_SetDestCenter(objBoss);
}

task MainTask
{
	PlaySE(spell_sound);
	Movement;
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = 0;
		loop(12)
		{
			ascent(i in 0..5)
			{
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2, angleT+i*7, DS_BALL_M_SKY, 10+i*30);
			}
		angleT+=360/12;
		}
	PlaySE(charm_sound);
	StartSlow(TARGET_ALL, 45);
	wait(130);
	StopSlow(TARGET_ALL);
	wait(180);
	}
}

task MainTaskCoat
{
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = GetAngleToPlayer(objBoss);
		loop(1)
		{
			ascent(i in 0..30)
			{
				CreateShotA1(0, rand(0,448), rand(1,2), 0, DS_BALL_BS_WHITE, 10+i*4);
				CreateShotA1(384, rand(0,448), rand(1,2), 180, DS_BALL_BS_WHITE, 10+i*4);
			}
			loop(30)
			{
				PlaySE(shot_sound);
				wait(4);
			}
		}
		wait(180);
	}
}

task Movement
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		ObjMove_SetDestAtFrame(objBoss, 192, 120, 60);
		wait(180);
        }
}

task StartSpell
{
	loop(2){yield;}
	let sekibanki = GetCurrentScriptDirectory ~ "Cirno Cutin.png";
	ObjCutin_SetSpellcardS4("Snow Sign \"Absolute Zero\"", sekibanki, MOKOU, 230, 230, 230);
	ObjCutin_LaunchS3(BYAKUREN, sekibanki, "Easy");
}

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

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

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