#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["CirnoSpell02"]
#Text["CirnoSpell02"]
#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 phase = false;
let objScene;
@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(2500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(3500000);
	}
}

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

//----------------------------------------------------
task TWork
{
	yield;
	ObjMove_SetDestAtWeight(objEnemy, GetStgFrameWidth/2, 120, 10,60);
	DamageRateAtFrame(50, 25,180);
	let cutinimage = GetCurrentScriptDirectory~"null.png";
	let way = 1;
	ObjCutin_SetSpellcardS4("Perfect Freeze (player ver.) ", cutinimage, NAZRIN, 240, 104, 104);
	wait(30);
	objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	ObjCutin_LaunchS3(YABUSAME, cutinimage, "Normal");
	wait(30);
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		TWave1(way);
		wait(360);
		BorderMove(90,GetStgFrameWidth-90,90,150,10,90);
		wait(120);
		way *= -1;
		DeleteAllSound;
		yield;
	}
}

task TWave1(way)
{
	let jag = 2;
	SakuraCharge("BLUE");
	PlaySound("Charge2",100,0);
	wait(60);
	ascent(n in 0..90)
	{
		if(n%30==29){jag++;}
		PlaySound("Shot1",100,0);
		ascent(i in 0..jag)
		{
			let v = 5-n/20;
			let angle = i*360/jag+n*23*way;
			CreateShotA1(GetEnemyX,GetEnemyY,v,angle,ICE_BLUE,12);
		}
		ascent(i in 0..jag)
		{
			let v = 5-n/20;
			let angle = i*360/jag+n*27*way+60;
			CreateShotA1(GetEnemyX,GetEnemyY,v,angle,SMALL_BLUE,12);
		}
		if(n==40){FreezeWave;}
		wait(3);
	}
}

task FreezeWave
{
	SakuraCharge2("BLUE");
	PlaySound("Charge3",100,0);
	FreezeCircle;
	task FreezeCircle
	{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory~"Function/image/burst.png";
		let co = GetColor("BLUE");
		let shotcolor;
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj,0,0,128,128);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
		ObjRender_SetScaleXYZ(obj,1,1,0);
		ascent(i in 0..120)
		{
			ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetScaleXYZ(obj,i/40,i/40,0);
			ObjRender_SetColor(obj,120-i,120-i,180-i);
			let shotarr = GetShotIdInCircleA2(GetPlayerX,GetPlayerY,i*1.5,TARGET_ENEMY);
			
			ascent(i in 0..length(shotarr))
			{
				if(ObjShot_GetImageID(shotarr[i])==ICE_BLUE){shotcolor = OVAL_BLUE+500;}
				else{shotcolor = OVAL_BLUE+500;}
				if(Obj_GetValueD(shotarr[i],"create",0)==0)
				{
					ascent(j in 0..4)
					{
						let obj = CreateShotOA1(shotarr[i],0,GetAngleToPlayer(shotarr[i])+j*90,shotcolor,20);
						CheckAlive(obj);
						ObjMove_AddPatternA2(obj,60,0,NO_CHANGE,0.02,0,rand(1.5,2.5));
						Obj_SetValue(obj,"create",j);
					}
				}
				Obj_Delete(shotarr[i]);
			}
			if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) == 0){break;}
			yield;
		}
		Obj_Delete(obj);
	}
}

task CheckAlive(obj)
{
	wait(60);
	if(!Obj_IsDeleted(obj)){PlaySound("BulletWave",90,0);}
}


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