#TouhouDanmakufu[Stage]
#Title["Youkai Road"]
#Text["Difficulty: Warm"]
#ScriptVersion[3]
//#System["./default_system/Default_SystemFullScreen.txt"]
#System["./default_system/Default_System.txt"]
#Player["./players/Reimu Hakurei/Player_Reimu.txt","./players/Marisa Kirisame/Player_Marisa.txt","./players/Sanae Kochiya/Player_Sanae.txt"]
#include "./default_system/MS_Index.txt"

let bgm1path = GetCurrentScriptDirectory ~ "default_system/se/bgm1.ogg";
let bgm2path = GetCurrentScriptDirectory ~ "default_system/se/bgm2.ogg";
let paths = [bgm1path, bgm2path];

@Initialize{
LoadCommonDataAreaA2("HighScore",GetCurrentScriptDirectory ~ "./default_system/score.dat");
SetCommonData("IsStage", true);

WorldBG;
LoadAssets;
STAGE;
BGM;
PoC;
BorderLineIcon;
Difficulty(2);
}

task PoC{
loop{
if(GetPlayerY<150){CollectAllItems;}
yield;
}
}

@MainLoop{
if(GetPlayerState()==STATE_END){
	loop(60){yield;}
	CloseStgScene();
}

yield;
}

@Finalize{}

/*
task BGM{
wait(60);
	let obj	= ObjSound_Create();
	ObjSound_Load(obj,GetCurrentScriptDirectory ~ "Default_System/se/bgm1.ogg");
	ObjSound_SetSoundDivision(obj,SOUND_BGM);
	ObjSound_SetVolumeRate(obj,85);
	ObjSound_SetLoopEnable(obj,true);
	ObjSound_SetLoopTime(obj, 11.857, 60*3+15.777);
	ObjSound_Play(obj);
}*/

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],70);}
ObjSound_SetLoopTime(bgm[0], 0, 60*6);
ObjSound_SetLoopTime(bgm[1], 12.401, 60*2+23.569);
ObjSound_Play(bgm[0]);
while(GetCommonData("IsBoss", 0) == 0){yield;}
ascent(i in 0..60){
ObjSound_SetVolumeRate(bgm[0],255-i*4.25);
yield;
}
ObjSound_Stop(bgm[0]);
wait(120);
ObjSound_SetVolumeRate(bgm[1],70);
ObjSound_Play(bgm[1]);


}

task STAGE{
	let Stgpart1 = GetCurrentScriptDirectory ~ "./Stgpt1.txt";
	let part1 = LoadScript(Stgpart1);
	StartScript(part1);
	
	while(IsCloseScript(part1) == false){
	yield;
	}
	
	
	
    let objmid=ObjEnemyBossScene_Create();
    ObjEnemyBossScene_Add(objmid,1,GetCurrentScriptDirectory ~ "./Mid1.txt");
    ObjEnemyBossScene_Add(objmid,2,GetCurrentScriptDirectory ~ "./Mid2.txt");
	ObjEnemyBossScene_LoadInThread(objmid);
	
	
    let obj=ObjEnemyBossScene_Create();
    ObjEnemyBossScene_Add(obj,1,GetCurrentScriptDirectory ~ "./NS1.txt");
    ObjEnemyBossScene_Add(obj,1,GetCurrentScriptDirectory ~ "./SP1.txt");
    ObjEnemyBossScene_Add(obj,2,GetCurrentScriptDirectory ~ "./NS2.txt");
    ObjEnemyBossScene_Add(obj,2,GetCurrentScriptDirectory ~ "./SP2.txt");
    ObjEnemyBossScene_Add(obj,3,GetCurrentScriptDirectory ~ "./NS3.txt");
    ObjEnemyBossScene_Add(obj,3,GetCurrentScriptDirectory ~ "./SP3.txt");
    ObjEnemyBossScene_Add(obj,4,GetCurrentScriptDirectory ~ "./NS4.txt");
    ObjEnemyBossScene_Add(obj,4,GetCurrentScriptDirectory ~ "./SP4.txt");
    ObjEnemyBossScene_Add(obj,5,GetCurrentScriptDirectory ~ "./SP5.txt");
	ObjEnemyBossScene_Add(obj,5,GetCurrentScriptDirectory ~ "./NSEnd.txt");
	ObjEnemyBossScene_LoadInThread(obj);
	
	RipMami;
	RipNon;
	RipSpell;
	BossIsDed;
	
	
	ObjEnemyBossScene_Regist(objmid);
	TimerCountdown(objmid);
    while(!Obj_IsDeleted(objmid)){
        yield;
    }
	SetCommonData("RenderTimer", 0);
	
	SetCommonData("IsBoss", 1);
	wait(60*5);
	
	SetCommonData("RenderTimer", 1);
	ObjEnemyBossScene_Regist(obj);
	TimerCountdown(obj);
    while(!Obj_IsDeleted(obj)){
        yield;
    }
	
	wait(120);
	StageClear(1000000);
	wait(90);
	if(GetCommonData("GotHighScore", 0) == 1){Highscore;}
	wait(90);
	SaveCommonDataAreaA2("HighScore",GetCurrentScriptDirectory ~ "./default_system/score.dat");
	wait(30);
	CloseStgScene;
}

