#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["NS2"]
#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(1000)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(35)
	}
}

@MainLoop{
	frame++;
	plusangle++;
	if(ObjMove_GetSpeed(objBoss)>0){
		wsignal=1;
	}else{
		wsignal=0;
	}
	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);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2+rand(-150,150),GetStgFrameHeight/3,10,5);
	TWater;
	TMovement;
	TShoot01;
	TShoot02;
	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);
	PlaySoundNR(0,100,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));
	}
	PlaySoundNR(0,100,0);
	while(wsignal!=1){ yield; }
	TWater;
}

task TMovement{
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2+rand(-150,150),GetStgFrameHeight/3+rand(-75,75),15,5);
		while(ObjMove_GetSpeed(objBoss)>0){ yield; }
		wait(90);
	}
}

task TShoot01{
	while(wsignal==1){ yield; }
	wait(90);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		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,25,0.1,0,4,objPlayer,NO_CHANGE);
		ObjMove_AddPatternA4(obj3,60,NO_CHANGE,-25,0.1,0,4,objPlayer,NO_CHANGE);
		wait(2);
		while(wsignal==1){ yield; }
	}
}

task TShoot02{
	while(wsignal==0){ yield; }
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		CreateShotB2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),rand(-3,3),-1,0,0.05,1,3,FIRE_BLUE,5);
		PlaySound(objBoss,12,100,0);
		wait(2);
		while(wsignal==0){ yield; }
	}
}