//---------------------------------------------------------------------------------------------
//
//	Chromatic Wing Mod
//	
//	function manager
//	include this manager before @Initialize
//	run SU_GloLoad in @Initialize
//	run SU_GloRun in @MainLoop
//
//---------------------------------------------------------------------------------------------
//	
//	glo_power	Power relate system
//	glo_score		Fever score system
//	glo_item		Custom item system
//	glo_penalty	Penalty system
//	
//---------------------------------------------------------------------------------------------
//	include file to each file
//	put this in script_enemy_main or else
//---------------------------------------------------------------------------------------------


	#include_function".\function\global_data.txt";
	#include_function".\function\stage_createmon.txt";

	#include_function".\function\glo_power.txt";
	#include_function".\function\glo_score.txt";
	#include_function".\function\glo_penalty.txt";



//---------------------------------------------------------------------------------------------
//	All global variable will declare here, so at least no error come when set it up.
//---------------------------------------------------------------------------------------------

//----- Global
	let ngc_continue = 1;		// Set continue
	let ngc_life = 3;			// Set life
	let ngc_playstage;			// Create play stage var
	SetCommonData("ngc_playstage", 0);	// Set play stage common data
	let ngc_pointextend;		// Set how much point to extend
	let ngc_pointextendcount = 0;	// Set point counter
	let ngc_grazebrakerflag = 0;		// Set graze braker flag

	SetCommonData("ng_feverdecrease", 0);	// Fever score keep decrease?
	SetCommonData("ngc_waitforclear", 0);	// Waiting for stage clear?




	let ng_stageintro = 0;		// Set stage intro counter

	let ng_bg_scroll1 = 0;		// Set bg scrolling var1
	let ng_bg_scroll2 = 0;		// Set bg scrolling var2
	let ng_bg_scroll3 = 0;		// Set bg scrolling var3
	let ng_bg_scroll4 = 0;		// Set bg scrolling var3

	let ng_bg_angle1 = 0;		// Set bg angle var1
	let ng_bg_angle2 = 0;		// Set bg angle var2

	let ng_bg_fade1 = 0;		// Set bg fading var1
	let ng_bg_fade2 = 0;		// Set bg fading var1

	let ng_stagevar1 = 0;		// Temp var in stage
	let ng_stagevar2 = 0;		// Temp var in stage


//---------------------------------------------------------------------------------------------
//	Sub function to load require graphic
//	put this in @Initialize or else
//---------------------------------------------------------------------------------------------


	sub SFU_GloLoad
	{
	LoadGraphic(UIOE_PBarImage);
	LoadGraphic(UIOE_PNumImage);
	LoadGraphic(UIOE_FeverImage);
	LoadGraphic(UIOE_GMImage);
	}


//---------------------------------------------------------------------------------------------
//	Sub function to run all of them
//	put this in @MainLoop or else
//---------------------------------------------------------------------------------------------

	sub SFU_GloRun
	{
	SFU_power;
	SFU_fever;
	SFU_DEAD;
	SFU_BOMB;
	}


//---------------------------------------------------------------------------------------------
//	
//	Basic function use everywhere .... baaaaaaa
//	
//---------------------------------------------------------------------------------------------


//----- Wait for a period
	function Wait(n)
	{
		loop(n) {	yield;	}
	}




//----- Wait until all enemy kill
	function WaitForZeroEnemy()
	{
		while(GetEnemyNum() != 0) {	yield;	}
	}

//----- Wait until stageclear
	function WaitForClear()
	{
		while(GetCommonDataDefault("ngc_waitforclear",0) != 0) {	yield;	}
	}



//----- How to set extend for point item
	function ExtendSystem
	{
		SetNormPoint(ngc_pointextend[ngc_pointextendcount]);
		if (GetPoint >= ngc_pointextend[ngc_pointextendcount])
			{
			ExtendPlayer(1);
			PlaySE(GetCurrentScriptDirectory~"sfx\se_extend.wav");
			ngc_pointextendcount++;
			}
		if (ngc_pointextendcount == 16)	{	SetNormPoint(0);	ngc_pointextendcount = 16;	}
	}

//----- Madness graze help you a lot!
	function GrazeBraker
	{
		if ((GetGraze%250 == 0) && (ngc_grazebrakerflag == 0))
			{
			DeleteEnemyShotToItem(SHOT);
			PlaySE(GetCurrentScriptDirectory~"sfx\se_bonus2.wav");
			ngc_grazebrakerflag = 1;
			}
		if ((GetGraze%250 == 1) && (ngc_grazebrakerflag == 1))
			{
			ngc_grazebrakerflag = 0;
			}
	}


// Convent number to string by NuclearCheese
	function IntString(number)
	{
	let str = ToString(number);
	let str2 = "";
	let i = 0;
		while (str[i] != '.')
		{
		str2 = str2 ~ [str[i]];
		i++;
		}
	return(str2);
	}



//----- Select Mode YEAH!

	function ModeSelect
	{
		Wait(1);
		ForbidBomb(true);
		CreateEventFromScript("Select");
		while(OnEvent){	yield;	}
		ForbidBomb(false);
	}
	
