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

let frame=0;
let sAngle=0;
let oAngle=0;
let orbs=[];
let T=0;
let addtoT=0;
let signal=0;
let orX;
let orY;
let ATTACK_TYPE=1;
let objScene=GetEnemyBossSceneObjectID;
let GCSD=GetCurrentScriptDirectory~"";

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(2500)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(50)
	}
}

@MainLoop{
	frame++;
	sAngle++;
	oAngle++;
	T+=addtoT;
	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;
	ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/3,7.5,5);
	while(IsPlayerSpellActive==true||ObjMove_GetSpeed(objBoss)>0){ yield; }
	TEnemyHitboxStart(objBoss,300);
	wait(30);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin(objBoss,"Spiritual Arts \"Maiden's Visionary Seals\"");
	TEnd;
	wait(120);
	TOrbs;
	wait(15);
	TShoot01;
	TShoot02;
	TAction;
}

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{
	TCharge(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
	wait(75);
	TExplode(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
	PlaySound(GCSD~"se/don20.wav");
	signal=1;
	let angle=180;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),2.5,angle,-0.05,0,BUBBLE_RED,15);
		TMove(obj);
		Trail(obj,BUBBLE_RED);
		wait(75);
		if(angle==180){
			angle=0;
		}else{
			angle=180;
		}
		while(ATTACK_TYPE!=1){ yield; }
		obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),2.5,angle,-0.05,0,BUBBLE_GREEN,15);
		TMove(obj);
		Trail(obj,BUBBLE_GREEN);
		TMovement;
		wait(75);
		if(angle==180){
			angle=0;
		}else{
			angle=180;
		}
		while(ATTACK_TYPE!=1){ yield; }
		obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),2.5,angle,-0.05,0,BUBBLE_BLUE,15);
		TMove(obj);
		Trail(obj,BUBBLE_BLUE);
		TMovement;
		wait(75);
		if(angle==180){
			angle=0;
		}else{
			angle=180;
		}
		ATTACK_TYPE=2;
		while(ATTACK_TYPE!=1){ yield; }
		TMovement;
	}
	task Trail(obj,c){
		PlaySound(GCSD~"se/se_boon00.wav");
		while(!Obj_IsDeleted(obj)&&ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
			let obj2=CreateShotOA1(obj,0,0,c,25);
			TD(obj2);
			yield;
		}
	}
	task TD(obj){
		wait(24);
		Obj_Delete(obj);
	}
	task TMove(obj){
		while(ObjMove_GetSpeed(obj)>0){ yield; }
		ObjMove_SetAngle(obj,GetAngleToPlayer(obj));
		ObjMove_SetAcceleration(obj,0.1);
		ObjMove_SetMaxSpeed(obj,6);
		while(!Obj_IsDeleted(obj)&&ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0&&ObjMove_GetY(obj)<(ObjMove_GetY(objPlayer)-150)||ObjMove_GetY(obj)>(ObjMove_GetY(objPlayer)+150)){
			TurnToAngle(obj,5,GetAngleToPlayer(obj));
			yield;
		}
	}
}

task TShoot02{
	wait(90);
	sAngle=0;
	let bS=[2,2.25];
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		if(ATTACK_TYPE==1){
			let angle=0;
			while(angle<360){
				ascent(i in 0..length(orbs)){
					ascent(k in 0..length(bS)){
						CreateShotA1(ObjMove_GetX(orbs[i]),ObjMove_GetY(orbs[i]),bS[k],angle+getAtanAngle(objBoss,orbs[i]),AMULET_RED,10);
					}
				}
				angle+=360/2;
			}
			PlaySound(GCSD~"se/tan00.wav");
		}
		wait(8);
	}
}

