#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Rumia Spell 10"]
#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 charge;
let death;
let signal=1;
let spinangle=0;
let spinangle2=0;
let spinangle3=0;
let spinangle4=0;
let spinangle5=0;
let spinangle6=0;
let spinangle7=0;
let speed=0;
let GCSD=GetCurrentScriptDirectory~"";
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(10000)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(240)
	}
}

@MainLoop{
	frame++;
	spinangle++;
	spinangle2++;
	spinangle3++;
	spinangle4++;
	spinangle5++;
	spinangle6++;
	spinangle7++;
	speed+=10;
	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;
	ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjMove_SetDestAtWeight(objBoss,stgx,GetStgFrameHeight()/2,15,10);
	wait(45);
	PlaySound(0,90,15);
	loop(3){
		wait(15);
		charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
		SetColor(charge,"VIOLET","BRIGHT");
		}
	wait(30);
	ObjEnemyBossScene_StartSpell(objScene);
	ObjMove_SetDestAtWeight(objBoss,stgx,GetStgFrameHeight()/3,10,5);
	TEnemyHitboxStart(objBoss,90);
	Cutin("\"Legendary Nightbird -Night's Demarcation-\"",16);
	TShoot01;
	TShoot02;
	TShoot03;
	TShoot04;
	TShoot05;
	TShoot06;
	TShoot07;
	TShoot08;
	TEnd;
	yield;
}

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		TExplosionA(GetEnemyX,GetEnemyX,10,0.6);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		Obj_Delete(objBoss);
		SetColor(charge,"VIOLET","BRIGHT");
		loop(90){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}

task TMovement{
	wait(120);
	let count=0;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let resultx=rand(0,1);
		resultx=round(resultx);
		let resulty=rand(0,1);
		resulty=round(resulty);
		let posx=rand(50,-50);
		if(posx<=10&&posx>=-10){
			if(resultx==1){
				posx=15;
				count++;
			}else{
				posx=-15;
				count--;
			}
		}
		if((GetEnemyX+posx)<=32||(GetEnemyX+posx)>=(GetStgFrameWidth()-32)){
			posx=-posx;
		}
		if(count==3){
			posx-=50;
			count-=3;
		}
		if(count==-3){
			posx+=50;
			count+=3;
		}
		let posy=rand(20,-20);
		if(posy<=7.5&&posy>=-7.5){
			if(resulty==1){
				posy=10;
			}else{
				posy=-10;
			}
		}
		if((GetEnemyY+posy)<=32||(GetEnemyY+posy)>=GetStgFrameHeight()/3){
			posy=-posy;
		}
		ObjMove_SetDestAtWeight(objBoss,GetEnemyX+posx,GetEnemyY+posy,10,5);
		wait(120);
	}
}

task TShoot01{
	wait(60);
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"RED","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	spinangle=0;
	speed=0;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=360/2;
			CreateShotA1(GetEnemyX,GetEnemyY,sin(speed)+1*2,spinangle*3.13+angle,PELLET_RED,5);
			}
		if(signal==1){
			PlaySound(9,80,0);
			}
		wait(3);
	}
}

task TShoot02{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>8750){ yield; }
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"ORANGE","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	TMovement;
	spinangle2=0;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=-15;
		while(angle<=15){
			angle+=30;
			CreateShotA1(GetEnemyX,GetEnemyY,2,spinangle2*4.13+angle-15,BUBBLE_ORANGE,25);
			}
		wait(10);
	}
}

task TShoot03{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>7500){ yield; }
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"YELLOW","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	spinangle3=0;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=-22.5;
		while(angle<=22.5){
			angle+=22.5;
			CreateShotA1(GetEnemyX,GetEnemyY,1,spinangle3*1.11+angle-22.5,S_STAR_YELLOW,5);
			}
		wait(6);
	}
}

task TShoot04{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>6250){ yield; }
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"GREEN","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	spinangle4=0;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		CreateShotA1(GetEnemyX+25*cos(-spinangle4*9.13),GetEnemyY+25*sin(-spinangle4*9.13),3,-spinangle4*9.13,SUPP_GREEN,15);
		wait(2);
	}
}

task TShoot05{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>5000){ yield; }
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"AQUA","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	spinangle5=0;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=-1.875;
		while(angle<=1.875){
			angle+=3.75;
			CreateShotA1(GetEnemyX+25*cos(angle+-spinangle5*2.13),GetEnemyY+25*sin(angle+-spinangle5*2.13),2.25,angle+-spinangle5*2.13-15,AMULET_AQUA,15);
			}
		wait(6);
	}
}

task TShoot06{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>3750){ yield; }
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"BLUE","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	spinangle6=0;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=-45;
		while(angle<=45){
			angle+=11.25;
			CreateShotA1(GetEnemyX,GetEnemyY,1.75,angle+spinangle6*3.65-11.25,ARROWHEAD_BLUE,15);
			}
		wait(3);
	}
}

task TShoot07{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>2750){ yield; }
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	SetColor(charge,"VIOLET","BRIGHT");
	PlaySound(0,90,0);
	wait(60);
	spinangle7=0;
	let n=3;
	let d=7;
	let t=10.31;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=360/4;
		CreateShotA1(GetEnemyX+100*cos(n/d*-spinangle7*t)*sin(-spinangle7*t+angle),GetEnemyY+100*sin(n/d*-spinangle7*t)*sin(-spinangle7*t+angle),1.75,-spinangle7*t+angle,BL_SMALL_PURPLE,15);
		}
		if(signal==1){
		PlaySound(2,80,0);
		}
		wait(6);
	}
}

task TShoot08{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>1750){ yield; }
	charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
	PlaySound(0,90,0);
	signal=2;
	loop(60){
		DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
		yield;
		}
	signal=1;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		CreateShotA1(GetEnemyX,GetEnemyY,rand(1,4),rand(-160,160)+angleToPlayer,DOT_GRAY,15);
		CreateShotA1(GetEnemyX,GetEnemyY,rand(1,4),rand(-160,160)+angleToPlayer,DOT_GRAY,15);
		wait(1);
	}
}