#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 1 - Darkness Card \"Melanistic Shot\""]
#Text[""]
#Background["./../BG.txt"]
#Player["./../player/ln/YK.txt", "./../player/ln/YS.txt", "./../player/gt_medicine/PracticeP.txt"]
#System["./../System/Default_System.txt"]

#include "./../lib/LenenShot/JINXShot_Const.txt"

#include "./../FxnSFX.dnh"
#include "./../lib/Funcs.dnh"
#include "./../lib/FXLib.dnh"
#include "./../lib/Render/MagicCircle.txt"

#include "./../lib/Render/LenenBossRender/RenderLenen.txt"
#include "./../lib/RMTCLenenCutinLib/RMTCLenenCutin.txt"

let objBoss;

let count = 0;

let objScene = GetEnemyBossSceneObjectID();
let shot  = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/e_shot_01.wav");
let shot2 = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/e_shot_00.wav");

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(2600);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(76);
    }
	case(EV_REQUEST_IS_DURABLE_SPELL){
		SetScriptResult(true);
	}
    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("wwafawdwg", 85); SetCommonData("music", true);}
	
    ObjMove_SetDestAtFrame(objBoss, gx(), 130, 50);
	StartSpell;
	TDrawLoop;
    TFinalize;
}

@MainLoop{
    ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
    ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24);
    if(count == 0){
        round_bullets;
        count = 0;
    } 
    count++;
    yield;
}

task StartSpell{
wait(25);
	ObjEnemyBossScene_StartSpell(objScene);
	
	let rumia = GetCurrentScriptDirectory() ~ "../img/Cutin.png";
	TRMTCLenenCutin(rumia, "Darkness Card \"Melanistic Shot\"", false, 204, 153, 0)
}

task TDrawLoop{
	renderBoss(objBoss, "Kurohebi", 1, 30, false);
}

task round_bullets{
ObjEnemy_SetDamageRate(objBoss, 0, 0);
wait(360);
ObjEnemy_SetDamageRate(objBoss, 45, 10);
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
    let rang = GetAngleToPlayer(objBoss);
	
	//{
		let ang;
		
		let L = 0;
		let b = 5;
		let v = 13;
	//}

    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		ascent(big_control in 0..999999){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
			ascent(control in 0..90){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
				
				PlaySFX(shot);
				ObjSound_SetVolumeRate(shot, 60);
				ascent(i in 0..6){
					if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
					
					let inv = CreateShotA1(bx, by, rand(5,7), L, 25, 5);
					ObjMove_AddPatternA1(inv, 60*1/5, 0, NO_CHANGE);
					ObjShot_SetSpellResist(inv, true);
					hide(inv, 60*1.25, 32, rand(1/3,3), true, round(rand(25,31)));
					L += b;
				}
				yield;
			}
			
			b *= -1;
			wait(60*1/10);
			
			L = angtoP(objBoss);
			ascent(control in 0..45){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
				
				PlaySFX(shot2); ObjSound_SetVolumeRate(shot2, 55);
				ascent(i in -10..11){
					let inv = CreateShotA1(bx, by, rand(1/3,1), L + i*15, 9, 5);
					ObjShot_SetSpellResist(inv, true);
					hide(inv, 60*0.75, 16, 8, false, round(rand(9,15)));
				}
				
				
				wait(60*1/20);
			}
		}
		wait(60*999);
    }
	
	task hide(shh, calado, dps, mds, vei, noza){
		let a = [-90, 90];
		wait(calado);
		ObjShot_SetGraphic(shh, dps); let dip = rand(mds, mds+1);
		if(vei){seta(shh, geta(shh) + a[round(rand(0,1))]); setsp(shh, mds);}
		else{setsp(shh, mds);} 
		while(GetObjectDistance(shh, GetPlayerObjectID()) > 110){yield;}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
		ObjShot_SetSpellResist(shh, !vei);
		ObjShot_SetGraphic(shh, noza);
	}
}	

task DelayLaser(x, y, ang, l, w, dt, graph, delay){
	let objlaser = CreateStraightLaserA1(x, y , ang, l, w, dt, graph, delay);
	ObjStLaser_SetSource(objlaser, false);
	//ObjStLaser_SetAngle(objlaser, angtoP(objBoss));
	
	loop(delay-1){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){Obj_Delete(objlaser); return;}
		yield;
	}
	Obj_Delete(objlaser);
}


task TFinalize {
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
		SetCommonData("extend", true);
		SetPlayerLife(GetPlayerLife() + 1);
		AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));
    }
	
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}