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

let objBoss;
let objScene = GetEnemyBossSceneObjectID();
let shot_sound = GetCurrentScriptDirectory ~ "./SFX/se_shot.wav";
let boon_sound = GetCurrentScriptDirectory ~ "./SFX/se_boon01.wav";
let spell_sound = GetCurrentScriptDirectory ~ "./SFX/se_UseSpell.wav";
let objFrog;

//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(3000000);
	}
}

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

	LoadSound(shot_sound);
	LoadSound(boon_sound);
	LoadSound(spell_sound);

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

	TDrawLoop;
	TFinalize;
	MainTask;
}

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

task MainTask
{
	Movement;
	PlaySE(spell_sound);
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = GetAngleToPlayer(objBoss);
		loop(27)
		{
			ascent(i in 0..3)
			{
				objFrog = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2.5-i/2, angleT+i*3, DS_BALL_M_GREEN, 30);
				Freeze(objFrog);
			}
		angleT+=360/27;
		}
	PlaySE(shot_sound);
	wait(180);
	}
}

task Freeze(objFrog)
{
	wait(100);
	ObjMove_SetAngle(objFrog, 90);
	ObjMove_SetSpeed(objFrog, 0);
	ObjShot_SetGraphic(objFrog, DS_BALL_M_SKY);
	PlaySE(boon_sound);
	wait(12);
	ObjMove_SetSpeed(objFrog, 0.2);
	wait(12);
	ObjMove_SetSpeed(objFrog, 0.4);
	wait(12);
	ObjMove_SetSpeed(objFrog, 0.6);
	wait(12);
	ObjMove_SetSpeed(objFrog, 0.8);
	wait(12);
	ObjMove_SetSpeed(objFrog, 1);
	wait(12);
	ObjMove_SetSpeed(objFrog, 1.25);
	wait(12);
	ObjMove_SetSpeed(objFrog, 1.5);
	wait(180);
}

task Movement
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		loop(60)
		{
			ObjMove_SetDestAtFrame(objBoss, 192, 150, 60);
			wait(180);
			ObjMove_SetDestAtFrame(objBoss, rand(30,60), rand(75,100), 60);
			wait(180);
			ObjMove_SetDestAtFrame(objBoss, rand(180,200), rand(50,75), 60);
			wait(180);
			ObjMove_SetDestAtFrame(objBoss, rand(300,320), rand(75,100), 60);
			wait(180);
		}
        }
}

task StartSpell
{
	loop(2){yield;}
	let sekibanki = GetCurrentScriptDirectory ~ "Cirno Cutin.png";
	ObjCutin_SetSpellcardS4("Hobby \"Let's Freeze Frogs!\"", sekibanki, MOKOU, 80, 230, 80);
	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));
	}
	Obj_Delete(objBoss);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	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;
}

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