#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["NS4"]
#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=-90;

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(2750)
	}
	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;
	TEnemyHitboxStart(objBoss,150);
	wait(30);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/3,15,5);
	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",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 TShoot01{
	wait(120);
	while(!Obj_IsDeleted(objBoss)){
			let obj=CreateShotA2(rand(0,GetStgFrameWidth),rand(0,(GetStgFrameHeight/3)),5,rand(100,80),-0.1,0.5,PELLET_BLUE,15);
			ObjMove_AddPatternA2(obj,90,NO_CHANGE,NO_CHANGE,0.1,0,2);
		wait(5);
	}
}

task TShoot02{
	wait(150);
	let a=360/2.5;
	let t=1;
	while(!Obj_IsDeleted(objBoss)){
		let angle=0;
		while(angle<360){
			let obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),7.5,angle+GetAngleToPlayer(objBoss),-0.25,0,SMALL_AQUA,15);
			let oa=ObjMove_GetAngle(obj);
			if(t==1){
				ObjMove_AddPatternA1(obj,30,1.25,ObjMove_GetAngle(obj)+a);
			}else{
				ObjMove_AddPatternA3(obj,30,1.25,rand(-45,45)+GetAngleToPlayer(objBoss),0,0,0,SMALL_BLUE);
			}
			angle+=360/20;
			t=-t;
		}
		a=-a;
		PlaySound(objBoss,12,100,0);
		PlaySound(objBoss,8,100,45);
		wait(45);
	}
}