#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 1 H"]
#Text["Azure Card \"Eastern Dragon's Room\""]
#Background["./Background.txt"] 

let bossObj;
let bossX = 0;
let bossY = 0;

#include "./Expanded CTC (Darkness1)/Expanded_ShotConst.txt"
#include"./FX/Bloom VFX darkwalker247/BloomFX.txt"
#include"./lib.txt"
#include"./FX/Effects.cs"
#include"./Cutin/cutin.txt"

@Initialize {
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	ObjMove_SetDestAtSpeed(bossObj,GetStgFrameWidth/2,GetStgFrameHeight/4,5);
	SFXload;
	mainTask;
} 

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

@MainLoop {
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);

	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,32);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,20);
	
	yield;
} 

@Finalize {
} 

task mainTask {
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	ObjEnemy_SetDamageRate(bossObj,1,1);
	ObjCutin_Name("Azure Card \"Eastern Dragon's Room\"");
	BossRender;
	movement;
	end;
} 


task movement {
ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
let F1_BulNo = 7;
let F1_BulNo2 = 5;
let F1_BulRing = 4;
let F1_angadd = 0.125;
let F1_distadd = 1.5;
let F1_time = 240;
let F1_delay = 180;

let F2_bull_num = 4;
let F2_min_spd = 1;
let F2_max_spd = 2;
let F2_trailnum = 3;

let F3_time = 120;
let F3_bull_ring = 15;
let F3_bull_num = 1;
let F3_min_spd = 1.5;
let F3_max_spd = 4;

wait(120);
PlaySE(charge4);
BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
wait(60);

Fire1(bossObj, true, F1_BulNo, F1_BulNo2, F1_BulRing, F1_angadd, F1_distadd, F1_time, F1_delay);

wait(F1_time + F1_delay);

ObjEnemy_SetDamageRate(bossObj,45,25);

loop{
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	PlaySE(charge4);
	BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
	
	wait(60);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	
	Fire1(bossObj, false, F1_BulNo, F1_BulNo2, F1_BulRing, F1_angadd, F1_distadd, F1_time, F1_delay);
	Fire2(bossObj, F2_bull_num, F1_delay, F1_BulNo, F2_min_spd, F2_max_spd, F2_trailnum);
	
	wait(F1_time + F1_delay);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	
	Fire3(F3_time, false, F3_bull_ring, F3_bull_num, F3_min_spd, F3_max_spd);
	
	wait(F3_time);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	
	PlaySE(charge4);
	BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
	
	wait(60);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	
	Fire1(bossObj, true, F1_BulNo, F1_BulNo2, F1_BulRing, F1_angadd, F1_distadd, F1_time, F1_delay);
	Fire2(bossObj, F2_bull_num, F1_delay, F1_BulNo, F2_min_spd, F2_max_spd, F2_trailnum);

	wait(F1_time + F1_delay);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	
	Fire3(F3_time, true, F3_bull_ring, F3_bull_num, F3_min_spd, F3_max_spd);
	
	wait(F3_time);
	}

//Fire_1(true, F1_Bull, F1_BulNo, F1_BulRing);

wait(30);

}

task Fire1(obj, rev, bullnum, bullnum2, bullring, angadd, distadd, time, delay){
	let colorA = 134;
	let colorB = 158;
	let colorC = 246;
if(rev == true){angadd *= -1; colorA--; colorB--; colorC--;}	
let ang = rand(0,360);
let posX = ObjMove_GetX(obj);
let posY = ObjMove_GetY(obj);
PlaySE(shot7);
loop(8){//---------------------
		let ObjFireA = CreateShotA1(posX,posY,1,ang,colorA,0);
		 ObjFire1_Move(ObjFireA, ang, angadd, distadd);
		 ObjShot_SetAutoDelete(ObjFireA,false);
		 ObjShot_SetSpellResist(ObjFireA,true);
		 ObjFireALaser(ObjFireA, time, delay, angadd, colorB);
		
		let ObjFireB = CreateShotA1(posX,posY,1,ang,colorA,0);
		 ObjShot_SetAutoDelete(ObjFireB,false);
		 ObjShot_SetSpellResist(ObjFireB,true);
		 ObjFire1_Move(ObjFireB, ang, angadd*-1, distadd);
		 ObjFireBshot(ObjFireB, time, bullnum, bullnum2, bullring, colorC, delay);
		 
		ang+=360/(8*2); //---------------------------
	}
}