//	function Extra1Select
//	{
//		Wait(1);
//		ForbidBomb(true);
//		CreateEventFromScript("ExtraSelect");
//		while(OnEvent){yield;}
//		ForbidBomb(false);
//		Wait(90);
//		return GetCommonDataDefault("Ex1Flg",false);
//	}



//---------------------------------
// Take from PoSR
// StageEnd

	task EndofStage
	{
	let playercheckok = 0;
	let OE_tempvarcount1 = 0;
	let OE_tempvarcount2 = 0;

	let scoretable1 = [1000000, 3000000, 4000000, 5000000, 7000000, 9000000, 12000000, 15000000];
	let scoretable2 = [50000, 100000, 125000, 150000, 175000, 200000, 300000, 500000];
	let scoretable3 = [10000, 25000, 30000, 35000, 40000, 45000, 50000, 75000];

	ForbidShot(true);
	ForbidBomb(true);
	SetCommonData("ng_feverdecrease", 0);
	SetCommonData("ngc_waitforclear", 1);

	let OE_EndStage = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(OE_EndStage, stageclear[GetCommonDataDefault("ngc_playstage",0)]);
	ObjEffect_SetLayer(OE_EndStage, 7);
	Obj_SetAlpha(OE_EndStage, 0);
	ObjEffect_CreateVertex(OE_EndStage, 4);
	ObjEffect_SetPrimitiveType(OE_EndStage, PRIMITIVE_TRIANGLEFAN);

	ObjEffect_SetVertexUV(OE_EndStage, 0, 0, 0);
	ObjEffect_SetVertexUV(OE_EndStage, 1, 390, 0);
	ObjEffect_SetVertexUV(OE_EndStage, 2, 390, 450);
	ObjEffect_SetVertexUV(OE_EndStage, 3, 0, 450);

	ObjEffect_SetVertexXY(OE_EndStage, 0, GetCenterX-195, GetCenterY-225);
	ObjEffect_SetVertexXY(OE_EndStage, 1, GetCenterX+195, GetCenterY-225);
	ObjEffect_SetVertexXY(OE_EndStage, 2, GetCenterX+195, GetCenterY+225);
	ObjEffect_SetVertexXY(OE_EndStage, 3, GetCenterX-195, GetCenterY+225);

	while (OE_tempvarcount1 < 130)
		{
		SetPlayerX(224);
		SetPlayerY(400);
		// Showing End of Stage
		if ((OE_tempvarcount1 < 50) && (playercheckok == 0))
			{
			ObjEffect_SetVertexColor(OE_EndStage, 0, OE_tempvarcount1*5, 255, 255, 255);	
			ObjEffect_SetVertexColor(OE_EndStage, 1, OE_tempvarcount1*5, 255, 255, 255);
			ObjEffect_SetVertexColor(OE_EndStage, 2, OE_tempvarcount1*5, 255, 255, 255);
			ObjEffect_SetVertexColor(OE_EndStage, 3, OE_tempvarcount1*5, 255, 255, 255);
			}

		// Hold it until player shot
		if ((OE_tempvarcount1 == 80) && (playercheckok == 0))
			{
			ObjEffect_SetVertexColor(OE_EndStage, 0, 255, 255, 255, 255);	
			ObjEffect_SetVertexColor(OE_EndStage, 1, 255, 255, 255, 255);
			ObjEffect_SetVertexColor(OE_EndStage, 2, 255, 255, 255, 255);
			ObjEffect_SetVertexColor(OE_EndStage, 3, 255, 255, 255, 255);
			}

		// shot? Fade out~
		if ((OE_tempvarcount1 > 80) && (playercheckok == 1))
			{
			ObjEffect_SetVertexColor(OE_EndStage, 0, 255-((OE_tempvarcount1-80)*5), 255, 255, 255);	
			ObjEffect_SetVertexColor(OE_EndStage, 1, 255-((OE_tempvarcount1-80)*5), 255, 255, 255);
			ObjEffect_SetVertexColor(OE_EndStage, 2, 255-((OE_tempvarcount1-80)*5), 255, 255, 255);
			ObjEffect_SetVertexColor(OE_EndStage, 3, 255-((OE_tempvarcount1-80)*5), 255, 255, 255);
			}

		// If player shot when stage clear complete show - check ok
		if ((GetKeyState(VK_SHOT) == KEY_PUSH) && (OE_tempvarcount1 == 80) && (playercheckok == 0))
			{
			PlaySE(GetCurrentScriptDirectory~"sfx\se_ok00.wav");
			playercheckok = 1;
			}

		if ((OE_tempvarcount1 < 80) && (playercheckok == 0))	{	OE_tempvarcount1++;	}
		if ((OE_tempvarcount1 >= 80) && (playercheckok == 1))	{	OE_tempvarcount1++;	}
		yield;
		}
	ForbidShot(false);
	ForbidBomb(false);

	AddScore(scoretable1[GetCommonDataDefault("ngc_playstage",0)]);
	AddScore(scoretable2[GetCommonDataDefault("ngc_playstage",0)] * GetPlayerLife);
	AddScore(scoretable3[GetCommonDataDefault("ngc_playstage",0)] * GetPlayerBomb);

	Obj_Delete(OE_EndStage);


	SetCommonData("ng_feverdecrease", 1);
	SetCommonData("ngc_waitforclear", 0);
	}




