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

let frame=0;
let objScene=GetEnemyBossSceneObjectID;
let GCSD=GetCurrentScriptDirectory~"";

@Initialize{
	mainTask;
}

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

@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{
	objBoss=TDrawBoss;
	TEnemyHitboxStart(objBoss,150);
	TEnd;
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/3,7.5,5);
	wait(150);
	TShoot01;
	TShoot02;
}

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 TShoot01{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		loop(2){
			let angle=0;
			let r=rand(0,360);
			while(angle<360){
				let obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3,r+angle,-0.0475,0,AMULET_RED,10);
				TBullet(obj);
				angle+=360/35;
			}
		PlaySound(GCSD~"se/tan01.wav");
		wait(10);
	}
	wait(90);
	}
}

task TShoot02{
	TMovement;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let t=1;
		let side=1;
		let B=[];
		ANIM_IDLE=false;
		loop(6){
			let s=3.75;
			ascent(i in 1..5){
				let angle=0;
				while(angle<360){
					if(side==1){
						let obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),s*2,angle+GetAngleToPlayer(objBoss)+i*t-t,-0.2,s/1.5,AMULET_GRAY,10);
					}else{
						let obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),s*2,angle+GetAngleToPlayer(objBoss)-i*t+t,-0.2,s/1.5,AMULET_GRAY,10);
					}
					angle+=360/6;
				}
				s-=3.75/6;
			}
			t+=4;
			side=-side;
			PlaySound(GCSD~"se/tan02.wav");
			wait(6);
		}
		ANIM_IDLE=true;
		side=1;
		wait(90);
		TMovement;
	}
}

task TBullet(obj){
	wait(10);
	while(ObjMove_GetSpeed(obj)>0){ yield; }
	ObjMove_SetAngle(obj,GetAngleToPlayer(obj));
	ObjMove_SetSpeed(obj,3);
	while(ObjMove_GetSpeed(obj)>0){ yield; }
	ObjMove_SetAngle(obj,GetAngleToPlayer(obj));
	ObjMove_SetSpeed(obj,3);
	ObjMove_SetAcceleration(obj,0);
}

sub TMovement{
	let resultx=rand(0,1);
	resultx=round(resultx);
	let resulty=rand(0,1);
	resulty=round(resulty);
	let posx=rand(100,-100);
	if(posx<50&&posx>-50){
		if(resultx==1){
			posx=50;
		}else{
			posx=-50;
		}
	}
	if((ObjMove_GetX(objBoss)+posx)<=50||(ObjMove_GetX(objBoss)+posx)>=(GetStgFrameWidth()-50)){
		posx=-posx;
	}
	let posy=rand(25,-25);
	if(posy<25&&posy>-25){
		if(resulty==1){
			posy=10;
		}else{
			posy=-10;
		}
	}
	if((ObjMove_GetY(objBoss)+posy)<=32||(ObjMove_GetY(objBoss)+posy)>=GetStgFrameHeight()/3){
		posy=-posy;
	}
	ObjMove_SetDestAtWeight(objBoss,ObjMove_GetX(objBoss)+posx,ObjMove_GetY(objBoss)+posy,15,10);
}