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

@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(), 140, 90);
	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(180);
	ObjEnemy_SetDamageRate(objBoss, 55, 15);

	red(); 
}

task red{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let x = [0, stgw]; 
		
		
		let sp = 1; let n = 20;
		let a; let r = 45;
		
		let v = 5;
	//}

	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		ascent(con in 0..4){
			let i = round(rand(0,1));
			
			let kn = CreateShotA1(x[i], rand(5, stgh-5), 11, 0, 317, 30);
			ChargeCircle(x[i], gety(kn), 30, 255, 0, 255, BLEND_ADD_RGB);
			seta(kn, angtoB(kn)); trail(kn, 60*1/45);
			
			wait(60*0.9);
		}
		wait(60*3); 
		
		let d = 0;
		ascent(l in 0..16){
			PlaySFX_V(shet, 60);
			let D = PolygonShot(bx, by, -2-l/2, r, 6, 6, 1, 252+d, 1);

			ascent(i in 0..length(D)){
				if(!Obj_IsDeleted(D[i])){
					let sv = Obj_GetValue(D[i], "sv");
					let g = ObjShot_GetImageID(D[i]);
						
					ObjRender_SetBlendType(D[i], BLEND_ADD_ARGB);
					ObjShot_SetSpellResist(D[i], true);
					ObjShot_SetGraphic(D[i], g-10);
					ObjMove_AddPatternA2(D[i], 60 * 1/4, NO_CHANGE, NO_CHANGE, +1/6*sv, v, 2*sv);
					ObjMove_AddPatternA2(D[i], 60 * 3/1, NO_CHANGE, NO_CHANGE, -1/10*sv, 0, -9*sv);
				}
			}
			
			d++; if(d > 8){d = 0;}
			
			r += sp; 
			wait(60*1/12);
		}
		DelayedPlaySFX_V(magik, 60*3, 75);

		
		ExplodeCircle(bx, by, 60, 80, 0, 80, BLEND_ADD_ARGB);
		r = rand(0,360); sp *= -1; n *= -1; v *= -1;
		wait(60*4); 
	}
	
	task trail(eu, sk){
		
		//{
			let B = []; let t = 0;
			
			let a; 
				let r = geta(eu); 
		//}

		ObjShot_SetSpellResist(eu, true); wait(30);
		while(!Obj_IsDeleted(eu)){
			wait(sk);
			if(Obj_IsDeleted(eu)){break;}
			
			PlaySFX_V(shot, 60);
			ascent(l in 0..22){
				ascent(i in 0..1){
					a = r + 360/1*i;
					
					B = B ~ [CreateShotA1(getx(eu), gety(eu), 0, a, 214, 2)];
					Obj_SetValue(B[t], "sp", 1 + l/5);
					
					t++;
				}
			}
			
			
			
			ascent(i in 0..length(B)){
				if(!Obj_IsDeleted(B[i])){
					ObjShot_SetSpellResist(B[i], true);
				}
			}

			r += 360/50; 
			
		}

		//wait(60*1);
		
		PlaySFX_V(shet, 75);
		ascent(i in 0..length(B)){
			if(!Obj_IsDeleted(B[i])){
				let g = ObjShot_GetImageID(B[i]);
				ObjShot_SetGraphic(B[i], g-10);
				
				let sp = Obj_GetValue(B[i], "sp");
				ObjMove_AddPatternA2(B[i], 1, -1/200, angtoP(B[i])+360/8*i, 1/2, 0, sp);
				ObjShot_SetDelay(B[i], 5);
				ObjRender_SetBlendType(B[i], BLEND_ADD_ARGB);
				
				ObjShot_SetSpellResist(B[i], false);
			}
			
			//if(i % 2 == 0 && i != 0){yield;}
		}
	}
}

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