#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 01 - Icy Wave \"Joyful Dance\""]
#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 laser = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Laser02.wav");
let magik = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Special01.wav");
let charge = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Charge01.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++;
	
	//DeleteShotInCircle(TYPE_ALL, TYPE_IMMEDIATE, bx, by, 2);
    yield;
}

task StartSpell{
wait(60*1);
	ObjEnemyBossScene_StartSpell(objScene);
	
	let objEnemy = GetEnemyBossObjectID[0];
	wait(30);
	
	let rumia = GetCurrentScriptDirectory() ~ "../img/save.png";
	TRMTCLenenCutin(rumia, "Icy Wave \"Joyful Dance\"", false, 102, 190, 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, 102, 190, 255, 1); 
	ChargeCircle(bx, by, 75, 102, 190, 255, BLEND_ADD_RGB);
	wait(65);
	renderAura(objBoss, "Haiji", 1.12, 60*1/2, 200, [102, 190, 255]);
	
	ObjEnemy_SetDamageRate(objBoss, 60, 20);

	red(); 
}


task red{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let a; let r = 45;
		let n = 0.7;
	//}
	
	task laz{
		PlaySFX_V(laser, 65);
		ascent(k in 0..2){
			ascent(i in 0..4+1){
				ExplodeCircle(  -5  , i*stgh/4, 30, 204, 255, 255, BLEND_ADD_RGB);
				ExplodeCircle(stgw+5, i*stgh/4, 30, 204, 255, 255, BLEND_ADD_RGB);
				
				let L = CreateCurveLaserA1(  -5  , i*stgh/4, 4, 000, 100, 20, 161, 1);
				let R = CreateCurveLaserA1(stgw+5, i*stgh/4, 4, 180, 100, 20, 161, 1);
				
				ObjMove_AddPatternA2(L, 1, NO_CHANGE, NO_CHANGE, 1/10, +n, 8);
				ObjMove_AddPatternA2(R, 1, NO_CHANGE, NO_CHANGE, 1/10, -n, 8);
				ObjCrLaser_SetTipDecrement(L, 0.0); ObjCrLaser_SetTipDecrement(R, 0.0);

			}
			n *= -1;
		}
	}
	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		PlaySFX_V(shot, 65);
		ascent(i in -2..6+2){
			let B = CreateShotA1(i*stgw/6, 10, 0, 90, 426, 5);
			ChargeCircle(i*stgw/6, 10, 45, 0, 153, 153, BLEND_ADD_RGB); sinwav(B, 6, 100, 60*1/23);
		}
		
		wait(60*4);
		
		laz();
		
		n *= -1;
		wait(60*3);SetCommonData("showtime", true);
		wait(60*4);
		
		PlaySFX_V(shot, 65);
		ascent(i in -2..6+2){
			let B = CreateShotA1(i*stgw/6, stgh-10, 0, 270, 426, 5);
			ChargeCircle(i*stgw/6, stgh-10, 45, 0, 153, 153, BLEND_ADD_RGB); sinwav(B, 5, 100, 60*1/20);
		}
		
		wait(60*4);
		
		laz();
		
		n *= -1;
		wait(60*3);SetCommonData("showtime", true);
		wait(60*3);
	}
	
	task sinwav(eu, spe, range, y){
		//{
			let B = []; let j = 0; let u = 0;
			
			let r = geta(eu); let s = r;
			let v = 4;
		//}
		
		SetCommonData("showtime", false); ObjShot_SetSpellResist(eu, true); wait(45);
		setsp(eu, spe);
		while(!Obj_IsDeleted(eu)){
			PlaySFX_V(shot, 50); 
			B = B ~ [CreateShotA1(getx(eu) + cos(u)*range, gety(eu), 0, r, 127, 45)];
			ObjShot_SetSpellResist(B[j], true);
			
			j++;
			
			B = B ~ [CreateShotA1(getx(eu) - cos(u)*range, gety(eu), 0, s, 127, 45)];
			ObjShot_SetSpellResist(B[j], true);
			r += v; s -= v;  
			u += 7;
			
			j++;
			
			wait(y);
		}

		while(GetCommonData("showtime", false) == false){yield;}
		PlaySFX_V(magik, 55);
		ascent(i in 0..length(B)){
			if(!Obj_IsDeleted(B[i])){
				ObjMove_AddPatternA2(B[i], 1, 0, NO_CHANGE, -1/60, 0, -2);
			}
		}
	}
}

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