#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Rumia Spell 7"]
#Text["."]
#include"./Functions/Index.txt"
#include"script/Rumia Boss/ZUNShot_Const.txt"
#include"script/Default_system/Default_Effect.Txt"

let objScene=GetEnemyBossSceneObjectID();

let objBoss;
let s=0;
let fam1;
let fam2;
let fam3;
let fam4;
let frame=0;
let sangle=140;
let sangle2=-140;
let GCSD=GetCurrentScriptDirectory~"";
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let waitframe;

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(5000)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(50)
	}
}

@MainLoop{
	frame++;
	sangle++;
	sangle2+=5;
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),25);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),27);
	
	if(IsPlayerSpellActive){
		ObjEnemy_SetDamageRate(objBoss,0,0);
	}else{
		ObjEnemy_SetDamageRate(objBoss,100,100);
	}
	
	yield;

}

task mainTask{
	TLoadSounds;
	objBoss=TDrawBoss;
	wait(30);
	ObjEnemyBossScene_StartSpell(objScene);
	ObjMove_SetDestAtWeight(objBoss,stgx,GetStgFrameHeight()/4,10,5);
	TEnemyHitboxStart(objBoss,120);
	Cutin("Fluttering Illusions\"Semi-Invisible Shadows -Night-\"",14);
	TShoot01;
	TShoot02;
	TShoot03;
	TEnd;
	yield;
}

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		TExplosionA(GetEnemyX,GetEnemyY,10,0.6);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		Obj_Delete(objBoss);
		Obj_Delete(fam1);
		Obj_Delete(fam2);
		Obj_Delete(fam3);
		Obj_Delete(fam4);
		loop(90){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}

task TShoot01{
	wait(70);
	let bullets=[1.5,1.55,1.60,1.65,1.70];
	let obj;
	fam1=FDrawFamiliar(objBoss,GetEnemyX,GetEnemyY,0,0,0,2.5,255,255,255,255);
	TMove(fam1);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		ascent(i in 0..length(bullets)){
			obj=CreateShotA1(ObjMove_GetX(fam1),ObjMove_GetY(fam1),bullets[i],s*1.01,S_STAR_PURPLE,15);
			obj=CreateShotA1(ObjMove_GetX(fam1),ObjMove_GetY(fam1),bullets[i],-s*1.01,S_STAR_PURPLE,15);
			let angle=ObjMove_GetAngle(obj);
		}
		PlaySound(9,80,0);
		wait(4);
	}
}

task TShoot02{
	wait(300);
	fam2=FDrawFamiliar(objBoss,GetEnemyX,GetEnemyY,0,0,0,2.5,255,255,255,255);
	let obj;
	let signal=1;
	let laserlength=300;
	ObjMove_SetDestAtWeight(fam2,stgx,stgy,10,5);
	wait(70);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
			let angle=0;
			while(angle<360){
				angle+=(360/10);
				let playerangle=atan2(GetPlayerY-ObjMove_GetY(fam2),GetPlayerX-ObjMove_GetX(fam2));
				obj=CreateStraightLaserA1(ObjMove_GetX(fam2),ObjMove_GetY(fam2),angle+playerangle,laserlength,15,30,LARGE_AQUA,90);
				}
			PlaySound(5,90,90);
			wait(60);
			let angle2=0;
			while(angle2<360){
				angle2+=(360/10);
				let obj2=CreateShotA2(ObjMove_GetX(fam2)+25*cos(angle2),ObjMove_GetY(fam2)+25*sin(angle2),3,angle2,-0.1,1.5,PELLET_BLUE,30);
			}
			wait(60);
			if(signal==1){
				signal=2;
				laserlength=75;
				ObjMove_SetDestAtFrame(fam2,GetPlayerX,GetPlayerY,40);
			}else{
				signal=1;
				laserlength=300;
				ObjMove_SetDestAtFrame(fam2,stgx,stgy,40);
			}
			wait(49);
	}
}

task TShoot03{
	wait(600);
	fam3=FDrawFamiliar(objBoss,GetEnemyX,GetEnemyY,0,0,0,2.5,255,255,255,255);
	fam4=FDrawFamiliar(objBoss,GetEnemyX,GetEnemyY,0,0,0,2.5,255,255,255,255);
	let obj;
	ObjMove_SetDestAtWeight(fam3,0,stgy,15,5);
	ObjMove_SetDestAtWeight(fam4,stgx*2,stgy,15,5);
	wait(70);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>=0){
		CreateShotB2(ObjMove_GetX(fam3),ObjMove_GetY(fam3),sin(sangle2)/1.2,-7,0,0.1,0,2,DROP_RED,15);
		CreateShotB2(ObjMove_GetX(fam4),ObjMove_GetY(fam4),sin(-sangle2)/1.2,-7,0,0.1,0,2,DROP_RED,15);
		wait(7);
	}
}

task TMove(obj){
	let xr=0;
	let yr=0;
	let sp=0;
	while((ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)){
		ObjMove_SetPosition(obj,GetEnemyX+xr*cos(s),GetEnemyY+yr*sin(s));
		s+=3;
		if(xr==160&&yr==80){
			xr+=0;
			yr+=0;
		}else{
			xr+=1;
			yr+=0.5;
		}
		yield;
		}
	}