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

	bullets();
	laser();

}

task bullets{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let a; let t = 90; 
		let o = 0;
	//}
	
	
	am();
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		
		PlaySFX_V(shot, 60);
		ascent(i in 0..20){
			a = t + 360/20*i;
			
			let r = CreateShotA2(bx, by, 3, a, -1/60, 1, 45, 3); 
			CreateShotA1(getx(r), gety(r), 3, a, 425, 3)
		}
		
		t += 29; o += 10;
		wait(60*1);
	}	
	
	task am{
		//{
			let a; let t = 270; 
			let v = 0.4;
		//}

		while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			wait(60*4); 
			ascent(i in 0..9){
				let A = PolygonShot(bx, by, 1, t, 6, 7, 1, 101+i, 3);
				
				ascent(j in 0..length(A)){
					if(!Obj_IsDeleted(A[j])){
						let SV = Obj_GetValue(A[j], "sv");
						
						ObjMove_AddPatternA2(A[j], 15, 0, NO_CHANGE, (1/16)*SV, v, (2+i/4)*SV);
					}
				}
			}
			
			t = rand(270-40, 270+40); v *= -1;
			DelayedPlaySFX_V(shet, 15, 72);
		}	
	}
}

task laser{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let a; let t = 45; 
		
		let y = -50; let z = 0;
	//}
	
	PlaySFX_V(woo, 123);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		
		ascent(j in 0..25){
			y = -80 + sin(z)*100;
			
			let L = accelLaser(-80, y, 0,4); 

			let M = accelLaser(stgw+80, y, 180, 4); z -= 5;
			wait(60*1/40);
		}

	}	

}


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