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

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(3500)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(45)
	}
}

@MainLoop{
	frame++;
	if(signal!=1){
		spinangle++;
	}else{
		spinangle--;
		}
	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);
	SetShotAutoDeleteClip(8,8,8,8);
	ObjEnemyBossScene_StartSpell(objScene);
	ObjMove_SetDestAtWeight(objBoss,stgx,stgy*2/3,10,5);
	TEnemyHitboxStart(objBoss,120);
	Cutin("Nightbird\"Glimpse of Illusionary Light\"",16);
	TMove;
	TShoot01;
	TShoot02;
	TEnd;
	yield;
}

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		TExplosionA(GetEnemyX,GetEnemyY,10,0.6);
		SetShotAutoDeleteClip(64,64,64,64);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		Obj_Delete(objBoss);
		loop(90){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}
	
task TMovement{
	wait(120);
	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;
			}else{
				posx=-15;
			}
		}
		if((GetEnemyX+posx)<=32||(GetEnemyX+posx)>=(GetStgFrameWidth()-32)){
			posx=-posx;
		}
		
		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(90);
	}
}

task TMove{
	wait(120);
	TMovement;
	let bullets=[2.0,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,3.0];
	let angles=[-15,-8,-3,0,3,8,15];
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		wait(120);
		charge=TCharge01(GetEnemyX,GetEnemyY,0.3,3,3);
		SetColor(charge,"VIOLET","BRIGHT");
		PlaySound(0,90,0);
		if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=0){
			Obj_Delete(charge);
			break;
		}
		wait(60);
		let angle=-45;
		while(angle<=45){
			angle+=22.5;
			ascent(i in 0..length(bullets)){
				ascent(k in 0..length(angles)){
					CreateShotA2(GetEnemyX,GetEnemyY,bullets[i]*3,angle+angleToPlayer-22.5+angles[k],-0.2,bullets[i],DOT_RED,15);
					}
				}
			}
		let angle2=0;
		while(angle2<360){
			angle2+=360/66;
			CreateShotA1(GetEnemyX,GetEnemyY,0.5,angle2+angleToPlayer,OVAL_AQUA,15);
			}
		wait(15);
		PlaySound(11,100,0);
		signal=-signal;
	}
}

task TShoot01{
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=360/3;
			let r=sin(spinangle/1.5)*50;
			TShot01(GetEnemyX+r*cos(angle+-spinangle*2),GetEnemyY+r*sin(angle+-spinangle*2),2.5,angle+spinangle*2,ICE_GREEN,5,true);
			}
		wait(3);
		}
	}
	
task TShoot02{
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=360/3;
			let r=sin(spinangle/1.5)*50;
			TShot01(GetEnemyX+r*cos(60+angle+spinangle*2),GetEnemyY+r*sin(60+angle+spinangle*2),2.5,60+rand(-20,20)+angle+-spinangle*2,ICE_PURPLE,5,false);
			}
		PlaySound(9,85,0);
		wait(2);
		}
	}

task TShot01(x,y,s,dir,g,d,i){
	let obj=CreateShotA1(x,y,s,dir,g,d);
	let alpha1=255;
	let alpha2=128;
	while(!Obj_IsDeleted(obj)){
		if(i==false&&signal==1){	
			ObjRender_SetAlpha(obj,128);
			ObjShot_SetIntersectionEnable(obj,false);
			}
		if(i==false&&signal==-1){
			ObjRender_SetAlpha(obj,255);
			ObjShot_SetIntersectionEnable(obj,true);
			}
		if(i==true&&signal==1){
			ObjRender_SetAlpha(obj,255);
			ObjShot_SetIntersectionEnable(obj,true);
			}
		if(i==true&&signal==-1){
			ObjRender_SetAlpha(obj,128);
			ObjShot_SetIntersectionEnable(obj,false);
			}
		yield;
		}
}

task TAngle(shot,s,a){
	while(!Obj_IsDeleted(shot)){
		if(ObjMove_GetSpeed(shot)==0){
		ObjMove_SetAngle(shot,ObjMove_GetAngle(shot)+a);
		ObjMove_SetSpeed(shot,3);
		ObjMove_SetAcceleration(shot,0);
		break;
		}
	yield;
	}
}