#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["S6"]
#Text["."]
#System["./System.txt"]
#include"./Functions/Index.txt"

let objScene=GetEnemyBossSceneObjectID();
let objBoss;
let frame=0;
let rCenter;
let GCSD=GetCurrentScriptDirectory~"";

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(3750)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(105)
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(10000)
	}
}

@MainLoop{
	frame++;
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),24);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	yield;
	
}

task mainTask{
	TLoadSounds;
	objBoss=TDrawBoss;
	TEnemyHitboxStart(objBoss,480);
	wait(90);
	PlaySound(objBoss,23,100,0);
	ObjEnemyBossScene_StartSpell(objScene);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/3,10,5);
	Cutin(objBoss,"Permanent Sea \"Streamline-like Sinkholes\"");
	TShoot01;
	TShoot02;
	TEnd;
	yield;
}

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		TMotionBlur;
		StartSlow(TARGET_ALL,10);
		loop(5){
			TExplosionA(ObjMove_GetX(objBoss)+rand(-50,50),ObjMove_GetY(objBoss)+rand(-50,50),10,0.3);
			PlaySoundNR(12,100,0);
			wait(5);
		}
		StopSlow(TARGET_ALL);
		loop(5){
			TExplosionA(ObjMove_GetX(objBoss)+rand(-50,50),ObjMove_GetY(objBoss)+rand(-50,50),10,0.3);
			PlaySoundNR(12,100,0);
			wait(5);
		}
		loop(15){
			TExplosionA(ObjMove_GetX(objBoss)+rand(-50,50),ObjMove_GetY(objBoss)+rand(-50,50),10,0.3);
			wait(2);
		}
		TExplosionA(ObjMove_GetX(objBoss)+rand(-50,50),ObjMove_GetY(objBoss)+rand(-50,50),10,1);
		PlaySoundNR(21,100,0);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		TDelete;
		Obj_Delete(objBoss);
		wait(120);
		CloseScript(GetOwnScriptID());
		return;
	}
	
task TDelete{
	loop(120){
		DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
		yield;
	}
}

task TShoot01{
	wait(60);
	rCenter=CreateShotA1(GetStgFrameWidth/2,-200,0,0,273,0);
	ObjShot_SetSpellResist(rCenter,true);
	ObjShot_SetAutoDelete(rCenter,false);
	ObjMove_SetDestAtWeight(rCenter,GetStgFrameWidth/2,GetStgFrameHeight/2,15,5);
	loop(90){
		TShot01;
		PlaySound(objBoss,7,100,0);
		wait((0.5/60)*950);
	}
}

task TShoot02{
	wait(60);
	while(!Obj_IsDeleted(objBoss)){
		TCharge01(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3,255,255,255);
		PlaySound(objBoss,22,100,0);
		wait(75);
		loop(2){
			ascent(i in 0..15){
				let r=rand(0,360);
				let t=i*24;
				let obj=CreateShotA2(ObjMove_GetX(objBoss)+75*cos(t),ObjMove_GetY(objBoss)+75*sin(t),3,rand(0,360),-0.075,0,40,15);
				let obj2=CreateShotA2(ObjMove_GetX(objBoss)+75*cos(-t),ObjMove_GetY(objBoss)+75*sin(-t),3,rand(0,360),-0.075,0,40,15);
				ObjMove_AddPatternA2(obj,45,0,t,0.1,0,3);
				ObjMove_AddPatternA2(obj2,45,0,-t,0.1,0,3);
				TChange(obj);
				TChange(obj2);
				PlaySound(objBoss,12,100,0);
				wait(2);
			}
		}
		wait(75);
	}
}

task TChange(obj){
	let signal=1;
	while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetY(obj)<=0&&signal==1){
			ObjMove_SetAngle(obj,45+rand(35,55));
			ObjMove_SetSpeed(obj,0);
			ObjMove_SetAcceleration(obj,0.05);
			ObjMove_SetMaxSpeed(obj,3);
			TLaser(obj);
			signal++;
		}
		if(ObjMove_GetY(obj)>=GetStgFrameHeight+8){
			Obj_Delete(obj);
		}
		yield;
	}
	task TLaser(obj){
		let l=CreateStraightLaserA1(ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetAngle(obj),0,8,600,13,0);
		while(!Obj_IsDeleted(obj)){
			ObjLaser_SetLength(l,GetGapLength(ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetX(l),ObjMove_GetY(l)));
			yield;
		}
		let w=8;
		loop(15){
			ObjLaser_SetRenderWidth(l,w);
			ObjLaser_SetIntersectionWidth(l,w);
			w-=8/15;
			yield;
		}
		Obj_Delete(l);
	}
}
			
function GetGapLength(xA,yA,xB,yB){
	return((xB-xA)^2+(yB-yA)^2)^0.5;
}

task TShot01{
	let sP=-90;
	let r=rand(35,300);
	let x=ObjMove_GetX(rCenter)+r*cos(sP);
	let y=ObjMove_GetY(rCenter)+r*sin(sP);
	let obj=CreateShotA1(x,y,0,0,84,5);
	ObjShot_SetAutoDelete(obj,false);
	ObjShot_SetSpellResist(obj,true);
	TAngle(obj);
	task TAngle(obj){
		wait(5);
		ObjMove_SetAngularVelocity(obj,0.5);
		while(Obj_IsDeleted(rCenter)!=true&&Obj_IsDeleted(objBoss)!=true){
			ObjMove_SetX(obj,ObjMove_GetX(rCenter)+r*cos(sP));
			ObjMove_SetY(obj,ObjMove_GetY(rCenter)+r*sin(sP));
			sP+=0.5;
			yield;
		}
	}
}
	

task TMotionBlur{
	let renderTexture = [GetReservedRenderTargetName(0), GetReservedRenderTargetName(1)];

	SetInvalidRenderPriorityA1(20, 79);

	let objCurentFrame = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objCurentFrame, 80);
	ObjRender_SetPermitCamera(objCurentFrame, false);

	let objOldFrame = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objOldFrame, 0);
	ObjRender_SetPermitCamera(objOldFrame, false);
	ObjRender_SetAlpha(objOldFrame, 224);
	Obj_SetVisible(objOldFrame, false);

	let frame2 = 0;
	while(frame2<=50)
	{
		let currentFrameTexture = renderTexture[frame % 2];

		RenderToTextureA1(currentFrameTexture, 20, 79, true);

		if(frame2 >= 1) 
		{
			let oldFrameTexture = renderTexture[(frame - 1) % 2];
			ObjPrim_SetTexture(objOldFrame, oldFrameTexture);
			ObjSprite2D_SetSourceRect(objOldFrame, 32, 16, 416, 464);
			ObjSprite2D_SetDestRect(objOldFrame, 32, 16, 416, 464);
			RenderToTextureB1(currentFrameTexture, objOldFrame, false);
		}

		ObjPrim_SetTexture(objCurentFrame, currentFrameTexture);
		ObjSprite2D_SetSourceRect(objCurentFrame, 32, 16, 416, 464);
		ObjSprite2D_SetDestRect(objCurentFrame, 32, 16, 416, 464);

		frame2++;
		yield;
	}

	Obj_Delete(objCurentFrame);
	Obj_Delete(objOldFrame);
	ClearInvalidRenderPriority();
}