#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["Perfect Possession - Yukari and Junko"]
//#BGM["script/resources/music/stage1.ogg"]
///#Player["./Okina Matara/Okina.dnh"]
#Text["The Perfect Possession[r] continues! [r] The divine avatars of [r]purity and chaos [r] stand before you."]
#System["./default_system/Default_System.txt"]
#include"script/default_system/Default_Effect.txt"
#include"./resources/include.txt"
#include"./StageBG.txt"

let frame=0;
let animation=0;
let angle=0;
let scoremode= false;
let difficulty = 2;
let objstg;
let objboss;
let objtlb;
let music = 0;

@Event{
	if (GetEventType == EV_PAUSE_ENTER) {
				// stop 7bgm
			ObjSound_Stop(objstg);
			ObjSound_Stop(objboss);
			ObjSound_Stop(objtlb);
			}

		else if (GetEventType == EV_PAUSE_LEAVE) {
			///resume bgm
			if(music==0){
				ObjSound_Play(objstg);
				}
				
			if(music==2){
				ObjSound_Play(objboss);
				}
				
			if(music==4){
				ObjSound_Play(objtlb);
				}
				
			}
		}

@Initialize{

	SetPlayData;
	StageControl;
	//Part2
	//FistFairy(GetStgFrameWidth/2, 135, 50, 400, 8, FIRE_RED);
	SetCommonData("Difficulty", difficulty);
	SetCommonData("Score Attack", scoremode);
	}
	
@MainLoop{
	//SetPlayerInvincibilityFrame(10);
	yield;	
	}
	
task SetPlayData{

	SetPlayerLife(20);
	SetPlayerSpell(2);
	
	}
	
task StageControl{
	let StageManageObject = ObjPrim_Create(OBJ_SPRITE_2D);
	//Obj_SetValue(StageManageObject, SpellDetect, false);
	ObjMove_SetPosition(StageManageObject, -60, -60);
	StageBG(StageManageObject);
	SoundCheck(StageManageObject);
	
	
	
	wait(100);
	
	Boss(StageManageObject);
	
	
	
	wait(300);
	
	//RaiseError("EEEEEEEEEEEEEEEEEEEEEEEH");
	
	CloseStgScene;
	CloseScript(GetOwnScriptID()); //Close the plural.
	
	}
	
function Midboss(tobj){
	let dir=GetCurrentScriptDirectory();
	let obj=ObjEnemyBossScene_Create();
	ObjEnemyBossScene_Add(obj,0,dir~"KaguyaNon1.txt");
	ObjEnemyBossScene_Add(obj,0,dir~"KaguyaSpell1.txt");
	ObjEnemyBossScene_Add(obj,1,dir~"KaguyaNon2.txt");
	ObjEnemyBossScene_Add(obj,1,dir~"KaguyaSpell2.txt");

	
	ObjEnemyBossScene_LoadInThread(obj);
	ObjEnemyBossScene_Regist(obj);
	
	while(!Obj_IsDeleted(obj)){
		yield;
		}
	
	//Part2;
	}
	
function Boss(tobj){
	let dir3=GetCurrentScriptDirectory();
	let obj2=ObjEnemyBossScene_Create();
	///////
	ObjEnemyBossScene_Add(obj2,0,dir3~"JunkoNon1.txt");
	ObjEnemyBossScene_Add(obj2,1,dir3~"JunkoNon2.txt");

	ObjEnemyBossScene_Add(obj2,0,dir3~"JunkoSpell1.txt");
	ObjEnemyBossScene_Add(obj2,1,dir3~"JunkoSpell2.txt");

	//ObjEnemyBossScene_Add(obj2,0,dir3~"JunkoSpell1.txt");
	//ObjEnemyBossScene_Add(obj2,1,dir3~"JunkoNon1.txt");
	//ObjEnemyBossScene_Add(obj2,1,dir3~"JunkoSpell1.txt");
	///ObjEnemyBossScene_Add(obj2,2,dir3~"JunkoNon3.txt");

	music = 1;
	ObjEnemyBossScene_LoadInThread(obj2);
	ObjEnemyBossScene_Regist(obj2);
	
	while(!Obj_IsDeleted(obj2)){
		yield;
		}
	
	//Part2;
	}

	
	
task SoundCheck(tobj){
	let dir = GetCurrentScriptDirectory;
	let dir1 = dir~"stgbgm.ogg";
	let dir2 = dir~"bgm1.mp3";
	let dir3 = dir~"tlbbgm.ogg";
	objstg = ObjSound_Create;
	ObjSound_Load(objstg, dir1);
	ObjSound_SetLoopEnable(objstg, true);
	//ObjSound_SetLoopSampleCount(objstg, 33880, 4061846);
	ObjSound_SetLoopSampleCount(objstg, 221184, 4233216);
	ObjSound_SetRestartEnable(objstg, true);
	let Vol = ObjSound_GetVolumeRate(objstg);
	ObjSound_SetVolumeRate(objstg, Vol*2);
	ObjSound_Play(objstg);
	
	objboss = ObjSound_Create;
	ObjSound_Load(objboss, dir2);
	//ObjSound_Load(objboss, dir1);
	ObjSound_SetLoopEnable(objboss, true);
	///ObjSound_SetLoopSampleCount(objboss, 1911118, 3649540);
	///ObjSound_SetLoopSampleCount(objboss, 221184, 4233216);
	ObjSound_SetRestartEnable(objboss, true);
	//let Vol = ObjSound_GetVolumeRate(objstg);
	ObjSound_SetVolumeRate(objboss, 40);
	
	objtlb = ObjSound_Create;
	ObjSound_Load(objtlb, dir3);
	//ObjSound_Load(objtlb, dir1);
	ObjSound_SetLoopEnable(objtlb, true);
	///ObjSound_SetLoopSampleCount(objtlb, 1911118, 3649540);
	///ObjSound_SetLoopSampleCount(objtlb, 221184, 4233216);
	ObjSound_SetRestartEnable(objtlb, true);
	//let Vol = ObjSound_GetVolumeRate(objstg);
	ObjSound_SetVolumeRate(objtlb, 40);
	
	while(!Obj_IsDeleted(tobj)){
		
		if(music==1&&ObjSound_IsPlaying(objstg)){
			music = 2;
			ObjSound_Stop(objstg);
			
			wait(20);
			
			ObjSound_Play(objboss);
			
			}
			
		if(music==3&&ObjSound_IsPlaying(objboss)){
			music = 4;
			ObjSound_Stop(objboss);
			
			wait(20);
			
			ObjSound_Play(objtlb);
			}
		
		yield;
		}
	
	Obj_Delete(objstg);
	Obj_Delete(objboss);
	}
	
function wait(r){
	loop(r){
		yield;
		}
		
	}
	
function TLB(tobj){
	let dir5=GetCurrentScriptDirectory();
	let obj5=ObjEnemyBossScene_Create();
	ObjEnemyBossScene_Add(obj5,0,dir5~"GuilTLB1.txt");
	ObjEnemyBossScene_Add(obj5,0,dir5~"GuilTLB2.txt");
	ObjEnemyBossScene_Add(obj5,1,dir5~"GuilTLB3.txt");
	
	music = 3;
	
	loop(200){
		yield;
		}
	ObjEnemyBossScene_LoadInThread(obj5);
	ObjEnemyBossScene_Regist(obj5);
	
	while(!Obj_IsDeleted(obj5)){
		yield;
		}
	}