#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Rumia Spell 6"]
#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 gsignal=1;
let fam1;
let fam2;
let waitframes=60;
let GCSD=GetCurrentScriptDirectory~"";
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let waitframe;

@Initialize{
	mainTask;
}

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

@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,25);
	TEnemyHitboxStart(objBoss,120);
	Cutin("Dangerous Darkness\"Kanabo Shrouded in Darkness\"",14);
	TShoot01;
	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);
		Obj_Delete(fam1);
		loop(90){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}

task TShoot01{
	wait(60);
	ObjMove_SetDestAtWeight(objBoss,stgx,GetStgFrameHeight()/10,10,25);
	wait(60);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		loop(200){
			TShot01;
			}
		PlaySound(8,90,0);
		fam1=FDrawFamiliar(objBoss,GetPlayerX,GetPlayerY,0,0,0,2.5,255,255,255,255);
		fam2=FDrawFamiliar(objBoss,GetPlayerX,GetPlayerY,0,0,0,-2.5,255,255,255,255);
		TMove;
		TSize(fam1,3,3,90);
		TSize(fam2,3,3,90);
		wait(30);
		PlaySound(0,90,0);
		ObjRender_SetColor(fam1,255,192,192);
		ObjRender_SetColor(fam2,255,192,192);
		wait(30);
		ObjRender_SetColor(fam1,255,128,128);
		ObjRender_SetColor(fam2,255,128,128);
		wait(30);
		gsignal=2;
		let resultx=GetPlayerX;
		let resulty=GetPlayerY;
		ObjMove_SetDestAtFrame(objBoss,resultx,resulty,45);
		wait(45);
		gsignal=0;
		Obj_Delete(fam1);
		Obj_Delete(fam2);
		gsignal=1;
		yield;
	}
}

task TShot01{
	let mxspeed=rand(0.5,2);
	let obj=CreateShotA2(GetEnemyX,GetEnemyY,1.5,rand(0,360),-mxspeed/60,mxspeed/6,round(rand(162,176)),5);
	wait(90);
	while(!Obj_IsDeleted(obj)){
		let angle=atan2(ObjMove_GetY(obj)-GetEnemyY,ObjMove_GetX(obj)-GetEnemyX);
		if(ObjMove_GetX(obj)<=(GetEnemyX+30)&&ObjMove_GetX(obj)>=(GetEnemyX-30)||ObjMove_GetY(obj)<=(GetEnemyY+30)&&ObjMove_GetY(obj)>=(GetEnemyY-30)){
			ObjMove_SetAngle(obj,angle);
			ObjMove_SetAcceleration(obj,0.5);
			ObjMove_SetMaxSpeed(obj,6);
		}else{
			ObjMove_SetAcceleration(obj,-0.1);
			ObjMove_SetMaxSpeed(obj,mxspeed);
			}
		yield;
		}
}

task TMove{
	let alpha=255;
	let scale=1;
	while(gsignal==1){
		ObjMove_SetDestAtWeight(fam1,GetPlayerX,GetPlayerY,5,100);
		ObjMove_SetDestAtWeight(fam2,GetPlayerX,GetPlayerY,5,100);
		yield;
		}
	while(gsignal==2){
		yield;
	}
	if(gsignal==0){
		loop(20){
		alpha-=12.75;
		scale-=0.15;
		ObjRender_SetAlpha(fam1,alpha);
		ObjRender_SetScaleXYZ(fam1,scale,scale,0);
		ObjRender_SetAlpha(fam2,alpha);
		ObjRender_SetScaleXYZ(fam2,scale,scale,0);
		yield;
	}
}
}

task TSize(obj,x,y,t){
	ObjRender_SetScaleXYZ(obj,x,y,0);
	let size=x;
	loop(t){
		ObjRender_SetScaleXYZ(obj,size,size,0);
		size-=x/t;
		yield;
		}
	}