#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["ReimuS1"]
#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(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,175);
	wait(30);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin(objBoss,"Spirit Sign \"Fantasy Seal -White-\"");
	TEnd;
	wait(120);
	TCharge(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
	wait(60);
	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{
	let sB=[5,5.25,5.5,5.75,6];
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			let angle2=-20;
			while(angle2<=20){
				ascent(i in 0..length(sB)){
					let obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),sB[i],360/10-angle+angle2-2.5,-0.125,0,AMULET_GRAY,10);
					TAngle(obj,angle2);
				}
				angle2+=5;
			}
			angle+=75;
			TMovement;
			PlaySound(GCSD~"se/tan01.wav");
			wait(20);
		}
		wait(45);
	}
	task TAngle(obj,a){
		while(ObjMove_GetSpeed(obj)>0){
			ObjMove_SetAngularVelocity(obj,-a/7.5);
			yield;
		}
		ObjMove_SetAngularVelocity(obj,0);
		ObjShot_SetGraphic(obj,SMALL_RED);
		wait(30);
		ObjShot_SetGraphic(obj,AMULET_RED);
		ObjMove_SetSpeed(obj,4);
		ObjMove_SetAngle(obj,GetAngleToPlayer(obj));
		while(ObjMove_GetSpeed(obj)>0){ yield; }
		ObjShot_SetGraphic(obj,SMALL_RED);
		wait(30);
		ObjShot_SetGraphic(obj,SMALL_PURPLE);
		wait(30);
		ObjShot_SetGraphic(obj,AMULET_PURPLE);
		ObjMove_SetSpeed(obj,0);
		ObjMove_SetAngle(obj,GetAngleToPlayer(obj));
		ObjMove_SetAcceleration(obj,0.1);
		ObjMove_SetMaxSpeed(obj,2);
	}
}

task TShoot02{
	let bS=[4,4.5,5];
	let turn=1;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		loop(3){
			ascent(i in 0..length(bS)){
				let angle=0;
				while(angle<360){
					let obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),bS[i]+rand(-0.25,0.25),angle+rand(-10,10),-0.1,0,DOT_PURPLE,10);
					if(turn==1){
						TAngle(obj,0.5);
					}else{
						TAngle(obj,-0.5);
					}
					angle+=360/7;
				}
				wait(5);
			}
			turn=-turn;
			wait(5);
		}
		wait(60);
	}
	task TAngle(obj,A){
		while(ObjMove_GetSpeed(obj)>0){ yield; }
		wait(45);
		ObjMove_SetAcceleration(obj,0.025);
		ObjMove_SetMaxSpeed(obj,2);
		ObjShot_SetGraphic(obj,ARROWHEAD_RED);
		ObjMove_SetAngle(obj,ObjMove_GetAngle(obj)+(A*2*90));
	}
}

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