#include "script/default_system/Default_ShotConst.txt"

let objBoss;
let objScene = GetEnemyBossSceneObjectID();
let shot_sound = GetCurrentScriptDirectory ~ "./SFX/se_shot.wav";
let charm_sound = GetCurrentScriptDirectory ~ "./SFX/se_charm.wav";
let objIce;

//Default dimension of Danmakufu's playing field is 384 * 448 pixels.

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(3333);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(31);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
}

@Initialize
{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	let img = GetCurrentScriptDirectory ~ "Cirno.png";
	ObjPrim_SetTexture(objBoss, img);
	ObjSprite2D_SetSourceRect(objBoss, 75, 0, 138, 80);
	ObjSprite2D_SetDestCenter(objBoss);
	ObjMove_SetDestAtFrame(objBoss, 90, 60, 60);

	LoadSound(shot_sound);

	TDrawLoop;
	TFinalize;
	MainTask;
	MainTaskRain;
	MainTaskCyro;
}

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, 34, 0, 69, 48);
		ObjSprite2D_SetDestCenter(objBoss);
		wait(180);
	}
}

task MainTask
{
	Movement;
	wait(60);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleA = GetAngleToPlayer(objBoss);
		objIce = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 4, angleA, DS_BALL_L_BLUE, 30);
		CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 4, angleA+15, DS_BALL_L_BLUE, 30);
		CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 4, angleA-15, DS_BALL_L_BLUE, 30);
		IceShatter;
		wait(90);
	}
}

task MainTaskRain
{
	wait(60);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		CreateShotA1(rand(0,384), 0, rand(1,2), 90, DS_ICE_BLUE, 15);
		PlaySE(shot_sound);
		wait(5);
	}
}

task MainTaskCyro
{
	wait(60);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		ascent(i in 0..6)
		{
			CreateShotA1(rand(0,384), rand(0,180), rand(1,2), rand(0,360), DS_BALL_S_SKY, 180);
			CreateShotA1(rand(0,384), rand(0,180), rand(1,2), rand(0,360), DS_BALL_S_SKY, 180);
			CreateShotA1(rand(0,384), rand(0,180), rand(1,2), rand(0,360), DS_BALL_S_SKY, 180);
		}
		wait(30);
	}
}

task IceShatter
{
	wait(60);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		let angleT = GetAngleToPlayer(objBoss);
		loop(30)
		{
			ascent(i in 0..1)
			{
				if(!Obj_IsDeleted(objIce))
				{
					CreateShotA1(ObjMove_GetX(objIce), ObjMove_GetY(objIce), rand(1,2)-i/3, angleT, DS_ICE_SKY, 7);
				}
			}
		angleT+=360/30;
		}
	PlaySE(charm_sound);
	Obj_Delete(objIce);
	wait(90);
	}
}

task Movement
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		ObjMove_SetDestAtFrame(objBoss, 192, 120, 60);
		wait(180);
		loop(60)
		{
			ObjMove_SetDestAtFrame(objBoss, GetPlayerX-rand(0,20), rand(50,150), 60);
			wait(180);
			ObjMove_SetDestAtFrame(objBoss, GetPlayerX+rand(0,20), rand(50,150), 60);
			wait(180);
			ObjMove_SetDestAtFrame(objBoss, GetPlayerX, 120, 60);
			wait(180);
		}
        }
}

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

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