#TouhouDanmakufu[Plural]
#ScriptVersion[3]
#System["./default_system/Default_System.txt"]
#Player["./player/Reimu/Reimu.txt"]
#Title["Treasure Hunters"]
#Text["By Foxigami: Replays are not allowed. sorry for a crap script i made the first part of the script in two weeks, then procrastinated, then finally finished it within 3 days... I am using some code from ajs sorry"]
#Background["./Background.txt"]

#include".\Resources.txt"

let music1 = ObjSound_Create;
let music1fadeout = false;
ObjSound_Load(music1,CSD ~ "./bgm/boss1.mp3");
ObjSound_SetRestartEnable(music1,true);
ObjSound_SetVolumeRate(music1,100);
ObjSound_SetLoopEnable(music1,true);



@Initialize{
	TPlural;
	SetPlayerLife(9999); //THE PUREST NUMBER
	SetPlayerSpell(2);
	SetPauseScriptPath(CSD ~ "./default_system/Default_Pause.txt");
	SetEndSceneScriptPath(CSD ~ "./default_system/Default_EndScene.txt");
	SetReplaySaveSceneScriptPath(CSD ~ "./default_system/Default_ReplaySaveScene.txt");
	SetStgFrame(0,0,GetScreenWidth,GetScreenHeight,20,80);
	SetPlayerClip(10,10,GetScreenWidth-10,GetScreenHeight-10);	
	ObjMove_SetPosition(GetPlayerObjectID,GetStgFrameWidth/2,GetScreenHeight+30);
	LifeBar;
	InstallFont(CSD ~ "./default_system/8bit.ttf");
	InstallFont(CSD ~ "./default_system/DTM-Mono.otf");
	InstallFont(CSD ~ "./default_system/DTM-Sans.otf");
	InstallFont(CSD ~ "./default_system/ARESSENCE.ttf");
	SetPlayerDownStateFrame(0);
	SetPlayerRebirthFrame(0);
	SetPlayerRebirthLossFrame(0);
	
	//SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);	
	
	SetCommonData("ShowHUD",false);
	SetCommonData("LifebarsRemaining",1);
	SetCommonData("SpellsRemaining", 6);
	SetCommonData("ChoosingDifficulty",1);
	SetCommonData("spell",false);
	SetCommonData("music", music1);
	ObjSound_Play(music1);
	Music1;
}

@Event{
	alternative(GetEventType)
	case(EV_USER + 1){
		ObjSound_Play(music1);
		ObjSound_SetVolumeRate(music1,100);
	}	
	
	case(EV_USER + 2){
		music1fadeout = true;
	}	
}

@MainLoop{
	yield;
}

task Music1{
	while(!music1fadeout){yield;}
	
	let vol = 100;
	
	loop(200){
		vol-=0.5;
		ObjSound_SetVolumeRate(music1,vol);
	yield;}
	ObjSound_SetVolumeRate(music1,0);
	


}



task TPlural
{
	let CSD=GetCurrentScriptDirectory();

	wait(20);
	
	//This task will make you invincible so long as you're holding down the SPACE bar. Use it for testing purposes, or to be a damn filthy cheater~ <3
	PlaytestInvincibility;
	
	
	let obj=ObjEnemyBossScene_Create();
	
	ObjEnemyBossScene_Add(obj,0,CSD~"difficulty.txt");
	ObjEnemyBossScene_Add(obj,0,CSD~"n1.txt");
	ObjEnemyBossScene_Add(obj,0,CSD~"s1.txt");
	ObjEnemyBossScene_Add(obj,0,CSD~"n2.txt");
	ObjEnemyBossScene_Add(obj,0,CSD~"s2.txt");
	ObjEnemyBossScene_Add(obj,0,CSD~"s3.txt");
	ObjEnemyBossScene_Add(obj,0,CSD~"n3.txt");
	ObjEnemyBossScene_Add(obj,0,CSD~"n4.txt");
	ObjEnemyBossScene_LoadInThread(obj);
	ObjEnemyBossScene_Regist(obj);
	while(!Obj_IsDeleted(obj))
	{
		yield;
	}
	CloseScript(GetOwnScriptID());
}

task LifeBar{
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj1, 90);
	ObjPrim_SetTexture(obj1, GetCurrentScriptDirectory~"./img/healthbar1.png");
	ObjRender_SetScaleXYZ(obj1, 1, 1, 1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 474, 16);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, GetStgFrameWidth/2, 25, 0);
	
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj2, 91);
	ObjPrim_SetTexture(obj2, GetCurrentScriptDirectory~"./img/healthbar2.png");
	ObjRender_SetScaleXYZ(obj2, 1, 1, 1);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 471, 13);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetPosition(obj2, 85, 18, 0);
	/*
	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj3, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj3, 92);
	ObjPrim_SetTexture(obj3, GetCurrentScriptDirectory~"./img/lifebar1_2.png");
	ObjRender_SetScaleXYZ(obj3, 1, 1, 1);
	ObjSprite2D_SetSourceRect(obj3, 0, 0, 471, 13);
	ObjSprite2D_SetDestCenter(obj3);
	ObjRender_SetPosition(obj3, GetStgFrameWidth/2, 10, 0);
	*/
	
	let SPArray = [];
	let maxSize = GetCommonData("LifebarsRemaining",2);
	let i = 0;
	
	loop(maxSize){
		let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetBlendType(obj3, BLEND_ALPHA);
		Obj_SetRenderPriorityI(obj3, 90);
		ObjPrim_SetTexture(obj3, GetCurrentScriptDirectory~"./img/SCsymbol.png");
		ObjRender_SetScaleXYZ(obj3, 1, 1, 1);
		ObjSprite2D_SetSourceRect(obj3, 0, 0, 16, 16);
		ObjSprite2D_SetDestCenter(obj3);
		ObjRender_SetPosition(obj3, 530-i, 37, 0);
		i+= 20;
		SPArray = SPArray ~ [obj3];
	}
	/*
	let obj4 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj4, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj4, 89);
	ObjPrim_SetTexture(obj4, GetCurrentScriptDirectory~"./img/healthbg.png");
	ObjRender_SetAlpha(obj4, 20);
	ObjRender_SetScaleXYZ(obj4, 1, 1, 1);
	ObjSprite2D_SetSourceRect(obj4, 0, 0, 471, 26);
	ObjSprite2D_SetDestCenter(obj4);
	ObjRender_SetPosition(obj4, 85*4-20, 26, 0);
	*/
	loop{
	ObjSprite2D_SetSourceRect(obj2,GetCommonData("BossHealth",471),0 , 471, 13);
	ObjSprite2D_SetDestRect(obj2,GetCommonData("BossHealth",471), 0, 471, 13);
	
	if(GetCommonData("SpellsRemaining", 2) < maxSize){
		Obj_Delete(SPArray[maxSize-1]);
		SPArray = erase(SPArray, maxSize-1);
		maxSize--;
	}
	
	/*
	if(GetCommonData("SurvivalCard",false)==true){ObjRender_SetAlpha(obj3,255);}
	else{ObjRender_SetAlpha(obj3,0);}
	
	*/
	yield;
	}
}