#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Rumia Spell 1"]
#Text["."]
#include"./Functions/Index.txt"
#include"script/Rumia Boss/ZUNShot_Const.txt"
#include"script/Default_system/Default_Effect.Txt"

let objScene=GetEnemyBossSceneObjectID();

let objBoss;
let frame=0;
let GCSD=GetCurrentScriptDirectory~"";
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let bossMove=0;
let plusangle=-150;
let plusangle2=-150;
let spinangle=-120;
let t=-150;

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(3500)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(40)
	}
}

@MainLoop{
	frame++;
	t++;
	plusangle+=3;
	plusangle2+=2;
	spinangle++;
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),25);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),27);
	
	if(IsPlayerSpellActive){
		ObjEnemy_SetDamageRate(objBoss,0,0);
	}else{
		ObjEnemy_SetDamageRate(objBoss,100,100);
	}
	
	yield;
}

task mainTask{
	TLoadSounds;
	objBoss=TDrawBoss;
	wait(30);
	ObjEnemyBossScene_StartSpell(objScene);
	ObjMove_SetDestAtWeight(objBoss,stgx,GetStgFrameHeight()/4,10,5);
	TEnemyHitboxStart(objBoss,120);
	Cutin("Secrets In Darkness\"Irregular Mirror In Shadows\"",15);
	TMovement;
	TShoot01;
	TShoot02;
	TShoot03;
	TShoot04;
	TShoot05;
	TEnd;
	yield;
}

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		TExplosionA(GetEnemyX,GetEnemyY,10,0.6);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		Obj_Delete(objBoss);
		loop(90){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}

task TMovement{
	wait(120);
	ObjMove_SetDestAtWeight(objBoss,stgx,GetStgFrameHeight()/4+50,10,5);
	wait(30);
	loop{
		ObjMove_SetPosition(objBoss,stgx+cos(bossMove+2)*100*sin(bossMove),(GetStgFrameHeight()/4+50)+50*sin(bossMove)-2);
		bossMove++;
		yield;
	}
}

task TShoot01{
	let k=2;
	let d=3;
	wait(150);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=(360/5);
			TShot01(GetEnemyX+50*cos(k/d*t)*sin(t),GetEnemyY+50*cos(k/d*t)*cos(t),0,angle+t*3,0,2,DOT_RED,15);
			}
		wait(8);
	}
}

task TShoot02{
	let n=2;
	let d=4;
	let m=5;
	wait(150);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			angle+=(360/4);
			TShot01(stgx+100+50*cos(n/d*t*m)*sin(t*m),stgy-75+50*cos(n/d*t*m)*cos(t*m),-0.1,angle+t*m,0,1,DOT_BLUE,15);
			TShot01(stgx-100+50*cos(-n/-d*-t*m)*sin(-t*m),stgy-75+50*cos(-n/-d*-t*m)*cos(-t*m),-0.1,angle+-t*m,0,1,DOT_BLUE,15);
			}
			wait(8);
	}
}

task TShoot03{
	wait(150);
	let bullets=[-100,100];
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		ascent(b in 0..length(bullets)){
			let obj=CreateShotA1(GetEnemyX+cos(plusangle)*25+bullets[b],GetEnemyY,2,90,PELLET_GRAY,15);
			let obj2=CreateShotA1(GetEnemyY+sin(plusangle)*12.5+bullets[b],GetEnemyY,1,90,PELLET_GRAY,15);
			ObjMove_SetAngularVelocity(obj,rand(-0.05,0.05));
			ObjMove_SetAngularVelocity(obj2,rand(-0.0075,0.0075));
		}
		PlaySound(9,80,0);
		wait(5);
	}
}

task TShoot04{
	wait(120);
	let bullets=[0.1,0.3,0.5,0.7,0.9,1.1];
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		ascent(i in 0..length(bullets)){
			let obj=CreateShotA1(GetEnemyX,GetEnemyY,bullets[i],angleToPlayer+45,S_STAR_GREEN,15);
			let obj2=CreateShotA1(GetEnemyX,GetEnemyY,bullets[i],angleToPlayer-45,S_STAR_GREEN,15);
			TAccel(obj,-bullets[i]*3,bullets[i]/1000,150,2.5,0);
			TAccel(obj2,bullets[i]*3,-bullets[i]/1000,150,2.5,0);
			
		}
		wait(240);
	}
}

task TShoot05{
	wait(120);
	let bullets=[2.1,2.5,2.9];
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
		angle+=360/4;
			ascent(i in 0..length(bullets)){
				let obj=CreateShotA1(GetEnemyX,GetEnemyY,bullets[i],angle+angleToPlayer,BUBBLE_ORANGE,25);
				}
			}
		PlaySound(3,90,25);
		wait(150);
	}
}

task TShot01(x,y,speed,angle,accel,maxspeed,graphic,delay){
	let obj=CreateShotA2(x,y,speed,angle,accel,maxspeed,graphic,delay);
	wait(90);
	ObjMove_SetAcceleration(obj,0.1);
}

task TAccel(obj,s,a,g,sp,n){
	loop(n){ yield; }
	ObjMove_SetAngularVelocity(obj,s);
	loop(g){ yield; }
	ObjMove_SetAngularVelocity(obj,a);
	ObjMove_SetSpeed(obj,sp);
}