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

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

@Initialize{
	mainTask;
}

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

@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,360);
	wait(30);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin(objBoss,"Red/White \"Shining Blessed Shimenawa\"");
	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{
//Code "borrowed" from Yukari's CtC first non spell
	let graphic=[AMULET_RED,AMULET_GRAY];
	let side=1;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=135*side;
		let n=4;
		let t=45;
		let speed=0.5;
		let aAngle=360/n;
		let numAngle=12.2*side;
		loop(t){
			let angle2=angle;
			ascent(i in 0..n){
				let obj=CreateShotA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),speed,angle2,graphic[i%2],25);
				angle2+=aAngle;
			}
			speed+=0.035;
			angle+=numAngle;
		}
		PlaySoundDelay(GCSD~"se/tan01.wav",25);
		side=-side;
		TMovement;
		wait(120);
		angle=-37.5;
		while(angle<=37.5){
			TOrb(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),6,angle+GetAngleToPlayer(objBoss),-0.2,1);
			bullet_g=-bullet_g;
			angle+=37.5;
		}
		PlaySound(GCSD~"se/kira00.wav");
		rotate_angle=-rotate_angle;
		wait(90);
		TMovement;
	}
}

task TOrb(x,y,s,a,ss,Ms){
	let obj=DrawFamiliar(x,y,"MAGENTA",true,true);
	ObjMove_SetSpeed(obj,s);
	ObjMove_SetAngle(obj,a);
	ObjMove_SetAcceleration(obj,ss);
	ObjMove_SetMaxSpeed(obj,Ms);
	ObjEnemy_SetLife(obj,350);
	let SIDE=rotate_angle;
	let g;
	if(bullet_g==1){
		g=200;
	}else{
		g=213;
	}
	Cyclone;
	function Cyclone{
		ascent(i in 2..9){
			let angle=0;
			loop(3){
				Shot(1.125,i*12,angle);
				angle+=120;
			}
		}
	}
	task Shot(sa,r,ad){
		let shot=CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0,0,g,0);
		ObjShot_SetSpellResist(shot,true);
		let sAngle=0;
		let x;
		let y;
		while(!Obj_IsDeleted(shot)&&!Obj_IsDeleted(obj)){
			x=ObjMove_GetX(obj)+sin(sAngle)*r*cos((sAngle*SIDE)+ad);
			y=ObjMove_GetY(obj)+sin(sAngle)*r*sin((sAngle*SIDE)+ad);
			ObjMove_SetPosition(shot,x,y);
			sAngle+=sa;
			yield;
		}
		if(!Obj_IsDeleted(shot)){
			ObjShot_FadeDelete(shot);
		}
	}
}

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