task TOrbs{
	oAngle=0;
	let angle=0;
	let R=0;
	let rr=0;
	let rrr=0;
	while(angle<360){
		let obj=DrawFamiliar(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),"RED",false,false);
		orbs=orbs~[obj];
		angle+=360/3;
		TPos(obj,angle);
	}
	let n=60;
	descent(i in 0..n){
		R+=(i/n)*3.25;
		oAngle=0;
		T=0;
		rr=R;
		yield;
	}
	ascent(i in 0..(n/4)){
		R+=-(i/n)*3.25;
		oAngle=0;
		T=0;
		rr=R;
		yield;
	}
	descent(i in 0..(n/4)){
		R+=-(i/n)*3.25;
		oAngle=0;
		T=0;
		rr=R;
		yield;
	}
	T=0;
	oAngle=0;
	rrr=R;
	rr=rrr;
	task TPos(obj,a){
		while(!Obj_IsDeleted(obj)&&ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
			if(signal==1&&addtoT<=1){
				addtoT+=0.005;
			}
			if(ATTACK_TYPE==1||ATTACK_TYPE==3){
				R=rr;
				orX=ObjMove_GetX(objBoss)+rr*cos(oAngle*2+a);
				orY=ObjMove_GetY(objBoss)+rr*sin(oAngle*2+a);
			}
			if(ATTACK_TYPE==2){
				R=rr*1.5;
				orX=ObjMove_GetX(objPlayer)+(R+5)*cos(oAngle*1.5+a);
				orY=ObjMove_GetY(objPlayer)+(R+5)*sin(oAngle*1.5+a);
			}
			ObjMove_SetDestAtWeight(obj,orX,orY,5,7.5);
			yield;
		}
	}
}

task TAction{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		if(ATTACK_TYPE==2){
			loop(60){
				ascent(i in 0..length(orbs)){
					ObjEnemy_SetIntersectionCircleToPlayer(orbs[i],ObjMove_GetX(orbs[i]),ObjMove_GetY(orbs[i]),0);
				}
				yield;
			}
			TCharge(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
			loop(60){
				ascent(i in 0..length(orbs)){
					ObjEnemy_SetIntersectionCircleToPlayer(orbs[i],ObjMove_GetX(orbs[i]),ObjMove_GetY(orbs[i]),0);
				}
				yield;
			}
			ascent(i in 0..length(orbs)){
				ObjEnemy_SetIntersectionCircleToPlayer(orbs[i],ObjMove_GetX(orbs[i]),ObjMove_GetY(orbs[i]),14);
			}
			ANIM_IDLE=false;
			loop(30){
				ascent(i in 0..length(orbs)){
					let angle=0;
					while(angle<360){
						CreateShotA1(ObjMove_GetX(orbs[i]),ObjMove_GetY(orbs[i]),rand(0.75,1.5),rand(-2.5,2.5)+angle+GetAngleToPlayer(orbs[i]),DOT_RED,5);
						angle+=360/8;
					}
				}
				PlaySound(GCSD~"se/tan00.wav");
				wait(2);
				TCheckHp(objBoss);
			}
			ANIM_IDLE=true;
			TMovement;
			ATTACK_TYPE=3;
			loop(60){
				ascent(i in 0..length(orbs)){
					ObjEnemy_SetIntersectionCircleToPlayer(orbs[i],ObjMove_GetX(orbs[i]),ObjMove_GetY(orbs[i]),0);
				}
				yield;
			}
			TCheckHp(objBoss);
			TCharge(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
			loop(75){
				ascent(i in 0..length(orbs)){
					ObjEnemy_SetIntersectionCircleToPlayer(orbs[i],ObjMove_GetX(orbs[i]),ObjMove_GetY(orbs[i]),0);
				}
				yield;
			}
			TCheckHp(objBoss);
			TExplode(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
			PlaySound(GCSD~"se/don20.wav");
			ascent(i in 0..length(orbs)){
				ObjEnemy_SetIntersectionCircleToPlayer(orbs[i],ObjMove_GetX(orbs[i]),ObjMove_GetY(orbs[i]),14);
			}
			ATTACK_TYPE=1;
		}
		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);
}