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

let objBoss;
let frame=0;
let waterpos;
let charge;
let wsignal=0;
let angleToPlayer;
let GCSD=GetCurrentScriptDirectory~"";
let plusangle=0;

@Initialize{
	mainTask;
}

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

@MainLoop{
	frame++;
	if(wsignal!=1){
		ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),24);
		ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	}
	angleToPlayer=atan2(ObjMove_GetY(objPlayer)-ObjMove_GetY(objBoss),ObjMove_GetX(objPlayer)-ObjMove_GetX(objBoss));
	yield;
	
}

task mainTask{
	TLoadSounds;
	objBoss=TDrawBoss;
	TEnemyHitboxStart(objBoss,150);
	wait(30);
	TShoot01;
	TEnd;
	yield;
}

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		TExplosionA(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),10,0.5);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		Obj_Delete(objBoss);
		loop(120){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}

task TWater{
	waterpos=TWaterHide(objBoss,"LARGE",true);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
	while(!Obj_IsDeleted(waterpos)&&wsignal==1){
		ObjRender_SetPosition(waterpos,ObjRender_GetX(objBoss),ObjRender_GetY(objBoss),0);
		yield;
	}
	Obj_Delete(waterpos);
	ObjRender_SetAlpha(objBoss,255);
	loop(5){
		TCallBubbles(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
	}
	while(wsignal!=1){ yield; }
	TWater;
}

task TShoot01{
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/3,10,5);
	let angular=[-0.5,-1.75,-3,-5,0.5,1.75,3,5];
	let bullets=[0.5,1,1.5,2,2.5,3];
	wait(90);
	let radius=0;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		loop(50){
			let angle=0;
			while(angle<360){
				let obj=CreateShotA2(ObjMove_GetX(objBoss)+radius*cos(angle+plusangle*10),ObjMove_GetY(objBoss)+radius/3*sin(angle+plusangle*10),5,GetAngleToPlayer(objBoss)+rand(-20,20),-0.125,0,PELLET_BLUE,15);
				ObjMove_AddPatternA4(obj,45,NO_CHANGE,0,0.1,0,4,objPlayer,NO_CHANGE);
				angle+=360/3;
			}
			PlaySound(objBoss,12,100,0);
			wait(1);
			plusangle++;
			radius+=3;
		}
		ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2+rand(-150,150),GetStgFrameHeight/3+rand(-50,50),10,5);
		ascent(i in 0..length(angular)){
			ascent(k in 0..length(bullets)){
				let angle=GetAngleToPlayer(objBoss);
				let obj=CreateShotA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3,GetAngleToPlayer(objBoss)+180,FIRE_BLUE,30);
				ObjMove_SetAngularVelocity(obj,angular[i]);
				ObjMove_AddPatternA3(obj,60,bullets[k],angle,0,0,bullets[k],NO_CHANGE);
				PlaySound(objBoss,13,100,90);
			}
		}
		wait(150);
		radius=0;
		plusangle=0;
		TCheckHp(objBoss);
		loop(50){
			let angle=0;
			while(angle<360){
				let obj=CreateShotA2(ObjMove_GetX(objBoss)+radius*cos(angle-plusangle*10),ObjMove_GetY(objBoss)+radius/3*sin(angle-plusangle*10),5,GetAngleToPlayer(objBoss)+rand(-20,20),-0.125,0,PELLET_BLUE,15);
				ObjMove_AddPatternA4(obj,60,NO_CHANGE,0,0.1,0,4,objPlayer,NO_CHANGE);
				angle+=360/3;
			}
			PlaySound(objBoss,12,100,0);
			wait(1);
			plusangle++;
			radius+=3;
		}
		ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2+rand(-150,150),GetStgFrameHeight/3+rand(-50,50),10,5);
		wait(150);
		radius=0;
		plusangle=0;
	}
}