#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 04 - Strange Art \"Radiant Wheel\""]
#Text[""]
#Background["./../BG.txt"]
#Player["./../player/lenen/YabuKura.txt", "./../player/lenen/TenkaBami.txt", "./../player/gt_medicine/PracticeP.txt"]
#System["./../System/Default_System.txt"]

#include "./../lib/Shot/Shotconstants.dnh"
//#include "./../lib/Shot/LenenShot/JINXShot_Const.txt"

//#include "./../lib/Render/EnemyMark.txt"
#include "./../lib/FxnSFX.dnh"
#include "./../lib/Funcs.dnh"
#include "./../lib/FXLib.dnh"
#include "./../lib/Render/MagicCircle.txt"
#include "./../lib/Patterns.dnh"

//#include "./../lib/Render/Gizmo's Touhou Boss Sprite Library/GizmoSpriteLibrary.txt"
#include "./../lib/Render/LenenBossRender/RenderLenen.txt"
#include "./../lib/RMTCLenenCutinLib/RMTCLenenCutin.txt"

let objBoss;
let objScene = GetEnemyBossSceneObjectID();

let count = 0; let timer;

let shot = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Shot01.wav");
let shet = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Shot03.wav");
let charge = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Charge01.wav");

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(3600);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(62);
    }
	case(EV_REQUEST_IS_DURABLE_SPELL){
		SetScriptResult(true);
	}
    case(EV_REQUEST_SPELL_SCORE){
        SetScriptResult(1000000);
    }
}

@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	
	if(GetCommonData("music", false) == false){BGM_Play("Boss", 75); SetCommonData("music", true);}
	
    ObjMove_SetDestAtFrameWeight(objBoss, gx(), 130, 50);
	StartSpell;
	TDrawLoop;
    TFinalize;
}

@MainLoop{
    ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
    ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24);
    if(count == 0){
        phases;
        count = 0;
    } 
	
	timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 27;
	
    count++;
    yield;
}

task StartSpell{
wait(60*1);
	ObjEnemyBossScene_StartSpell(objScene);
	
	let objEnemy = GetEnemyBossObjectID[0];
	wait(30);
	
	let rumia = GetCurrentScriptDirectory() ~ "../img/save.png";
	TRMTCLenenCutin(rumia, "Strange Art \"Radiant Wheel\"", false, 77, 0, 0);
}

task TDrawLoop{
	renderBoss(objBoss, "Haiji", 1.12, 60*1/2, false);
	magiccircle(objBoss, 1.6, false);
}

task phases{
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(60*4.5); PlaySFX_V(charge, 65);
	Concentration(bx, by, 60, 300, 3, 2, 3, BLEND_ADD_RGB, 77, 0, 0, 1); 
	ChargeCircle(bx, by, 75, 77, 0, 0, BLEND_ADD_RGB);
	wait(65);
	renderAura(objBoss, "Haiji", 1.12, 60*1/2, 200, [77, 0, 0]);
	ObjEnemy_SetDamageRate(objBoss, 75, 20);

	red(); egg();
}

task egg{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let a; 
			let r = 270; 

	//}
	
	while((ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 1800) && timer){yield;}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		
		PlaySFX_V(shot, 45);
		ascent(i in 0..20){
			a = r + 360/20*i;
			
			let T = CreateShotA1(bx, by, 6, a, 42, 3);
			ObjMove_AddPatternA2(T, 15, NO_CHANGE, NO_CHANGE, -1/3, 0, 0.7);
			ObjShot_SetSpellResist(T, true);
		}
		
		r += 31; 
		wait(60*1);
	}
	
}

task red{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let a; 
			let r = 270; let s = 270;
		
		let t = 15; let v = (360/5)+(0.31);
		
		let o = 0; let c1 = gx;
	//}
	
	ConcentrationExplosion(bx - 200, by, 72, 100, 2, 2, BLEND_ADD_RGB, 102, 00, 00, 8);
	ConcentrationExplosion(bx + 200, by, 72, 100, 2, 2, BLEND_ADD_RGB, 013, 13, 89, 8);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		
		PlaySFX_V(shot, 55);
		ascent(j in 0..20){
			ascent(i in 0..3){
				a = r + 360/3*i;
				let yt = CreateShotA1(c1 - cos(o)*200, by, 8, a, 581, 3);
				
				a = s + 360/3*i;
				let ty = CreateShotA1(c1 + cos(o)*200, by, 8, a, 581, 3);
				
				ObjMove_AddPatternA3(yt, t, 0, NO_CHANGE, 1/40, 0, 4, 842);
				ObjMove_AddPatternA3(ty, t, 0, NO_CHANGE, 1/40, 0, 4, 848);
				
				ObjShot_SetSpellResist(yt, true); ObjShot_SetSpellResist(ty, true);
				mb(yt, t+15); mb(ty, t+15);
			}
			r += v; s -= v; o += 0.5;
			yield;
		}
		
		v *= -1;
		r += absolute(v)*20; s -= absolute(v)*20;

		wait(60*1/8);
	}
	
	task mb(eu, n){
		wait(n);
		ObjShot_SetSpellResist(eu, false);
	}
	
}

task TFinalize {
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
		SetCommonData("extend", true);
		
		SetPlayerLife(GetPlayerLife() + 1);
		AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));
    }
	
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}