#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell 02"]
#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/Render/Gizmo's Touhou Boss Sprite Library/GizmoSpriteLibrary.txt"
#include "./../lib/Render/LenenBossRender/RenderLenen.txt"

let objBoss; let t;
let objScene = GetEnemyBossSceneObjectID();

let count = 0;

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

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(3000);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(61);
    }
    case(EV_REQUEST_SPELL_SCORE){
        SetScriptResult(1000000);
    }
}
@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	
	//if(GetCommonData("set_dif", false) == false){Difficulty(3); SetCommonData("set_dif", true);}
	if(GetCommonData("music", false) == false){BGM_Play("Boss", 75); SetCommonData("music", true);}
	
	//ObjMove_SetPosition(objBoss, gx, -400);
    ObjMove_SetDestAtFrameWeight(objBoss, gx(), 150, 60);
	TDrawLoop;
    TFinalize;
}

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

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

task phases{
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(90);
	ObjEnemy_SetDamageRate(objBoss, 55, 15);

	red(); 
	yang();
	fire();
}

task fire{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let x = bx;

		let g = [842, 843, 844]; let i = 0;
	//}

	
	while((ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 1900) && t){yield;}
	ChargeCircle(bx, by, 30, 255, 153, 0, BLEND_ADD_RGB);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
		
		PlaySFX_V(fiery, 52);
		ascent(k in 0..4){
			x = k*stgw/3;
			
			ChargeCircle(x, 3, 15, 255, 153, 0, BLEND_ADD_RGB);
			ascent(j in 0..20){
				if(i > 1){i = 0;}
				let d = CreateShotA2(x, 3, 1/10, 90, 1/4, 4 + j/2, g[i], 5);
				seta(d, angtoP(d)); Obj_SetRenderPriority(d, 0.51);
				i++;
			}
			
			yield;
		}
		
		
		wait(60 * 3.21);
	}	
}

task yang{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let x = 0;
		
		let g = [882, 892]; let i = 0;
	//}

	
	while((ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 2500) && t){yield;}
	ChargeCircle(bx, by, 60, 255, 102, 102, BLEND_ADD_RGB);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
		
		PlaySFX_V(shet, 47);
		i = round(rand(0,1));
		ascent(j in 0-4..7+4){
			if(i > 1){i = 0;}
			CreateShotA2(x + j*stgw/7, -9, 1, 90, 1/50, 6, g[i], 5);
			i++; 
		}
		
		x = rand(-40, 40);
		wait(60 * 0.56);
	}	
}

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

	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
		
		PlaySFX_V(shot, 48);
		ascent(i in -4..5){
			a = r + 22*i;
			let T = CreateShotA1(bx + 30*cos(a), by + 30*sin(a), 3, a, 152, 2);
			ObjMove_AddPatternB3(T, 30, NO_CHANGE, 0, 0, 1/20, 0, 6, 151);
			
		
			let G = CreateShotA1(bx + 30*cos(a), by + 30*sin(a), 3, a, 152, 2);
			ObjMove_AddPatternB3(G, 30, NO_CHANGE, 0, 0, -1/20, 0, -6, 141);
			
			ObjShot_SetSpellResist(T, true); ObjShot_SetSpellResist(G, true);
		}
		
		r = 270 + sin(o)*15; o += 5;
		wait(60*1/14);
	}	
}

task TFinalize {
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_FADE);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}