#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["AkyuSpell02"]
#Text["AkyuSpell02"]
#Background["./NoobSystem/Default_Background_IceMountain.txt"]
#BGM["./Novum.mp3"]
#System["./NoobSystem/Default_System2.txt"]
#Player[DEFAULT]

#include"./Shot/AllStarShot_Const.dnh"
#include"./NoobSystem/Default_Effect.txt"
#include"./Function/Basic.txt"
#include"./Function/SoundFunctions.txt"
#include"./Function/EffectFunctions.txt"
#include"./Animations.txt"
#include"./Cutin/Cutin.txt"

let objEnemy;
let cx = GetStgFrameWidth/2;
let ct = GetStgFrameHeight/2;
let objScene;
@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	TWork;
	TAnimate("Akyu",GetCurrentScriptDirectory~"dot_akyu.png");
	TEnd;
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
}

@MainLoop
{
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),18);
	ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),24);
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(2000);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(3500000);
	}
}

//----------------------------------------------------

task TWork
{
	let way = 1;
	BossAttack;
	yield;
	ObjMove_SetDestAtWeight(objEnemy, GetStgFrameWidth/2, 120, 10,10);
	DamageRateAtFrame(50, 25,360);
	let cutinimage = GetCurrentScriptDirectory~"null.png";
	ObjCutin_SetSpellcardS4("The Ninth Child of Miare", cutinimage, NAZRIN, 240, 104, 104);
	wait(30);
	ObjCutin_LaunchS3(YABUSAME, cutinimage, "Normal");
	DamageRateAtFrame(75, 25,1200);
	objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	wait(60);
	PlaySound("Charge2",100,0);
	SakuraCharge("ORANGE");
	wait(60);
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		let array = [rand(60,120),rand(60,120),rand(60,120),rand(60,120),rand(60,120)];
		PlaySound("bright",100,0);
		ascent(i in 0..18)
		{
			ascent(j in -3..4)
			{
				RecursiveRing(objEnemy,i*20+j*2,array,4,way);
			}
		}
		way = -way;
		wait(150);
	}
}

	task RecursiveRing(core,currentangle,randdegree,timeleft,way)
	{
		let color = [112,107,109,111,99];
		let obj = CreateShotA2(ObjMove_GetX(core),ObjMove_GetY(core),2.45,currentangle,-2.45/60,0,color[timeleft],12);
		if(timeleft>=0){ObjShot_SetAutoDelete(obj,false);}
		ObjShot_SetSpellResist(obj,true);
		wait(75);
		let r = GetDistance(GetEnemyX,GetEnemyY,ObjMove_GetX(obj),ObjMove_GetY(obj));
		let degree = randdegree[timeleft]/90;
		let targetangle;
		ascent(i in 0..120)
		{
			targetangle = currentangle+i*degree*sin(i)*way;
			ObjMove_SetPosition(obj,GetEnemyX+r*cos(targetangle),GetEnemyY+r*sin(targetangle));
			ObjMove_SetAngle(obj,targetangle);
			wait(i%2);
		}
		wait(15);
		if(timeleft>0)
		{
			RecursiveRing(obj,GetAngleToEnemy(obj)+180,randdegree,timeleft-1,way);
		}
		ObjShot_SetAutoDelete(obj,true);
		ObjShot_SetSpellResist(obj,false);
		Obj_Delete(obj);
	}

task BossAttack
{
	wait(300);
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		PlaySound("Shot6",95,0);
		ascent(i in 0..12)
		{
			CreateShotA2(GetEnemyX,GetEnemyY,4,i*30+GetAngleToPlayer(objEnemy),-4/90,1,237,12);
		}
		wait(60);
	}
}

task TestObj(angle)
{
	let r = 120;
	let r_angle = 180;
	let obj = CreateShotA1(GetEnemyX+r*cos(angle),GetEnemyY+r*sin(angle),0,angle,ARROWHEAD_RED,0);
	ascent(i in 0..90)
	{
		ObjMove_SetPosition(obj,GetEnemyX+r*cos(angle+r_angle*sin(i)),GetEnemyY+r*sin(angle+r_angle*sin(i)));
		ObjMove_SetAngle(obj,angle+r_angle*sin(i));
		yield;
	}
}

task TestObj2(angle)
{
	let r = 120;
	let r_angle = 180;
	let obj = CreateShotA1(GetEnemyX+r*cos(angle),GetEnemyY+r*sin(angle),0,angle,ARROWHEAD_RED,0);
	let calculateangle; 
	ascent(i in 0..90)
	{
		calculateangle = angle+r_angle*sin(i);
		ObjMove_SetPosition(obj,GetEnemyX+r*cos(calculateangle),GetEnemyY+r*sin(calculateangle));
		ObjMove_SetAngle(obj,calculateangle);
		yield;
	}
}


//----------------------------------------------------
task TEnd
{
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		yield;
	}

	let ex = ObjMove_GetX(objEnemy);
	let ey = ObjMove_GetY(objEnemy);
	TExplosionA(ex, ey, 10, 0.6);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	Obj_Delete(objEnemy);
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) +ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0)
	{
		AddScore(ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE));
	}
	loop(30)
	{
		DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
		yield;
	}

	CloseScript(GetOwnScriptID());
}
