#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["CirnoSpell01"]
#Text["CirnoSpell01"]
#BGM["./Overrate.mp3"]
#System["./NoobSystem/Default_System2.txt"]
#Background["./NoobSystem/Default_Background_IceMountain.txt"]
#Player["./Reisen/Reisen.txt"]

#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;
let trigger = false;
let mway = 1;
let mcount = -1;
let mtime = 0;
@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	TWork;
	TAnimate("Mima",GetCurrentScriptDirectory~"dot_cirno.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(61);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(3500000);
	}
}

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

//----------------------------------------------------
task TWork
{
	yield;
	ObjMove_SetDestAtWeight(objEnemy, GetStgFrameWidth/2, 120, 10,60);
	DamageRateAtFrame(50, 25,240);
	let cutinimage = GetCurrentScriptDirectory~"null.png";
	ObjCutin_SetSpellcardS4("Ice Gear", cutinimage, NAZRIN, 240, 104, 104);
	wait(30);
	objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	ObjCutin_LaunchS3(YABUSAME, cutinimage, "Normal");
	wait(30);
	CreateIce;
	TriggerWave;
	wait(90);
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		let r = 20;
		PlaySound("Shot1",100,0);
		ascent(i in 0..24)
		{
			let angle = i*360/(24)+GetAngleToPlayer(objEnemy);
			CreateShotA1(GetEnemyX+r*cos(angle),GetEnemyY+r*sin(angle),2,angle,BL_SMALL_BLUE+500,15);
			if(i==0)
			{
				CreateShotA1(GetEnemyX+r*cos(angle+3),GetEnemyY+r*sin(angle+3),2,angle+3,BL_SMALL_BLUE+500,15);
				CreateShotA1(GetEnemyX+r*cos(angle-3),GetEnemyY+r*sin(angle-3),2,angle-3,BL_SMALL_BLUE+500,15);
			}
		}
		wait(60-mtime);
	}
}

task CreateIce
{
	PlaySound("BulletWave",100,0);
	ascent(i in 0..8)
	{
		IceCore(i*45,2.5,95);
		IceCore(i*45+20,7,321);
	}
	task IceCore(angle,v,r)
	{
		let core = CreateShotA2(GetEnemyX,GetEnemyY,v,angle,-v/90,0,LARGE_BLUE+500,20);
		let speed = 0;
		let acc = 0.01;
		ObjShot_SetAutoDelete(core,false);
		ObjShot_SetSpellResist(core,true);
		wait(90);
		ascent(i in 0..15)
		{
			IceBullet(i*24,15);
		}
		ascent(i in 0..6)
		{
			ascent(j in 1..4)
			{
				IceBullet(i*60,15+j*7);
			}
		}
		/*wait(30);
		WriteLog(GetObjectDistance(objEnemy,core));*/
		while(!Obj_IsDeleted(core))
		{
			if(speed<0.35){speed+=acc;}
			angle += speed*mway;
			ObjMove_SetDestAtWeight(core,GetEnemyX+r*cos(angle),GetEnemyY+r*sin(angle),20,10);
			if(trigger == true){ChangeR(r);}
			yield;
		}
		task ChangeR(rvalue)
		{
			if(rvalue == 95)
			{
				loop(226/2){r+=2;yield;}
			}
			else
			{
				loop(226/2){r-=2;yield;}
			}
		}
		task IceBullet(angle2,r2)
		{
			let obj = CreateShotOA1(core,0,angle,DOT_GRAY+500,0);
			let icount = 0;
			ObjShot_SetAutoDelete(obj,false);
			ObjShot_SetSpellResist(obj,true);
			while(!Obj_IsDeleted(obj))
			{
				angle2 += speed*3*mway;
				let x = ObjMove_GetX(core)+sin(icount)*r2*cos(angle2);
				let y = ObjMove_GetY(core)+sin(icount)*r2*sin(angle2);
				ObjMove_SetPosition(obj,x,y);
				if(icount<90){icount++;}
				yield;
			}			
		}
	}
}

task TriggerWave
{
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		wait(180);
		PlaySound("Charge2",100,0);
		SakuraCharge("BLUE");
		wait(60);
		PlaySound("Boon1",100,0);
		trigger=true;
		yield;
		trigger=false;
		mcount++;
		mtime+=4;
		if(mcount==2)
		{
			mcount = 0;
			mway *= -1;
		}
	}
}

//BorderMove(90,GetStgFrameWidth-90,90,150,10,90);

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