#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell 03"]
#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 shot = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Shot01.wav");
let shet = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Shot02.wav");
let magik = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Special01.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(), 170, 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);
	ObjEnemy_SetDamageRate(objBoss, 55, 15);

	red(); 
	circs();

}

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

	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
		
		r = 90 - sin(o)*30; PlaySFX_V(shet, 42);
		ascent(k in 0..3){
			ascent(j in 0..20){
				a = r + 360/20*j;
				
				ascent(i in -2..3){
					let v = k/18;
					
					let H = CreateShotA1(bx + 120*cos(a), 10 + 20*sin(a), 4, r + 30*i, 67, 0);
					setup(H, a, v);
					ObjMove_AddPatternB3(H, 30, NO_CHANGE, -1 + v, 0, 1/45, 0, 6 + v, 47);
					Obj_SetRenderPriority(H, 0.6);
					
					if(NormalizeAngle(a) > 180 && NormalizeAngle(a) < 360){ObjRender_SetAlpha(H, 100);}
				}
			}
		}
		DelayedPlaySFX_V(rumble, 33, 82);
		
		o += 30;
		wait(60*1);
	}	
	
	task setup(eu, let a, let v){
		ObjRender_SetBlendType(eu, BLEND_ADD_ARGB); Obj_SetRenderPriority(eu, 0.6);
		wait(30);
		ObjMove_AddPatternB3(eu, 0, NO_CHANGE, -1 + v, 0, 1/45, 0, 6 + v, 47);
		ObjRender_SetBlendType(eu, BLEND_ALPHA); Obj_SetRenderPriority(eu, 0.6);
		if(NormalizeAngle(a) > 180 && NormalizeAngle(a) < 360){ObjRender_SetAlpha(eu, 100);}
	}
	
}

task red{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let a; let r = 0; let s = 180;
		let v = 3; let n = 360/24;
		
		let tt = 0.8; let f = 0;
	//}

	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
		
		descent(j in 0..60){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
			
			PlaySFX_V(shot, 62);
			ascent(i in 0..6){
				a = r + 360/6*i;
				
				let B = CreateShotA1(bx, by, 1+j/14, a, 202+f, 3);
				run(B, 5+j/6, v, 5, 242+f, +tt); 
			}
			
			f++; if(f > 2){f = 0;}
			r += n; tt *= -1;
			wait(60*1/40);
		}
		DelayedPlaySFX_V(magik, 60*2, 62);
		
		v *= -1; 
		s += 7; n *= -1;
		
		r = rand(0,360);
		wait(60*3);
	}	
	
	task run(C, s, spin, turns, graph, ang){
		ObjRender_SetBlendType(C, BLEND_ADD_ARGB);
		ObjShot_SetSpellResist(C, true); ObjShot_SetAutoDelete(C, false);
		wait(45); setsp(C, 0); 
		//{
			let d; let dist = GetObjectDistance(C, objBoss);
			let a = angtoO(objBoss, C);
			
			let o = 0;
		//}
		
		ascent(i in 0..turns/absolute(spin)){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
			
			d = dist + sin(o)*50;
			if(!Obj_IsDeleted(C)){
				setpos(C, bx + d*cos(a), by + d*sin(a));
				seta(C, a);
			}
			o += 4;
			a += spin;
			yield;
		}
		
		ObjMove_AddPatternA3(C, 1, -3, NO_CHANGE, 1/30, ang, s, graph);
		ObjShot_SetAutoDelete(C, true);
		
	}
	
}

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