#TouhouDanmakufu[Stage]
#Title["Orin Boss Fight"]
#Text["By Meowstic [r]Difficulty: Slightly hard"]
#ScriptVersion[3]
#System["./default_system/Default_System.txt"]

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

let bgm = GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/BGM.ogg";

@Initialize{
SetPlayerLife(20);
SetCommonData("musicstart",0);
SetCommonData("deathsound",0);
STAGE;
BGM;
}

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

yield;
}

@Finalize{}

task BGM{
wait(140);
let obj = ObjSound_Create;
ObjSound_Load(obj, bgm);
ObjSound_SetLoopEnable(obj,true); 
ObjSound_SetVolumeRate(obj,90);
ObjSound_SetLoopTime(obj, 60+27.386, 60*3+36.730);
ObjSound_Play(obj);
while(GetCommonData("musicswap",0) < 1){yield;}
let volume = ObjSound_GetVolumeRate(obj);
while(volume>0){
	volume--;
	ObjSound_SetVolumeRate(obj,volume);
	wait(2);
}
}

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",[100, 00, 00]);
	SpellBurstB1(GetCommonData("BossX", 1),GetCommonData("BossY", 1),10,[100, 00, 00]);
	}
	yield;
	SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/death.wav", 100);
	SetCommonData("Died",0);
	yield;
	}
}


task STAGE{
    let dir=GetCurrentScriptDirectory();
    let obj=ObjEnemyBossScene_Create();
	PlaytestInvincibility;
	SetCommonData("IsStage", 1);
    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_LoadInThread(obj);
	DeathSoundLoop;
	DeathEffect;
wait(280);
ObjEnemyBossScene_Regist(obj);
	TimerCountdown(obj);
    while(!Obj_IsDeleted(obj)){
        yield;
    }
wait(60);
SetCommonData("musicswap",1);
wait(290);
CloseStgScene;
}