#TouhouDanmakufu[Stage]
#Title["The Strings of the Spider's Web"]
#Text["Inspired in RaNGE 16's Rules[r]Dedicated to DLS[r]Difficulty: [r]Prepare to Die Edition"]
#ScriptVersion[3]
#System["./default_system/Default_System.txt"]
#Player["./player/Reimu Hakurei/Player_Reimu.txt","./player/Marisa Kirisame/Player_Marisa.txt","./player/Sakuya Izayoi/Player_Sakuya.txt","./player/Sanae Kochiya/Player_Sanae.txt","./player/Youmu Kompaku/Player_Youmu.txt"]

#include "./EverythingElse/Functions/Index.txt"
#include "./EverythingElse/Functions/BGStage.txt"
let objBoss; //Because errors.
let PlayX;
let PlayY;

let bgm1path = GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/bgmstage.ogg";
let bgm2path = GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/bgmboss.ogg";
let paths = [bgm1path,bgm2path];

@Initialize{
SetPlayerLife(3);
SetCommonData("fade",0);
SetCommonData("deathsound",0);//SET ALL OF THEM COMMON DATAS!!!!!
SetCommonData("musicswap",0);
SetCommonData("Downwards", 0);
SetCommonData("Fade", 0);
SetCommonData("Fade2", 0);
SetCommonData("Midboss", 0);
SetCommonData("Speedup", 0);
SetCommonData("Fade3", 0);
SetCommonData("IsStage", true);
STAGE;
}

@MainLoop{
if(GetPlayerState()==STATE_END){
	wait(120);
	CloseStgScene();
}

yield;
}

task BGM{
let a = ObjSound_Create;
let b = ObjSound_Create;
let bgm = [a,b];
ascent(i in 0..2){ObjSound_Load(bgm[i],paths[i]); 
ObjSound_SetLoopEnable(bgm[i],true); 
ObjSound_SetVolumeRate(bgm[i],30);}
ObjSound_SetLoopTime(bgm[0], 0, 60*6);
ObjSound_SetLoopTime(bgm[1], 14.5, 60*2+56.082);
ObjSound_Play(bgm[0]);
while(GetCommonData("musicswap",0) < 1){yield;}
descent(i in 5..0){
ObjSound_SetVolumeRate(bgm[0],51*i);
}
ObjSound_Stop(bgm[0]);
ObjSound_SetVolumeRate(bgm[1],80);
ObjSound_Play(bgm[1]);
while(GetCommonData("musicswap",0) < 2){yield;}
let volume = ObjSound_GetVolumeRate(bgm[1]);
while(volume>0){
	volume--;
	ObjSound_SetVolumeRate(bgm[1],volume);
	wait(2);
}
}

@Finalize{}

task DeathSoundLoop{
	loop{
	while(GetCommonData("deathsound",0) < 1){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/death2.wav", 100);
	SetCommonData("deathsound",0);
	yield;
	}
}

task DeathEffect{
	loop{
	while(GetCommonData("Died",0) < 1){yield;}
	loop(3){
	SpellBurstA1(GetCommonData("BossX", 1),GetCommonData("BossY", 1),"Cherry",[255,255,255]);
	SpellBurstB1(GetCommonData("BossX", 1),GetCommonData("BossY", 1),10,[255,255,255]);
	}
	yield;
	SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/death.wav", 100);
	SetCommonData("Died",0);
	yield;
	}
}

task STAGE{
AddLives;
BGM;
    let dir=GetCurrentScriptDirectory();
    let obj=ObjEnemyBossScene_Create();
	let obj2=ObjEnemyBossScene_Create();
	PlaytestInvincibility;
	ObjEnemyBossScene_Add(obj2,0,dir~"./EverythingElse/Mid1.txt");
	ObjEnemyBossScene_Add(obj2,0,dir~"./EverythingElse/Mid2.txt");
    ObjEnemyBossScene_Add(obj,0,dir~"./EverythingElse/NS1.txt");
    ObjEnemyBossScene_Add(obj,0,dir~"./EverythingElse/SP1.txt");
    ObjEnemyBossScene_Add(obj,1,dir~"./EverythingElse/NS2.txt");
    ObjEnemyBossScene_Add(obj,1,dir~"./EverythingElse/SP2.txt");
	ObjEnemyBossScene_Add(obj,2,dir~"./EverythingElse/NS3.txt");
	ObjEnemyBossScene_Add(obj,2,dir~"./EverythingElse/SP3.txt");
	ObjEnemyBossScene_Add(obj,3,dir~"./EverythingElse/NS4.txt");
	ObjEnemyBossScene_Add(obj,3,dir~"./EverythingElse/SP4.txt");
	ObjEnemyBossScene_Add(obj,4,dir~"./EverythingElse/SP5.txt");
	ObjEnemyBossScene_Add(obj,5,dir~"./EverythingElse/SP6.txt");
	ObjEnemyBossScene_Add(obj,6,dir~"./EverythingElse/SP7.txt");
    ObjEnemyBossScene_LoadInThread(obj);
	ObjEnemyBossScene_LoadInThread(obj2);
	DeathSoundLoop;
	DeathEffect;
	
	wait(120);

	wait(400);
	ObjEnemyBossScene_Regist(obj2);
	TimerCountdown(obj2);
    while(!Obj_IsDeleted(obj2)){
        yield;
    }
	SetCommonData("Speedup", 1);
		loop{
		if(GetCommonData("Fade3",0)==1){FadeWhite; break;}
		yield;
		}
	wait(400);
ObjEnemyBossScene_Regist(obj);
	TimerCountdown(obj);
    while(!Obj_IsDeleted(obj)){
        yield;
    }
	SetCommonData("fade",1);
	SetCommonData("musicswap",2);
wait(350);
CloseStgScene;
}