#TouhouDanmakufu[Stage]
#Title["An Unknown White UFO"]
#Text["For LoCAA 8 contest[r]Difficulty: Purely Ultra"]
#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 path1 = GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/bgm1.ogg";
let furies = GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/purefuries.ogg";
let alien = GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/bgm2.ogg";

@Initialize{
SetPlayerLife(30);
SetCommonData("fade",0);
SetCommonData("deathsound",0);
SetCommonData("heianalien",0);
SetCommonData("purefuries",0);
SetCommonData("musicswitch",0);
STAGE;
}

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

yield;
}

task BGM{
wait(10);
let obj = ObjSound_Create;
ObjSound_Load(obj, path1);
ObjSound_SetLoopEnable(obj,true); 
ObjSound_SetVolumeRate(obj,90);
ObjSound_SetLoopTime(obj, 36.110, 60*2+56.406);

let obj2 = ObjSound_Create;
ObjSound_Load(obj2, furies);
ObjSound_SetLoopEnable(obj2,true); 
ObjSound_SetVolumeRate(obj2,90);

let obj3 = ObjSound_Create;
ObjSound_Load(obj3, alien);
ObjSound_SetLoopEnable(obj3,true); 
ObjSound_SetVolumeRate(obj3,90);
ObjSound_SetLoopTime(obj3, 60+28.343, 60*2+56.248);

ObjSound_Play(obj);
loop{
	loop{
		if(GetCommonData("purefuries",0) == 1 && GetCommonData("musicswitch",0) == 1){ 
		ObjSound_Play(obj2);
		ObjSound_Stop(obj);
		SetCommonData("musicswitch",0);
		break;
		}
		if(GetCommonData("heianalien",0) == 1 && GetCommonData("musicswitch",0) == 1){ 
		ObjSound_Play(obj3);
		ObjSound_Stop(obj);
		SetCommonData("musicswitch",0);
		break;
		}
		if(GetCommonData("purefuries",0) == 0 && GetCommonData("musicswitch",0) == 1){ 
		ObjSound_Play(obj);
		ObjSound_Stop(obj2);
		SetCommonData("musicswitch",0);
		break;
		}
		if(GetCommonData("fade",0) == 1){break;}
	yield;
	}
	if(GetCommonData("fade",0) == 1){break;}
}
let volume = ObjSound_GetVolumeRate(obj);
while(volume>0){
	volume--;
	ObjSound_SetVolumeRate(obj,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{
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/SP4.txt");
	ObjEnemyBossScene_Add(obj,4,dir~"./EverythingElse/SP5.txt");
    ObjEnemyBossScene_LoadInThread(obj);
	DeathSoundLoop;
	DeathEffect;
wait(50);
TimerCountdown(obj2);
ObjEnemyBossScene_Regist(obj2);
    while(!Obj_IsDeleted(obj2)){
        yield;
    }
wait(200);
ObjEnemyBossScene_Regist(obj);
	TimerCountdown(obj);
    while(!Obj_IsDeleted(obj)){
        yield;
    }
	SetCommonData("fade",1);
wait(350);
CloseStgScene;
}