#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 3 - Imperceptible"]
#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 warn  = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/warning.wav");
let shot  = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/e_shot_01.wav");
let ded = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/boss_end.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", 75); 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, "\"Imperceptible\"", 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, 20);
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
    let rang = GetAngleToPlayer(objBoss);
	
	//{
		let ang; let r = 0;
		let x = rand(gx-80,gx+80); let y = rand(40,140);
	//}

	
	PlaySFX(warn); ObjSound_SetVolumeRate(warn, 55);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		rain();
		
		ascent(big_control in 0..999999){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
			
			PlaySFX(shot); ObjSound_SetVolumeRate(shot, 55);
			r = 0;
			ascent(i in 0..55){
				ang = r + 360/55*i;
					
				let hd = CreateShotA1(x, y, 12, ang, 96, 5);
				hide(hd, 130, ang, 2, 92);
			}
			
			r = rand(0,360);
			ascent(i in 0..28){
				ang = r + 360/28*i;
					
				let hd = CreateShotA1(bx, by, 2, ang, 90, 5);
			}

			x = rand(gx-80,gx+80); y = rand(40,140);
			wait(60*1/3);
		}
		wait(60*999);
    }

	task hide(ob, num, ang, spe, v){
		ObjRender_SetAlpha(ob, 0);
		while(GetObjectDistance(ob, GetPlayerObjectID()) > num){yield;}
		ObjRender_SetAlpha(ob, 255);
		ObjMove_AddPatternA1(ob, 1, spe, ang); ObjShot_SetGraphic(ob, v);
		
		while(!Obj_IsDeleted(ob)){
			if(ObjMove_GetSpeed(ob) <= 0){Obj_Delete(ob);}
			yield;
		}
	}
	
	task rain(){
		while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			ascent(i in 0..14){
				let hmm = CreateShotA1(i*stgw/13, 0, 6, 90, 40, 5);
				rad(hmm, 50);
			}
			
			wait(60*1/15);
		}
		
		task rad(ob, num){
			ObjShot_SetSpellResist(ob, true);
			while(GetObjectDistance(ob, GetPlayerObjectID()) > num){yield;}
			Obj_Delete(ob);
		}
	}
}	

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));
    }
	
	ObjMove_SetDestAtFrame(objBoss, bx + 60, by - 60, 60*2);
	ascent(i in 0..80){
		ExplodeCircle(rand(bx-80, bx+80), rand(by-80, by+80), 60*4, 204-i, 153-i, 0, BLEND_ADD_ARGB);
		ObjRender_SetAlpha(objBoss, 255-i*2);
		yield;
	}
	PlaySFX(ded);
	
	StartSlow(TARGET_ALL, 45);
		ConcentrationExplosion(bx, by, 60*2, 50, 10/4, 2, BLEND_ADD_ARGB, 204, 153, 0, 5);
		wait(60*2);
	StopSlow(TARGET_ALL); 

	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
    Obj_Delete(objBoss);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}