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

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(3750)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(50)
	}
}

@MainLoop{
	frame++;
	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);
	ObjEnemyBossScene_StartSpell(objScene);
	ObjMove_SetDestAtWeight(objBoss,stgx,GetStgFrameHeight()/4,10,5);
	TEnemyHitboxStart(objBoss,120);
	Cutin("Darkness Sign\"Reflective 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{
	let move=1;
	let bullets=4;
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angleplayer=angleToPlayer;
		loop(3){
			let angle=0;
			while(angle<360){
				angle+=(360/bullets);
				TShot01(2.5,angle+angleplayer+180);
			}
			wait(2);
			}
		wait(90);
		if(move==1){
			ObjMove_SetDestAtFrame(objBoss,50,GetStgFrameHeight()/3,90);
			move=0;
		}else{
			ObjMove_SetDestAtFrame(objBoss,GetStgFrameWidth()-50,GetStgFrameHeight()/3,90);
			move=1;
		}
		bullets+=2;
		if(bullets>10){
			bullets=7;
			}
		wait(150);
	}
}
task TShoot02{
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=(360/2);
			CreateShotA2(GetEnemyX+50*cos(spinangle*3+angle),GetEnemyY+50*sin(spinangle*3+angle),10,angle+spinangle*3,-0.5,1.5,KUNAI_ORANGE,5);
			}
		PlaySound(9,80,0);
		wait(3);
	}
}
	
task TShot01(speed,dir){
	let graph=98;
	let signal=2;
	let obj=CreateShotA1(GetEnemyX,GetEnemyY,speed,dir,graph,15);
	while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetX(obj)>=GetClipMaxX||ObjMove_GetX(obj)<=GetClipMinX&&signal>=1){
				ObjMove_SetAngle(obj,2*90-ObjMove_GetAngle(obj));
				graph=+graph+2;
				signal-=1;
				ObjShot_SetGraphic(obj,graph);
			}
			if(ObjMove_GetY(obj)<=GetClipMinY&&signal>=1){
				ObjMove_SetAngle(obj,2*180-ObjMove_GetAngle(obj));
				graph=+graph+2;
				signal-=1;
				ObjShot_SetGraphic(obj,graph);
			}
			if(((ObjMove_GetX(obj)>=GetClipMaxX||ObjMove_GetX(obj)<=GetClipMinX)||ObjMove_GetY(obj)<=GetClipMinY)&&signal==0){
				ObjMove_SetMaxSpeed(obj,4);
				ObjMove_SetSpeed(obj,0);
				ObjMove_SetAcceleration(obj,0.05);
				signal-=1;
				let angle=atan2(GetPlayerY - ObjMove_GetY(obj)+rand(-5,5),GetPlayerX - ObjMove_GetX(obj))+rand(-5,5);
				ObjMove_SetAngle(obj,angle);
				let obj2=CreateLooseLaserA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2.5,angle,150,10,G_LARGE_PURPLE,15);
				PlaySound(6,90,15);
				Obj_Delete(obj);
				}
		yield;
	}
}