#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["ReimuNS2"]
#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(2250)
	}
	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;
	TShoot03;
}

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{
	let side=1;
	let change=1;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		TCharge(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
		wait(60);
		let t=5;
		loop(10){
			let s=5.5;
			ascent(i in 0..12){
				if(side==1){
					CreateShotA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),s,GetAngleToPlayer(objBoss)+i*t,AMULET_PURPLE,10);
				}else{
					CreateShotA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),s,GetAngleToPlayer(objBoss)-i*t,AMULET_PURPLE,10);
				}
				if(change==1){
					s-=5.5/15;
				}else{
					s+=5.5/15;
				}
			}
			t+=0.9;
			side=-side;
			PlaySound(GCSD~"se/tan01.wav");
			wait(4);
		}
		TMovement;
		wait(105);
		change=-change;
	}
}

task TShoot02{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		if(ObjMove_GetSpeed(objBoss)>0){
			let angle=-45;
			while(angle<=45){
				TFam(angle-90+rand(-25,25));
				angle+=90;
			}
			PlaySound(GCSD~"se/tan00.wav");
			wait(15);
		}
		yield;
	}
}

task TShoot03{
	let s=[2,2.5,3,3.5,4,4.5];
	let gap=2.5;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		if(ObjMove_GetSpeed(objBoss)>0){
			let angle=-gap;
			while(angle<=gap){
				ascent(i in 0..length(s)){
					CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0,angle+GetAngleToPlayer(objBoss)+0.75,s[i]/30,s[i]*2,LARGE_RED,15);
					CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0,angle+GetAngleToPlayer(objBoss)-0.75,s[i]/30,s[i]*2,LARGE_RED,15);
				}
				angle+=5;
				gap+=1;
			}
		}
		if(ObjMove_GetSpeed(objBoss)==0){
			gap=2.5;
		}
		wait(15);
	}
}

task TFam(a){
	let obj=DrawFamiliar(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),"RED",true,true);
	ObjMove_SetSpeed(obj,3.5);
	ObjMove_SetAngle(obj,a);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		if(ObjMove_GetX(obj)<=0||ObjMove_GetX(obj)>=GetStgFrameWidth){
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));
		}
		if(ObjMove_GetY(obj)<=0){
			ObjMove_SetAngle(obj,360-ObjMove_GetAngle(obj));
		}
		yield;
	}
}

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);
}