task ObjFireALaser(obj, time, delay, angadd, colorB){
	let ang = ObjMove_GetAngle(obj);
	if(Obj_IsDeleted(obj)){return;}
		//PlaySE(shot3);
		let lasnum = 4;
		let angplus = 1;
		loop(lasnum){
			if(Obj_IsDeleted(obj)){return;}
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			let Fire1_Laser = CreateStraightLaserA1(ObjMove_GetX(obj),ObjMove_GetY(obj),ang,1,15,time, colorB, delay);
			Fire1LaserMove(obj, Fire1_Laser, time, delay, angadd, lasnum, angplus);
			ang += 360/(lasnum);
			angplus ++;
		}
		wait(delay);
		PlaySE(laser2);
	}

task Fire1LaserMove(obj, Fire1_Laser, time, delay, angadd, lasnum, angplus){
	let objcount = 0;
	
	loop(time + delay + 20){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			let ang = ObjMove_GetAngle(obj);
			let X = ObjMove_GetX(obj);
			let Y = ObjMove_GetY(obj);
            ObjMove_SetPosition(Fire1_Laser, X,Y);
            ObjStLaser_SetAngle(Fire1_Laser, ang+((360/lasnum)*angplus));
            objcount+= 0;
			let length = GetObjectDistance(obj, bossObj);
			ObjLaser_SetLength(Fire1_Laser, length);
			yield;
        }
			ObjShot_SetAutoDelete(obj,true);
		
	}
	
	
	
task ObjFireBshot(obj, time, bullnum, bullnum2, bullring, colorC, delay){
	wait(delay);
	loop(bullnum){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		loop(bullnum2){
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		let ang = ObjMove_GetAngle(obj);
		if(Obj_IsDeleted(obj)){return;}
		PlaySE(shot3);
		loop(bullring){
			if(Obj_IsDeleted(obj)){return;}
			let BShot = CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0.1,ang,0.01,3, colorC, 30);
			ObjBshot_DeleteObj(BShot,time);
			ObjShot_SetAutoDelete(BShot, false);
			ang += 360/bullring;
		}
		wait(time/bullnum/2/bullnum2);
		}
		wait(time/bullnum/2);
	}
	ObjShot_SetAutoDelete(obj,true);
}

task ObjBshot_DeleteObj(BShot, time){
	wait(time+120);
	ObjShot_SetAutoDelete(BShot,true);
}

task ObjFire1_Move(obj, ang, angadd, distadd){ //credits: Sparen
	let objcount = 0;
	let trig = 0;
	let X = bossX;
	let Y = bossY;
	while(!Obj_IsDeleted(obj)){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
            ObjMove_SetPosition(obj, X + trig*cos(ang*30 + objcount), Y + trig * sin(ang*30 + objcount));
            ObjMove_SetAngle(obj, (ang*30 + objcount)-180);
            objcount+=angadd;
            trig+=distadd;
            yield;
        }
	
}

task Fire2(obj, bull_num, time, bullring, min_spd, max_spd, trailnum){
	loop(bull_num){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		let ang = GetAngleToPlayer(bossObj);
		if(Obj_IsDeleted(obj)){return;}
		PlaySE(shot9);
		loop(bullring){
			if(Obj_IsDeleted(obj)){return;}
		let spd = min_spd;
		loop(trailnum){
		CreateShotA2(bossX, bossY, 0, ang, spd*0.01, spd, 247, 30);
		spd += (max_spd/min_spd)/trailnum;
		}
		CreateShotA2(bossX, bossY, 0, ang, spd* 0.01, spd, 135, 30);
			ang += 360/bullring;
		}
		
		wait(time/bull_num);
	}
}

task Fire3(time, rev, bull_ring, bull_num, min_spd, max_spd){
	let ang = 270;
	let angadd = 1;
	if(rev == true){angadd *= -1}
	loop(bull_ring){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		PlaySE(shot1);
		loop(bull_num){
		let spd = rand(min_spd, max_spd);
		ascent(i in -2 .. 3){
			CreateShotA2(bossX, bossY, spd, ang + (20 * i) + rand(-10,10), -spd * 0.01, spd*0.2, round(rand(125,126)), 30);
			}
			ang+=(270/(bull_ring-1))*angadd;
		}
		wait(time / bull_ring);
	}
}



task end {
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
	PlaySE(SpellOff);
	SetAutoDeleteObject(true);
	loop(5){
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	yield;
	}
	Obj_Delete(bossObj);
	wait(30);	
	CloseScript(GetOwnScriptID());
}