#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["YukariSpell01"]
#Text["YukariSpell01"]
#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;
let di = "A";
@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	TWork;
	TAnimate("Mima",GetCurrentScriptDirectory~"dot_yukari.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";
	ObjCutin_SetSpellcardS4("Partial Framing", cutinimage, NAZRIN, 240, 104, 104);
	wait(30);
	objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	ObjCutin_LaunchS3(YABUSAME, cutinimage, "Normal");
	wait(60);
	YukariWave;
}

task YukariWave
{
	let way = 1;
	let rem = "A";
	let time = 0;
	PlaySound("Charge2",100,0);
	SakuraCharge("PURPLE");
	wait(60);
	BounceLaser("L");
	wait(30);
	CycleShot;
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		wait(120);
		BorderMove(90,GetStgFrameWidth-90,90,150,10,150);
		wait(120);
		let result = ["L","R","D"][round(rand(-0.49,2.49))];
		while(rem==result)
		{
			result = ["L","R","D"][round(rand(-0.49,2.49))];
		}
		rem = result;
		BounceLaser(result);
		wait(30);
		way *= -1;
		CycleShot;
	}
	task CycleShot
	{
		let a = GetAngleToPlayer(objEnemy);
		ascent(n in 0..25+time/2)
		{
			PlaySound("Shot1",100,0);
			ascent(i in 0..3)
			{
				let angle = i*120+a+n*11*way;
				let v = 1+n/4;
				BShot(GetEnemyX,GetEnemyY,v,angle,LARGE_GRAY,15);
			}
			wait(3);
		}
		time++;
	}
	
		task BShot(x,y,v,angle,color,delay)
		{
			let obj = CreateShotA1(x,y,v,angle,color,delay);
			let count = 0;
			while(!Obj_IsDeleted(obj))
			{
				if(ObjMove_GetX(obj)<0 && di == "L")
				{
					ScatterShot(GetStgFrameWidth,ObjMove_GetY(obj),ObjMove_GetAngle(obj));
					Obj_Delete(obj);
				}
				if(ObjMove_GetX(obj)>GetStgFrameWidth && di == "R")
				{
					ScatterShot(0,ObjMove_GetY(obj),ObjMove_GetAngle(obj));
					Obj_Delete(obj);
				}
				if(ObjMove_GetY(obj)<0 && di == "U")
				{
					ScatterShot(ObjMove_GetX(obj),GetStgFrameHeight,ObjMove_GetAngle(obj));
					Obj_Delete(obj);
				}
				if(ObjMove_GetY(obj)>GetStgFrameHeight && di == "D")
				{
					ScatterShot(ObjMove_GetX(obj),0,ObjMove_GetAngle(obj));
					Obj_Delete(obj);
				}
				if(count>150){ObjShot_FadeDelete(obj);}
				count++;
				yield;
			}
			task ScatterShot(px,py,dir)
			{
				PlaySound("BulletWave",100,0);
				ascent(i in 0..5)
				{
					let spd = 6-(|i|);
					CreateShotA2(px,py,spd,dir+i*4,-spd/90,spd/3,ARROWHEAD_WHITE,10);
					CreateShotA2(px,py,spd,dir-i*4,-spd/90,spd/3,ARROWHEAD_WHITE,10);
				}
			}
		}
	
}

task BounceLaser(d)
{
	di = d;
	if(d=="L"){StaticLaser(0,-30,90);}
	if(d=="R"){StaticLaser(GetStgFrameWidth,-30,90);}
	if(d=="U"){StaticLaser(-30,0,0);}
	if(d=="D"){StaticLaser(-30,GetStgFrameHeight,0);}
	task StaticLaser(px,py,angle)
	{
		let sl = CreateStraightLaserA1(px,py,angle,500,30,240,ST_LASER_GRAY,0);
	}
	wait(240);
	di = "A";
}


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