#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell 05"]
#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"

let objBoss;

let count = 0;

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

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(3000);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(51);
    }
    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;
    } 

    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); PlaySFX_V(charge, 65);
	Concentration(bx, by, 60, 300, 3, 2, 3, BLEND_ADD_RGB, 255, 117, 26, 1); 
	ChargeCircle(bx, by, 75, 255, 117, 26, BLEND_ADD_RGB);
	wait(65);
	renderAura(objBoss, "Haiji", 1.12, 60*1/2, 200, [255, 117, 26]);
	
	ObjEnemy_SetDamageRate(objBoss, 55, 15);
	waves();
}

task waves{
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let a; let t = 90;
		let r; let d = 45;
		
		let n = 8; let h = 0;
		let g = 360/2 + 0.6;
	//}
	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		
		let T = []; let G = [];
		ascent(l in 0..360/absolute(n)+absolute(n)*2){
			PlaySFX_V(shot, 52);
			
			ascent(j in 0..3){
				r = d + 360/3*j;
				ascent(i in 0..2){
					a = t + 360/2*i;
					
					T = T ~ [CreateShotA2(bx - 100*cos(r), by - 100*sin(r), 4, a, -1/4, 0, 142+h, 3)];
					G = G ~ [CreateShotA2(bx - 100*cos(r), by - 100*sin(r), 4, a, -1/4, 0, 142+h, 3)];
				}
			}
			
			h++; if(h > 2){h = 0;}
			ascent(i in 0..length(T)){
				if(!Obj_IsDeleted(T[i])){ObjShot_SetAutoDelete(T[i], false); ObjShot_SetSpellResist(T[i], true); ObjRender_SetBlendType(T[i], BLEND_ALPHA);}
				if(!Obj_IsDeleted(G[i])){ObjShot_SetAutoDelete(G[i], false); ObjShot_SetSpellResist(G[i], true); ObjRender_SetBlendType(G[i], BLEND_MULTIPLY);}
			}
			
			d += g; t += n*5;
			wait(60*1/55);
		}
		DelayedPlaySFX_V(magik, 60 + 2, 60); DelayedPlaySFX_V(rumble, 60 + 90, 100);
		wait(60*1); let cr = 1.23*2.34; goy();
		ascent(i in 0..length(T)){
			if(!Obj_IsDeleted(T[i])){
				let g = ObjShot_GetImageID(T[i]);
				ObjMove_AddPatternA3(T[i], 2, 0, NO_CHANGE, -1/3, cr, -2, g);
				ObjMove_AddPatternA3(T[i], 90, NO_CHANGE, NO_CHANGE, 1/30, 0, -1.6, g-80);
				trans(T[i]);
			}
			if(!Obj_IsDeleted(G[i])){
				let h = ObjShot_GetImageID(G[i]);
				ObjMove_AddPatternA3(G[i], 2, 0, NO_CHANGE, -1/3, cr, -2, h);
				ObjMove_AddPatternA3(G[i], 90, NO_CHANGE, NO_CHANGE, 1/30, 0, -1, h-74);
				trans(G[i]);
			}
			
			cr *= -1;
		}
		
		d = rand(0,360); n *= -1; g *= -1;
		wait(60*5.4);
	}	
	
	task trans(ei){
		
		wait(2);
		ObjRender_SetBlendType(ei, BLEND_ALPHA);
		wait(90);
		
		ObjRender_SetBlendType(ei, BLEND_ADD_ARGB);
		ObjShot_SetDelay(ei, 10);
		ObjShot_SetAutoDelete(ei, true);
	}
	
	task goy(){
		wait(90); let r = angtoP(objBoss);
		
		ascent(j in 0..20){
			PlaySFX_V(shet, 72);
			ExplodeCircle(bx, by, 30 + j*8, 300, 153, 255, BLEND_INV_DESTRGB);
			ascent(i in 0..31){
				a = r + 360/31*i;
					
				let R = CreateShotA2(bx, by, 0, a, 1/2, 2 + j/5, 804, 2);
				ObjRender_SetBlendType(R, BLEND_INV_DESTRGB);
				
			}
			yield;
		}
	}

}

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