#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["Demon bear is trying his best."]
#Image["./img/Tray.png"]
#Text["Debug Keys:[r]L - Life++;[r]S - Spell++;[r]I - Invincible"]
#Background["./BG.txt"]
#Player["./TrickyPlayer/Tricky.dnh"]
#System["./system/Default_System.dnh"]

#include "./Loading.dnh"
#include "./lib/SFXConstList.dnh"

let dir = GetCurrentScriptDirectory();
const int numScripts = 3; let current;

	let bgm = [ObjSound_Create(), ObjSound_Create()];
	ObjSound_Load(bgm[0], dir ~ "./lib/BGM/Prevent_Fire.ogg"); 
	ObjSound_Load(bgm[1], dir ~ "./lib/BGM/Drek.mp3");
	ascent(i in 0..2){
		ObjSound_SetSoundDivision(bgm[i], SOUND_BGM);
		ObjSound_SetResumeEnable(bgm[i], true);
		ObjSound_SetVolumeRate(bgm[i], 70);
		ObjSound_SetLoopEnable(bgm[i], true);
	}

	ObjSound_SetVolumeRate(bgm[1], 80);
	ObjSound_SetLoopTime(bgm[0], 0, 60*4+15.5);
	ObjSound_SetLoopTime(bgm[1], 0, 60*5);

@Initialize{
	Difficult(2, [GetStgFrameWidth - 50, 20]);
	SetCommonData("music", true);
	
	TPlural;
	SetPlayerLife(9);
	SetPlayerSpell(6);
}

@Loading {
	LoadStuff(numScripts);
}

@Event{
	alternative(GetEventType())
	case(EV_USER_STAGE + 10){
		ObjSound_Stop(bgm[0]);
		ObjSound_Play(bgm[1]); current = 1;
		BGMName("BGM: Demon Beast ~ Bear with Me!", 200, -1);
	}
	case(EV_PAUSE_ENTER){ObjSound_Stop(bgm[current]);}
	case(EV_PAUSE_LEAVE){ObjSound_Play(bgm[current]);}
}

@MainLoop{
	if(GetKeyState(KEY_I) == KEY_PUSH){
		SetCommonData("PlayerOP", true);
		SetPlayerInvincibilityFrame(216000 * 4); //4 hours
	}
	if(GetKeyState(KEY_L) == KEY_PUSH){
		SetPlayerLife(GetPlayerLife() + 1);
	}
	if(GetKeyState(KEY_S) == KEY_PUSH){
		SetPlayerSpell(GetPlayerSpell() + 1);
	}
	yield;
}


task TPlural{
	wait(60 * 0.75);
	ObjSound_Play(bgm[0]); current = 0;
	wait(60 * 1.25);
	BGMName("Album: Hive Jump OST", 228, 0);
	wait(60 * 5);
	BGMName("BGM: Track 14 - Only You Can Prevent Cavern Fires", 120, -1);

	let obj = ObjEnemyBossScene_Create(); 
	let N = []; let S = [];
	
	ascent(i in 0..numScripts){ //0, 1, 2, 3 - 4
		N = N ~ [dir ~ "./scripts/Non" ~ vtos("02d", i+1) ~ ".dnh"]; //1, 2, 3, 4
		S = S ~ [dir ~ "./scripts/Spell" ~ vtos("02d", i+1) ~ ".dnh"];
	}
	S = S ~ [dir ~ "./scripts/Spell" ~ vtos("02d", numScripts+1) ~ ".dnh"];

	ObjEnemyBossScene_Add(obj, 0, dir ~ "./dialogue/Dialogue.dnh");
	ascent(i in 0..numScripts){	
		ObjEnemyBossScene_Add(obj, i, N[i]);
		ObjEnemyBossScene_Add(obj, i, S[i]);
	}
	ObjEnemyBossScene_Add(obj, numScripts, S[numScripts]);

	ObjEnemyBossScene_LoadInThread(obj);
	ObjEnemyBossScene_Regist(obj);
	
	funny(obj); isplayerded();
}

task isplayerded(){
	while(GetPlayerState != STATE_END){yield;}
	wait(60);
	
	CloseScript(GetOwnScriptID());
	CloseStgScene();
}

task funny(obj){
	while(!Obj_IsDeleted(obj)){yield;}
	wait(60*5);
	
	CloseScript(GetOwnScriptID());
	CloseStgScene();
}


task BGMName(text, x, sidepitch){
	let objText = ObjText_Create();
	ObjText_SetText(objText, text);
	ObjText_SetFontType(objText, "Monofonto");
	ObjText_SetFontSize(objText, 16.5);
	ObjText_SetFontBold(objText, false);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetFontCharacterSet(objText, 0x00000001);
	ObjText_SetSidePitch(objText, sidepitch);
	
	ObjText_SetMaxWidth(objText, 1337);
	
	Obj_SetRenderPriority(objText, 1);
	
	ObjRender_SetX(objText, x);
	ObjRender_SetY(objText, GetStgFrameHeight-25);
	ObjRender_SetScaleX(objText, 0);
	
	let y = 40; let i = 0;
	loop(30){
		ObjSound_SetResumeEnable(bgm, true);
		ObjRender_SetScaleX(objText, i); i += 1/29;
		yield;
	}
	
	wait(60*3);
	
	loop(30){
		ObjRender_SetY(objText, ObjRender_GetY(objText)+(y/30));
		yield;
	}
	
	Obj_Delete(objText);
}

task Difficult(num, pos){
	let pic = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(pic, GetCurrentScriptDirectory() ~ "system/FX/diff.png");
	ObjSprite2D_SetSourceRect(pic, 23, 0, 86, 21);
	alternative(num)
		case(0){ObjSprite2D_SetSourceRect(pic, 23, 0, 86, 21);} //Easy
		case(1){ObjSprite2D_SetSourceRect(pic, 10, 24, 100, 45);} //Normal
		case(2){ObjSprite2D_SetSourceRect(pic, 17, 47, 89, 70);} //Hard
		case(3){ObjSprite2D_SetSourceRect(pic, 13, 71, 97, 94);} //Lunatic
		case(4){ObjSprite2D_SetSourceRect(pic, 16, 95, 92, 118);} //Ex
		case(5){ObjSprite2D_SetSourceRect(pic, 0, 120, 109, 143);} //Last Word
	ObjSprite2D_SetDestCenter(pic);
	ObjRender_SetBlendType(pic, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(pic, 1, 1, 0);
	Obj_SetRenderPriority(pic, 1);
	ObjRender_SetPosition(pic, GetScreenWidth()/2, pos[1], 0);
	ObjRender_SetAlpha(pic, 0);

	blink(pic, pos);
}

task blink(obj, pos){
		wait(30);

		let o = 0;
		loop(360/4){
			ObjRender_SetAlpha(obj, sin(o)*255); o += 4*4;
			yield;
		}

		o = 0;
		ObjRender_SetPosition(obj, pos[0], pos[1], 0);
		loop(90/1){
			ObjRender_SetAlpha(obj, sin(o)*255); o += 1;
			yield;
		}
	}