#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Rumia Spell 2"]
#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 spinangle=0;

@Initialize{
	mainTask;
}

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

@MainLoop{
	frame++;
	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("Moonlight\"Moonlight Reflection\"",16);
	TShoot01;
	TShoot02;
	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 TShoot01{
	wait(150);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
	loop(5){
		let angle=0;
		while(angle<360){
				angle+=(360/10);
				TBullet01(GetEnemyX,GetEnemyY,1.5,angle+spinangle/rand(2,3),ICE_RED,15);
			}
			PlaySound(8,90,10);
			wait(5);
		}
		wait(150);
	}
}

task TShoot02{
	wait(180);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
	loop(4){
		TLaser01(GetEnemyX,GetEnemyY,angleToPlayer-20,550,15,45,ST_LASER_AQUA,90,0.1);
		TLaser01(GetEnemyX,GetEnemyY,angleToPlayer+20,550,15,45,ST_LASER_AQUA,90,-0.1);
		PlaySound(5,90,90);
		wait(15);
		}
		wait(180);
	}
}

task TBullet01(x,y,v,a,g,d){
	let signal=1;
	let obj=CreateShotA1(x,y,v,a,g,d);
	while(!Obj_IsDeleted(obj)){
		let bangle=ObjMove_GetAngle(obj);
		if(signal>0&&ObjMove_GetX(obj)<=0||ObjMove_GetX(obj)>(stgx*2)){
			ObjMove_SetAngle(obj,90*2-bangle);
			signal-=1;
			g++;
			ObjShot_SetGraphic(obj,g);
		}
		if(signal>0&&ObjMove_GetY(obj)<=0||ObjMove_GetY(obj)>(stgy*2)){
			ObjMove_SetAngle(obj,180*2-bangle);
			signal-=1;
			g++;
			ObjShot_SetGraphic(obj,g);
		}
		yield;
	}
}

task TLaser01(x,y,a,l,w,dt,g,d,na){
	let obj=CreateStraightLaserA1(x,y,a,l,w,dt,g,d);
	while(!Obj_IsDeleted(obj)){
		ObjStLaser_SetAngle(obj,ObjStLaser_GetAngle(obj)+na);
		yield;
		}
}