#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell 06"]
#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 fam = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Special04.wav");
let woo = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Charge01.wav");
let laser = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Laser01.wav");

let shot = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Shot01.wav");
let shet = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Shot02.wav");
let magik = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Special01.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(), 160, 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{
	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);
	
	enemies(); waves();
}

task enemies{
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let x; let y; let a;
		let r = 270;  let n = -0.5;
	//}
	
	let len = 250; PlaySFX_V(fam, 62);
	let L = CreateEnemy(bx - len, by, 35); let R = CreateEnemy(bx + len, by, 35); wait(40);
	orbit(L, 0.5); orbit(R, 0.5);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		let T = 11; PlaySFX_V(woo, 82);
		ascent(j in 0..T){
			ascent(i in -4..5){
				r = angtoP(L);
				
				a = r + 28*i;
				x = getx(L); y = gety(L);
				let N = CreateSTLaser(x, y, a, 650, 35, 4, [54, 100]); ObjRender_SetBlendType(N, BLEND_SUBTRACT);
				
				r = angtoP(R);
				a = r + 28*i;
				x = getx(R); y = gety(R);
				N = CreateSTLaser(x, y, a, 650, 35, 10, [54, 100]); ObjRender_SetBlendType(N, BLEND_SUBTRACT);
			}
			DelayedPlaySFX_V(laser, 54 + 5*j, 75);
			wait(5);
		}
		
		wait(100); PlaySFX_V(shet, 60);
		ascent(i in 0..41){
			a = r + 360/41*i;
			
			x = getx(L); y = gety(L);
			let A = CreateShotA1(x, y, 2, a, 264, 5); ObjRender_SetBlendType(A, BLEND_ALPHA);
			ObjMove_AddPatternA2(A, 60, NO_CHANGE, NO_CHANGE, 1/60, 0, 6); 
			if(i % 2 == 0 && i != 0){twice(A, 60);}
			
			x = getx(R); y = gety(R);
			let B = CreateShotA1(x, y, 2, a, 264, 5); ObjRender_SetBlendType(B, BLEND_ALPHA);
			ObjMove_AddPatternA2(B, 60, NO_CHANGE, NO_CHANGE, 1/60, 0, 6);
			if(i % 2 == 0 && i != 0){twice(B, 60);}
		}
		
		n *= -1; 
		wait(45);
	}	
	
	task twice(en, f){wait(f+1); ObjShot_SetGraphic(en, 269); setsp(en, -1);}
	
	task orbit(en, l){
		let a = angtoO(objBoss, en);
		while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			setpos(en, bx + len*cos(a), by + 70*sin(a));
			a += l;
			yield;
		}
	}
	
}

task waves{
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let a; let r = 0;
		let g = [329,323]; let v = 5;
		
		let n = 20; let h = 0;
	//}

	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		DelayedPlaySFX_V(magik, 120, 75);
		ascent(j in 0..16){
			let t = v/absolute(v); PlaySFX_V(shot, 60);
			ascent(i in 0..13){
				a = r + 360/13*i;
					//540
				let st = CreateShotA1(bx, by, 2, a, g[h]+540, 5); ObjRender_SetBlendType(st, BLEND_ADD_ARGB);
				ObjMove_AddPatternA3(st, 060, -3, NO_CHANGE, 1/15, v, 1 + j/2, g[h]-240);
				ObjMove_AddPatternA3(st, 120, +0, NO_CHANGE, 1/05, 0, 1 + j/2, g[h]);
			}
			
			h++; if(h > 1){h = 0;} 
			wait(60*1/10);
		}
		
		r += n; v *= -1;
		wait(60*2);	
	}	

}

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