#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Rumia Spell 8"]
#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 frame=0;
let spinangle=0;
let t=-140;
let GCSD=GetCurrentScriptDirectory~"";
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(4000)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(60)
	}
}

@MainLoop{
	frame++;
	spinangle++;
	t+=4;
	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()/2,10,5);
	TEnemyHitboxStart(objBoss,120);
	Cutin("Nightfall\"Eternal Cycle Of Darkness\"",16);
	TShoot01;
	TShoot02;
	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);
		loop(90){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}

task TShoot01{
	wait(90);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		loop(6){
			TShot01;
		}
		PlaySound(2,90,0);
		wait(360);
	}
}

task TShoot02{
	wait(140);
	let timesangle=0;
	let ad=1;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		loop(360){
			let angle=0;
			while(angle<360){
				angle+=(360/3);
				let obj=CreateShotA1(GetEnemyX,GetEnemyY,2,angle+timesangle*2,KUNAI_PURPLE,15);
				}
			if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=0){
				break;
				}
			PlaySound(9,80,0);
			wait(2);
			timesangle+=ad;
		}
		ad=-ad;
	}
}

task TShot01{
	let angle1=atan2(stgy*2-GetEnemyY,0-GetEnemyX);
	let angle2=atan2(stgy*2-GetEnemyY,stgx*2-GetEnemyX);
	let randa=rand(angle1,angle2);
	let obj=CreateShotA2(GetEnemyX,GetEnemyY,0,randa,0.1,10,LARGE_YELLOW,15);
	let obj2=CreateShotA2(GetEnemyX,GetEnemyY,0,randa,0.1,10,LARGE_YELLOW,15);
	ObjShot_SetSpellResist(obj,true);
	let signal=1;
	while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetY(obj)>=stgy*2&&signal==1){
			ObjMove_SetY(obj,stgy*2);
			ObjMove_SetSpeed(obj,0);
			ObjMove_SetAcceleration(obj,0);
			ObjMove_SetMaxSpeed(obj,0);
			ObjMove_SetY(obj2,stgy*2);
			ObjMove_SetSpeed(obj2,0);
			ObjMove_SetAcceleration(obj2,0);
			ObjMove_SetMaxSpeed(obj2,0);
			loop(3){
			let angle=0;
			while(angle<360){
				angle+=(360/6);
				let obj3=CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,angle+rand(0,360),0.01,2,G_LARGE_ORANGE,15);
				ObjShot_SetSpellResist(obj3,true);
			}
			PlaySound(10,90,0);
			wait(1);
			}
			wait(120);
			ObjMove_SetAngle(obj2,-randa);
			ObjMove_SetAngle(obj,-randa+rand(-10,10));
			ObjShot_SetGraphic(obj2,LARGE_RED);
			ObjMove_SetAcceleration(obj,0.075);
			ObjMove_SetMaxSpeed(obj,3);
			ObjMove_SetAcceleration(obj2,0.075);
			ObjMove_SetMaxSpeed(obj2,3);
			signal=2;
			}
			yield;
		}
	}