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

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

@Initialize{
	mainTask;
}

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

@MainLoop{
	frame++;
	sAngle++;
	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,150);
	wait(30);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin(objBoss,"Fantasy Sign \"Vivid Demon Seal\"");
	TEnd;
	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{
	sAngle=0;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			let obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),6,angle,-0.2,0,AMULET_RED,25);
			TSpeed(obj);
			angle+=360/45;
			PlaySound(GCSD~"se/tan00.wav");
		}
		wait(90);
	}
	task TSpeed(obj){
		while(ObjMove_GetSpeed(obj)>0){ yield; }
		ObjShot_SetGraphic(obj,AMULET_PURPLE);
		ObjMove_SetAngle(obj,GetGapAngle(ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetX(objPlayer),ObjMove_GetY(objPlayer)));
		ObjMove_SetSpeed(obj,3);
		ObjMove_SetAcceleration(obj,0);
	}
}

task TShoot02{
	sAngle=0;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			let obj=DrawFamiliar(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),"BLUE",true,true);
			ObjMove_SetSpeed(obj,2.5);
			ObjMove_SetAngle(obj,angle+(sAngle*0.63));
			TCheck(obj);
			obj=DrawFamiliar(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),"BLUE",true,true);
			ObjMove_SetSpeed(obj,2.5);
			ObjMove_SetAngle(obj,angle+(-sAngle*0.63));
			TCheck(obj);
			angle+=360/2;
		}
		PlaySound(GCSD~"se/tan01.wav");
		wait(60);
	}
	task TCheck(obj){
		let bS=[2,2.4,2.8];
		let signal=1;
		while(!Obj_IsDeleted(obj)&&signal==1){
			if(ObjMove_GetX(obj)<=0&&!Obj_IsDeleted(obj)&&signal==1){
				let angle=-20;
				while(angle<=20){
					ascent(i in 0..length(bS)){
						CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),bS[i],angle,ARROWHEAD_RED,5);
					}
					angle+=20;
				}
				signal++;
			}
			if(ObjMove_GetX(obj)>=GetStgFrameWidth&&!Obj_IsDeleted(obj)&&signal==1){
				let angle=-20;
				while(angle<=20){
					ascent(i in 0..length(bS)){
						CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),bS[i],180+angle,ARROWHEAD_RED,5);
					}
					angle+=20;
				}
				signal++;
			}
			if(ObjMove_GetY(obj)<=0&&!Obj_IsDeleted(obj)&&signal==1){
				let angle=-20;
				while(angle<=20){
					ascent(i in 0..length(bS)){
						CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),bS[i],90+angle,ARROWHEAD_RED,5);
					}
					angle+=20;
				}
				signal++;
			}
			if(ObjMove_GetY(obj)>=GetStgFrameHeight&&!Obj_IsDeleted(obj)&&signal==1){
				let angle=-20;
				while(angle<=20){
					ascent(i in 0..length(bS)){
						CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),bS[i],270+angle,ARROWHEAD_RED,5);
					}
					angle+=20;
				}
				signal++;
			}
			yield;
		}
	}
}

sub TMovement{
	if(ObjMove_GetSpeed(objBoss)==0){
		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);
	}
}