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

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

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(2250)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(55)
	}
}

@MainLoop{
	frame++;
	plusangle++;
	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;
	ObjMove_SetPosition(objBoss,GetStgFrameWidth/2,-50);
	TEnemyHitboxStart(objBoss,120);
	TWater;
	TEnter;
	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",false);
	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);
}

task TEnter{
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2+rand(-100,100),GetStgFrameHeight/3+rand(-50,100),10,15);
	while(ObjMove_GetSpeed(objBoss)!=0){ yield; }
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/3,10,5);
	while(ObjMove_GetSpeed(objBoss)!=0){
		yield;
		}
	wsignal=0;
	loop(5){
	TCallBubbles(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
	}
	PlaySoundNR(0,100,0);
}

task TShoot01{
	let a=1;
	while(wsignal==1){ yield; }
	wait(30);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		loop(2){
		loop(20){
			let obj1=CreateShotA2(rand(0,GetStgFrameWidth),0,5,rand(80,100),-0.125,0,PELLET_BLUE,15);
			let obj2=CreateShotA2(rand(0,GetStgFrameWidth),0,5,rand(80,100),-0.125,0,PELLET_BLUE,15);
			let obj3=CreateShotA2(rand(0,GetStgFrameWidth),0,5,rand(80,100),-0.125,0,PELLET_BLUE,15);
			ObjMove_AddPatternA4(obj1,60,NO_CHANGE,0,0.1,0,4,objPlayer,NO_CHANGE);
			ObjMove_AddPatternA4(obj2,60,NO_CHANGE,35,0.1,0,4,objPlayer,NO_CHANGE);
			ObjMove_AddPatternA4(obj3,60,NO_CHANGE,-35,0.1,0,4,objPlayer,NO_CHANGE);
			yield;
			}
		wait(15);
		TCheckHp(objBoss);
		}
		wait(90);
		let speed=0.5;
		let angleplayer=angleToPlayer;
		loop(15){
			let randa=rand(-5,5);
			let angle=0;
			while(angle<360){
				CreateShotA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),speed,angle+angleplayer,LARGE_BLUE,15);
				angle+=360/6;
				}
			speed+=0.175;
			TCheckHp(objBoss);
			PlaySound(objBoss,12,100,0);
			yield;
			}
		TCheckHp(objBoss);
		wait(45);
		let spin=0;
		let size=0;
		let delay=90;
		loop(5){
			let angle=0;
			while(angle<360){
				CreateShotB2(ObjMove_GetX(objBoss)+size*cos(angle+spin),ObjMove_GetY(objBoss)+size*sin(angle+spin),rand(-2,2),-1,0,0.05,1,3,FIRE_BLUE,delay);
				CreateShotB2(ObjMove_GetX(objBoss)+size*cos(angle+spin),ObjMove_GetY(objBoss)+size*sin(angle+spin),-rand(-2,2),-1,0,0.05,-1,3,FIRE_BLUE,delay);
				angle+=360/3;
				}
				delay-=(90/5);
			size+=15;
			spin+=a*5;
			PlaySound(objBoss,12,100,0);
			wait(1);
			}
		a=-a;
		ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2+rand(-150,150),GetStgFrameHeight/3,10,5);
		wait(120);
	}
}
