#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 2 - Slash Card \"Slit Dragon 'Stealth'\""]
#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", 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, "Slash Card \"Slit Dragon 'Stealth'\"", 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 dist = 70; let way = 11;
		let m = 10;
	//}

    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		ascent(big_control in 0..999999){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
			
			ascent(i in 0..5){
				ang = r + 360/5*i; 
				let spi = CreateShotA1(bx + (dist)*cos(ang), by + (dist)*sin(ang), 0, ang, 90, 5); spin(spi, dist, way);
			}
			
			way *= -1; r = rand(0,360);
			wait(60*5);
		}
		wait(60*999);
    }
	
	task spin(of, dir, mod){
		DelayLaser(getx(of), gety(of), angtoB(of), 800, 100, 1, 169, 60*1);
		
		let an = geta(of); 
		if(mod > 0){seta(of, an+90);} else{seta(of, an-90);}
		
		wait(60*1);
		
		ObjShot_SetSpellResist(of, true);
		ascent(i in 0..(720/absolute(mod)) + 1){
			if(Obj_IsDeleted(of)){return;}
			ObjMove_SetPosition(of, bx + dir*cos(an), by + dir*sin(an));
			if(mod > 0){seta(of, an+90);} else{seta(of, an-90);}
				
			PlaySFX(shot); ObjSound_SetVolumeRate(shot, 55);
			CreateShotA1(getx(of), gety(of), rand(1/2,2), geta(of), 45, 0);
			
			an += mod;
			yield;
		}
		
		PlaySFX(shot2); ObjSound_SetVolumeRate(shot, 75);
		ObjMove_AddPatternA2(of, 1, 0, angtoB(of), 1, 0, 10); thrill(of);
		task thrill(oui){
			wait(2);
			let con = geta(oui); 
			let vt = [con-90, con+90]; let p = 0;
			
			while(!Obj_IsDeleted(oui)){
				let bai = CreateShotA1(getx(oui), gety(oui), rand(1/4,4), vt[p], 26, 5);
				p++; if(p > 1){p = 0;}
				let oh = CreateShotA1(getx(oui), gety(oui), rand(1/4,4), vt[p], 26, 5);
				p++; if(p > 1){p = 0;}
				
				ObjMove_AddPatternA2(bai, 60*1/2, NO_CHANGE, NO_CHANGE, -1/8, 0, 1); ObjMove_AddPatternA2(oh, 60*1/2, NO_CHANGE, NO_CHANGE, -1/8, 0, 1);
				wait(60*1/40);
			}
		}
	}
}	

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;
}