#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["FlanSpell02"]
#Text["FlanSpell02"]
#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_flandre.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("7 Lävatein", cutinimage, NAZRIN, 240, 104, 104);
	wait(30);
	objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	ObjCutin_LaunchS3(YABUSAME, cutinimage, "Normal");
	wait(90);
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		LWave;
		wait(250);
		BorderMove(90,GetStgFrameWidth-90,90,150,10,150);
		wait(150);
	}
}

task LWave
{
	let mcolor = [PELLET_RED,PELLET_ORANGE,PELLET_YELLOW,PELLET_GREEN,PELLET_AQUA,PELLET_BLUE,PELLET_PURPLE];
	PlaySound("Charge2",100,0);
	SakuraCharge("RED");
	wait(60);
	PlaySound("Laser2",100,0);
	PlaySound("Eff1",100,75);
	ascent(i in 0..7)
	{
		Laser(i*360/7+GetAngleToPlayer(objEnemy));
	}
	task Laser(angle)
	{
		let px = GetEnemyX+0*cos(angle);
		let py = GetEnemyY+0*sin(angle);
		let tx = GetEnemyX+500*cos(angle);
		let ty = GetEnemyY+500*sin(angle);
		let l = CreateStraightLaserA1(px,py,angle,500,30,120,L_LASER_RED,75);
		ascent(i in 0..60)
		{
			ObjLaser_SetLength(l,i*500/60);
			yield;
		}
		wait(15);
		let arr = GetLineBorderPoint(px, py, tx, ty);
		let result = rand(0,360/18);
		ascent(i in 0..18)
		{
			BounceShot(arr[0],arr[1],i*360/18+result,PELLET_RED,20);
		}
		wait(120);
		descent(i in 0..30)
		{
			ObjLaser_SetRenderWidth(l,i);
			yield;
		}
	}
	task BounceShot(x,y,angle,color,delay)
	{
		let bshot = CreateShotA2(x,y,0,angle,2.5/120,2.5,color,delay);
		let count = 0;
		let dangle = 0;
		wait(30);
		if(ObjMove_GetX(bshot)<0 || ObjMove_GetX(bshot)>GetStgFrameWidth || ObjMove_GetY(bshot)<0 || ObjMove_GetY(bshot)>GetStgFrameHeight)
		{Obj_Delete(bshot);}
		while(ObjMove_GetX(bshot)>0 && ObjMove_GetY(bshot)>0 && ObjMove_GetX(bshot)<GetStgFrameWidth && ObjMove_GetY(bshot)<GetStgFrameHeight)
		{yield;}
		if(ObjMove_GetX(bshot)<0 || ObjMove_GetX(bshot)>GetStgFrameWidth){dangle = 180-ObjMove_GetAngle(bshot);}
		if(ObjMove_GetY(bshot)<0 || ObjMove_GetY(bshot)>GetStgFrameHeight){dangle = -ObjMove_GetAngle(bshot);}
		let rc = round(rand(0,6));
		if(!Obj_IsDeleted(bshot)){PlaySound("Shot9",100,0);CreateShotA2(ObjMove_GetX(bshot),ObjMove_GetY(bshot),0,dangle,2.5/120,2.5,mcolor[rc],15);}
		Obj_Delete(bshot);
	}
}


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