#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 02 - Silk Sign \"Impatient Worm\""]
#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"
#include "./../lib/RMTCLenenCutinLib/RMTCLenenCutin.txt"

let objBoss;
let objScene = GetEnemyBossSceneObjectID();

let count = 0; 

let shot = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Shot01.wav");
let charge = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Charge01.wav");
let rebound = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Special02.wav");

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(3600);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(62);
    }
	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("music", false) == false){BGM_Play("Boss", 75); SetCommonData("music", true);}
	
    ObjMove_SetDestAtFrameWeight(objBoss, gx(), gy, 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){
        phases;
        count = 0;
    } 
	
    count++;
    yield;
}

task StartSpell{
wait(60*1);
	ObjEnemyBossScene_StartSpell(objScene);
	
	let objEnemy = GetEnemyBossObjectID[0];
	wait(30);
	
	let rumia = GetCurrentScriptDirectory() ~ "../img/save.png";
	TRMTCLenenCutin(rumia, "Silk Sign \"Impatient Worm\"", false, 255, 153, 255);
}

task TDrawLoop{
	renderBoss(objBoss, "Haiji", 1.12, 60*1/2, false);
	magiccircle(objBoss, 1.6, false);
	
}

task phases{
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(60*4); PlaySFX_V(charge, 65);
	Concentration(bx, by, 60, 300, 3, 2, 3, BLEND_ADD_RGB, 255, 153, 255, 1); 
	ChargeCircle(bx, by, 75, 255, 153, 255, BLEND_ADD_RGB);
	wait(65);
	renderAura(objBoss, "Haiji", 1.12, 60*1/2, 200, [255, 153, 255]);
	ObjEnemy_SetDamageRate(objBoss, 80, 20);

	red(); 
}

task red{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let a; let r = 0; 	
		let v = 3; let n = 60;
	//}

	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	
		r = rand(0,360);
		ascent(l in 0..21){
		
			PlaySFX_V(shot, 65);
			ascent(i in 0..10){
				a = r + 360/10*i;
				
				let A = CreateShotA1(bx, by, 2.5, a, 67, 0); 
				if(i % 5 == 0){
					let B = CreateShotA1(getx(A), gety(A), 2.5, a, 424, 0); setsp(A, 2.5);
					ObjShot_SetSpellResist(B, true); ObjShot_SetSpellResist(A, true);
					
					reflectN1(B, 7, 422, 8); 
					reflectN1(A, 7, 67, 8);
				}
			}
			
			r += n;
			wait(60*1/36);
		}
		
		n *= -1;
		wait(60*7.8);
	}
	
	task reflectN1(obj, time, graph, maxspeed){
		ObjShot_SetSpellResist(obj, true);
		while(getx(obj) > 0 && getx(obj) < stgw && gety(obj) > 0 && gety(obj) < stgh){yield;}
		ObjMove_AddPatternA2(obj, 1, 0, NO_CHANGE, 1/45, 0, maxspeed);
		
		while(time > 0){
			if(getx(obj) < 0 || getx(obj) > stgw || gety(obj) < 0 || gety(obj) > stgh){PlaySFX_V(rebound, 35);}
			
			if(getx(obj) < 0 || getx(obj) > stgw){seta(obj, 180 - geta(obj)); time--; circ();}
			if(gety(obj) < 0 || gety(obj) > stgh){seta(obj, geta(obj) * -1); time--; circ();}
			
			if(getx(obj) < 0){setx(obj, 0);} else if(getx(obj) > stgw){setx(obj, stgw);}		
			if(gety(obj) < 0){sety(obj, 0);} else if(gety(obj) > stgh){sety(obj, stgh);}		
			yield;
		}
		
		task circ(){
			ObjShot_SetGraphic(obj, graph); 
			
			ExplodeCircle(getx(obj), gety(obj), 24, 255, 77, 77, BLEND_ADD_ARGB);
			ExplodeCircle(getx(obj), gety(obj), 30, 77, 166, 255, BLEND_ADD_ARGB);
		}
	}

}

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