#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Rumia Spell 3"]
#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 speed=3;
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(50)
	}
}

@MainLoop{
	frame++;
	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("Darkness Sign\"Disorderly Conducted Nightfall\"",16);
	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);
		loop(90){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}
	
task TShoot01{
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=360/5;
			TShot01(GetEnemyX,GetEnemyY,0,angle+angleToPlayer,0.1,5,LARGE_BLUE,25);
			}
		PlaySound(8,90,0);
		wait(300);
		}
	}
		

task TShot01(x,y,s,dir,ac,mx,g,d){
	let obj=CreateShotA2(x,y,s,dir,ac,mx,g,d);
	let obj2;
	let signal=1;
	let count=0;
	while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetY(obj)<=0&&signal==1){
			ObjShot_SetGraphic(obj,ARROWHEAD_YELLOW);
			ObjMove_SetSpeed(obj,0);
			ObjMove_SetMaxSpeed(obj,speed);
			ObjMove_SetAcceleration(obj,0.05);
			ObjMove_SetAngle(obj,90);
			TLaser01(ObjMove_GetX(obj),ObjMove_GetY(obj),0,90,250,10,G_LARGE_YELLOW);
			signal=2;
			wait(60);
				loop(15){
				Bullets01(ObjMove_GetX(obj),ObjMove_GetY(obj),3,rand(0,360),-0.1,0,PELLET_BLUE,5);
				wait(5);
				}
			}
		if(ObjMove_GetY(obj)>=stgy*2&&signal==1){
			ObjShot_SetGraphic(obj,ARROWHEAD_YELLOW);
			ObjMove_SetSpeed(obj,0);
			ObjMove_SetMaxSpeed(obj,speed);
			ObjMove_SetAcceleration(obj,0.05);
			ObjMove_SetAngle(obj,270);
			TLaser01(ObjMove_GetX(obj),ObjMove_GetY(obj),0,270,250,10,G_LARGE_YELLOW);
			signal=2;
			wait(60);
				loop(15){
				Bullets01(ObjMove_GetX(obj),ObjMove_GetY(obj),3,rand(0,360),-0.1,0,PELLET_BLUE,5);
				wait(5);
				}
			}
		if(ObjMove_GetX(obj)<=0&&signal==1){
			ObjShot_SetGraphic(obj,ARROWHEAD_YELLOW);
			ObjMove_SetSpeed(obj,0);
			ObjMove_SetMaxSpeed(obj,speed);
			ObjMove_SetAcceleration(obj,0.05);
			ObjMove_SetAngle(obj,0);
			TLaser01(ObjMove_GetX(obj),ObjMove_GetY(obj),0,0,250,10,G_LARGE_YELLOW);
			signal=2;
			wait(60);
				loop(15){
				Bullets01(ObjMove_GetX(obj),ObjMove_GetY(obj),3,rand(0,360),-0.1,0,PELLET_BLUE,5);
				wait(5);
				}
			}
		if(ObjMove_GetX(obj)>=stgx*2&&signal==1){
			ObjShot_SetGraphic(obj,ARROWHEAD_YELLOW);
			ObjMove_SetSpeed(obj,0);
			ObjMove_SetMaxSpeed(obj,speed);
			ObjMove_SetAcceleration(obj,0.05);
			ObjMove_SetAngle(obj,180);
			TLaser01(ObjMove_GetX(obj),ObjMove_GetY(obj),0,180,250,10,G_LARGE_YELLOW);
			signal=2;
			wait(60);
				loop(10){
				Bullets01(ObjMove_GetX(obj),ObjMove_GetY(obj),3,rand(0,360),-0.1,0,PELLET_BLUE,5);
				PlaySound(9,90,0);
				wait(5);
				}
			}
		yield;
		}
		
	task TLaser01(x,y,s,dir,l,w,g){
		obj2=CreateLooseLaserA1(x,y,s,dir,l,w,g,25);
			ObjMove_SetSpeed(obj2,speed);
			}
	task Bullets01(x,y,s,dir,a,max,g,d){
		let obj3=CreateShotA2(x,y,s,dir,a,max,g,d);
		while(!Obj_IsDeleted(obj3)){
			if(ObjMove_GetSpeed(obj3)==0){
				CreateShotB2(ObjMove_GetX(obj3),ObjMove_GetY(obj3),rand(-1.5,1.5),-2,0,0.05,1,2,PELLET_RED,15);
				CreateShotB2(ObjMove_GetX(obj3),ObjMove_GetY(obj3),rand(-1.5,1.5),-2,0,0.05,-1,2,PELLET_RED,15);
				PlaySound(2,90,0);
				wait(60);
				ObjMove_SetAngle(obj3,atan2(GetPlayerY-ObjMove_GetY(obj3),GetPlayerX-ObjMove_GetX(obj3))+rand(-10,10));
				ObjMove_SetAcceleration(obj3,0.05);
				ObjMove_SetMaxSpeed(obj3,3);
				break;
				}
			yield;
			}
	}
}

task TShoot02{
	wait(90);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=-90;
		while(angle<=90){
			angle+=10;
			CreateShotA1(GetEnemyX,GetEnemyY,3.5,angle-100+rand(-2.5,2.5),LARGE_GREEN,25);
			}
		wait(15);
		}
	}

task TShoot03{
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=360/3;
			let obj=CreateShotA2(GetEnemyX,GetEnemyY,10,angle+angleToPlayer,-0.25,3,BUBBLE_PURPLE,25);
			SetBulletTrailDelay(obj,30,25,"PURPLE","BRIGHT",255,true,true);
			ObjMove_SetAngularVelocity(obj,0.25);
			let obj2=CreateShotA2(GetEnemyX,GetEnemyY,10,angle+angleToPlayer,-0.25,3,BUBBLE_PURPLE,25);
			SetBulletTrailDelay(obj2,30,25,"PURPLE","BRIGHT",255,true,true);
			ObjMove_SetAngularVelocity(obj2,-0.25);
			}
		PlaySound(10,90,25);
		wait(200);
		}
	}