#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Rumia's Last Word"]
#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=0;
let angleacc=0;
let sina=-120;
let radius=320;
let size=25;
let charge;
let signal=1;
let GCSD=GetCurrentScriptDirectory~"";
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(5500)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(40)
	}
	case(EV_REQUEST_IS_LAST_SPELL){
		SetScriptResult(true)
	}
}

@MainLoop{
	frame++;
	sina++;
	size+=0.015;
	if(radius>150){
		radius-=0.25;
		}
	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,stgy*2/3,10,5);
	TEnemyHitboxStart(objBoss,120);
	CutinLast("\"Rumia -Forbidden Demise-\"",16);
	TShoot01;
	TShoot02;
	TShoot03;
	TShoot04;
	TEnd;
	yield;
}

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){ yield; }
		StartSlow(TARGET_ALL,15);
		TMotionBlur(signal);
		loop(9){
			TExplosionA(GetEnemyX+rand(-25,25),GetEnemyY+rand(-25,25),10,0.6);
			PlaySoundNR(10,95,0);
			wait(10);
			}
		StopSlow(TARGET_ALL);
		loop(3){
			TExplosionA(GetEnemyX+rand(-25,25),GetEnemyY+rand(-25,25),10,0.6);
			PlaySoundNR(10,95,0);
			wait(10);
			}
		TExplosionA(GetEnemyX,GetEnemyY,15,5);
		PlaySoundNR(1,100,0);
		Obj_Delete(objBoss);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		signal=2;
		wait(360);
		CloseScript(GetOwnScriptID());
		return;
	}

task TShoot01{
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0&&signal==1){
		let angle=0;
		while(angle<360){
			angle+=360/10;
			CreateShotA1(stgx+(sin(sina)*size+radius)*cos(angle+spinangle),stgy+(sin(sina)*size+radius)*sin(angle+spinangle),5,angle+spinangle,ICE_GREEN,5);
			spinangle+=angleacc;
			angleacc+=0.0001;
			}
		wait(1);
		}
	}

task TShoot02{
	wait(120);
	let delay=45;
	let speed=1;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0&&signal==1){
		let angle=0;
		while(angle<stgx*2){
			angle+=stgx*2/15;
			let obj=CreateStraightLaserA1(angle-stgx/7.5,0,90,600,10,60,ST_LASER_BLUE,60);
			ObjStLaser_SetAngle(obj,getAtanAngle(obj,objPlayer));
			if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=0){ break; }
			PlaySound(5,90,60);
			wait(3);
		}
		wait(120);
	}
}

task TShoot03{
	wait(120);
	let bullets=20;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0&&signal==1){
		let angle=-45;
		while(angle<45){
			angle+=bullets;
			let obj=CreateShotA2(GetEnemyX,GetEnemyY,0,angle-90,0.1,5,SMALL_GREEN,15);
			if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=0){ break; }
			PlaySound(8,85,0);
			TShot(obj);
			wait(1);
			}
		let angle2=45;
		while(angle2>-45){
			angle2-=bullets;
			let obj=CreateShotA2(GetEnemyX,GetEnemyY,0,angle2-90,0.1,5,SMALL_GREEN,15);
			if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=0){ break; }
			PlaySound(8,85,0);
			TShot(obj);
			wait(1);
		}
		if(bullets>5){
			bullets-=2.5;
			}
		wait(120);
	}
	task TShot(obj){
		let signal2=1;
		while(!Obj_IsDeleted(obj)){
			if(ObjMove_GetY(obj)<=0&&signal2==1){
				ObjShot_SetGraphic(obj,SMALL_YELLOW);
				ObjMove_SetSpeed(obj,0);
				ObjMove_SetAngle(obj,90+rand(-10,10));
				ObjMove_SetMaxSpeed(obj,2.5);
				ObjMove_SetAcceleration(obj,0.05);
				signal2=2;
				PlaySound(2,85,0);
				}
			yield;
		}
	}
}

task TShoot04{
	wait(120);
	let bullets=20;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0&&signal==1){
		let angle=-90;
		while(angle<90){
			angle+=5;
			let obj=CreateShotA2(GetEnemyX,GetEnemyY,5,-angle-90,-0.1,0,AMULET_RED,15);
			ObjMove_AddPatternA4(obj,50,0,0,0.1,0,1.5,GetPlayerObjectID,NO_CHANGE);
			
			let obj2=CreateShotA2(GetEnemyX,GetEnemyY,5,angle-90,-0.1,0,AMULET_RED,15);
			ObjMove_AddPatternA4(obj2,50,0,0,0.1,0,1.5,GetPlayerObjectID,NO_CHANGE);
			
			let obj3=CreateShotA2(GetEnemyX,GetEnemyY,5,angle-90,-0.1,0,AMULET_RED,15);
			ObjMove_AddPatternA4(obj3,50,0,22.5,0.1,0,3,GetPlayerObjectID,NO_CHANGE);
			
			let obj4=CreateShotA2(GetEnemyX,GetEnemyY,5,angle-90,-0.1,0,AMULET_RED,15);
			ObjMove_AddPatternA4(obj4,50,0,-22.5,0.1,0,3,GetPlayerObjectID,NO_CHANGE);
			if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=0){ break; }
			PlaySound(9,85,0);
			wait(1);
		}
		wait(90);
		CreateShotA1(GetEnemyX,GetEnemyY,1,angleToPlayer,BUBBLE_GREEN,15);
		wait(90);
		}
	}