#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Rumia Spell 9"]
#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 charge;
let signal=1;
let GCSD=GetCurrentScriptDirectory~"";
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;

@Initialize{
	mainTask;
	ObjRender_SetAlpha(objBoss,128);
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(5000)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(45)
	}
	case(EV_REQUEST_IS_DURABLE_SPELL){
		SetScriptResult(true)
	}
}

@MainLoop{
	frame++;
	spinangle++;
	
	yield;

}

task mainTask{
	TLoadSounds;
	objBoss=TDrawBoss;
	wait(30);
	ObjEnemyBossScene_StartSpell(objScene);
	ObjMove_SetDestAtWeight(objBoss,stgx,GetStgFrameHeight()/3,10,5);
	TEnemyHitboxStart(objBoss,120);
	Cutin("Darkness Sign\"Nightbird's Egg\"",16);
	TShoot01;
	TShoot02;
	TShoot03;
	TShoot04;
	TShoot05;
	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);
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"VIOLET","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=-45;
		while(angle<=45){
			angle+=6;
			TShot01(angle+angleToPlayer-6+180);
			}
		if(signal==2){
			break;
			}
		PlaySound(2,90,0);
		wait(120);
	}
}

task TShoot02{
	wait(600);
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"VIOLET","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=360/3;
			CreateShotA1(GetEnemyX,GetEnemyY,2,angle+spinangle*5.13,ICE_AQUA,5);
			}
		if(signal==2){
			break;
			}
		PlaySound(8,75,0);
		wait(3);
	}
}

task TShoot03{
	wait(1100);
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"VIOLET","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	let n=7;
	let d=3;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=360/4;
			CreateShotA1(GetEnemyX+100*cos(n/d*spinangle)*sin(spinangle),GetEnemyY+100*sin(n/d*spinangle)*sin(spinangle),2,angle+spinangle*11,SMALL_BLUE,5);
			}
		if(signal==2){
			break;
			}
		wait(5);
	}
}

task TShoot04{
	wait(1600);
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"VIOLET","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	let n=7;
	let d=3;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=360/8;
			CreateShotA1(GetEnemyX+sin(spinangle)*75*cos(spinangle*8.1321+angle),GetEnemyY+sin(spinangle)*75*sin(spinangle*8.1321+angle),3,angle+spinangle*8.1321,ARROWHEAD_YELLOW,5);
			}
		if(signal==2){
			break;
			}
		wait(7);
	}
}

task TShoot05{
	wait(2000);
	signal=2;
	let obj;
	ObjMove_SetDestAtWeight(objBoss,stgx,stgy,5,5);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	loop(100){
		DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
		yield;
	}
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"VIOLET","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	loop(500){
		let rands=rand(0.5,5);
		obj=CreateShotA2(GetEnemyX,GetEnemyY,rands,rand(0,360),-rands/150,-rands*1.25,PELLET_GRAY,0);
		ObjShot_SetSpellResist(obj,true);
		ObjShot_SetAutoDelete(obj,false);
		TCheck(obj);
	}
	PlaySound(8,95,0);
	task TCheck(obj){
		while(ObjEnemyBossScene_GetInfo(objScene,INFO_TIMERF)>60){ yield; }
		ObjShot_SetAutoDelete(obj,true);
		}
}
	
task TShot01(angle){
	let obj=CreateShotA2(GetEnemyX,GetEnemyY,5,angle,-0.05,1.5,DROP_PURPLE,30);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		if(ObjMove_GetX(obj)<=GetClipMinX){
			ObjMove_SetAngle(obj,2*90-ObjMove_GetAngle(obj));
			ObjMove_SetX(obj,ObjMove_GetX(obj)+0.1);
			}
		if(ObjMove_GetX(obj)>=GetClipMaxX){
			ObjMove_SetAngle(obj,2*90-ObjMove_GetAngle(obj));
			ObjMove_SetX(obj,ObjMove_GetX(obj)-0.1);
			}
		if(ObjMove_GetY(obj)<=GetClipMinY){
			ObjMove_SetAngle(obj,2*180-ObjMove_GetAngle(obj));
			ObjMove_SetY(obj,ObjMove_GetY(obj)+0.1);
			}
			yield;
		}
}