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

@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;
	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,360);
	wait(30);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin(objBoss,"Holy Ritual \"Divine Particle Summoning\"");
	TEnd;
	wait(120);
	TShoot01;
}

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 bS=[1.5,3.5];
	let graphic=[SMALL_RED,BL_SMALL_RED,LARGE_RED];
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let n=200;
		let angle=0;
		let obj=[];
		ascent(i in 0..(n/10)){
			let angle2=0;
			while(angle2<360){
				let o=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),4,angle2+angle,-0.1,0,graphic[i%3],15);
				obj=obj~[o];
				angle2+=360/10;
			}
			PlaySoundDelay(GCSD~"se/tan00.wav",15);
			yield;
			angle+=360/n;
		}
		ascent(i in 0..length(obj)){
			while(ObjMove_GetSpeed(obj[i])>0){ yield; }
		}
		let sAngle=0;
		ascent(i in 0..length(obj)){
		ObjMove_SetAcceleration(obj[i],0);
			ObjMove_SetSpeed(obj[i],rand(0.1,4));
			sAngle++;
		}
		PlaySound(GCSD~"se/kira00.wav");
		wait(90);
		TMovement;
		n=60;
		ascent(i in 0..n){
			ascent(k in 0..length(bS)){
				CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),6,i*(360/n),-0.375,sin(-i*128)+bS[k],AMULET_RED,5);
			}
		}
		PlaySound(GCSD~"se/tan01.wav");
		wait(60);
	}
}


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