task RipNon{
	loop{
	while(GetCommonData("RipNon",0) < 1){yield;}
	PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/explode.wav", 90, 1);
	ExplodeCircleT(GetCommonData("bossx", 0), GetCommonData("bossy", 0), 60, 12, 0);
	SetCommonData("RipNon",0);
	yield;
	}
}

task RipMami{
	loop{
	while(GetCommonData("RipMami",0) < 1){yield;}
	PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/barrage.wav", 90, 1);
	loop(60){ParticleT(GetCommonData("bossx", 0), GetCommonData("bossy", 0), rand(0.5, 10), rand(0,360), 0.6, rand(0,360), "Cherry", [255, 10, 10], 4);}
	loop(40){CreateItemU1(1, GetCommonData("bossx", 0)+rand(-100, 100), GetCommonData("bossy", 0)+rand(-100, 100), 1000);}
	ascent(i in 0..60){
	let random = rand(0,360);
	let randomrad = rand(0, 25);
	MamiSmoke(GetCommonData("bossx", 0)+randomrad*cos(random),  GetCommonData("bossy", 0)+randomrad*sin(random), rand(1, 5), rand(0,360), 0.5, 0, 5, 30);
	}
	SetCommonData("RipMami",0);
	yield;
	}
}

task RipSpell{
	loop{
	while(GetCommonData("RipSpell",0) < 1){yield;}
	PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/cardbreak.wav", 90, 1);
	loop(60){ParticleT(GetCommonData("bossx", 0), GetCommonData("bossy", 0), rand(0.5, 10), rand(0,360), 0.6, rand(0,360), "Cherry", [255, 10, 10], 4);}
	loop(40){CreateItemU1(1, GetCommonData("bossx", 0)+rand(-100, 100), GetCommonData("bossy", 0)+rand(-100, 100), 1000);}
	SetCommonData("RipSpell",0);
	yield;
	}
}

task BossIsDed{
	loop{
	while(GetCommonData("BossIsDed",0) < 1){yield;}
	PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/death.ogg", 90, 1);
	ScreenShake(120,15);
	Slowdown;
	loop(120){ParticleT(GetCommonData("bossx", 0), GetCommonData("bossy", 0), rand(0.5, 10), rand(0,360), 0.6, rand(0,360), "Cherry", [255, 10, 10], 4);}
	ascent(i in 0..6){ExplodeCircleT(GetCommonData("bossx", 0), GetCommonData("bossy", 0), 60, i*12, 0);}
	loop(40){CreateItemU1(1, GetCommonData("bossx", 0)+rand(-100, 100), GetCommonData("bossy", 0)+rand(-100, 100), 1000);}
	SetCommonData("BossIsDed",0);
	yield;
	}
}

task Slowdown{
StartSlow(TARGET_ALL, 15);
wait(20);
StopSlow(TARGET_ALL);
}