//---------------------------------------------------------------------------------------------
//
//	Chromatic Wing Mod
//	
//	Power variable setting / declare
//
//---------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------
// Declare variable
//---------------------------------------------------------------------------------------------

// Power System Relate
	let UIOE_PBarImage = GetCurrentScriptDirectory~"./img/powerbar.png";
	let UIOE_PNumImage = GetCurrentScriptDirectory~"./img/number.png";
	let UIOE_Pbar;
	let UIOE_Pnum1;
	let UIOE_Pnumdot;
	let UIOE_Pnum2;
	let UIOE_Pnum3;
	let UIOE_num1;
	let UIOE_num2;
	let UIOE_num3;

	let UIOE_PnumsizeX1;
	let UIOE_PnumsizeX2;
	let UIOE_PnumsizeY1;
	let UIOE_PnumsizeY2;


	let UIOE_count = 1;
	let ng_tempbomb = 0;
	SetCommonData("CD_PowerMax", 0);





//---------------------------------------------------------------------------------------------
// Sub function to be call later.
//---------------------------------------------------------------------------------------------

// For update each frame.

	sub SFU_power
	{

	if ( GetCommonDataDefault("CD_Power",0) < 0) { SetCommonData("CD_Power", 0); }
	if ( GetCommonDataDefault("CD_Power",0) > 500) { SetCommonData("CD_Power", 500); }


	UIOE_num1 = floor(GetCommonDataDefault("CD_Power",0)/100);
	UIOE_num2 = floor((GetCommonDataDefault("CD_Power",0)-(UIOE_num1*100))/10);
	UIOE_num3 = GetCommonDataDefault("CD_Power",0)%10;


	// Don't let player gain more than 5 bomb
	if (GetPlayerBomb > 5)	{	 AddBomb(-1);	}

	// Refill all bomb to equal to player power

	ng_tempbomb = floor((GetCommonDataDefault("CD_Power",0) / 100));
	if (ng_tempbomb < GetPlayerBomb)	{	 AddBomb(-1);	}
	else if (ng_tempbomb > GetPlayerBomb)	{	 AddBomb(1);	}



	// Counter for 1 frame at mainloop then, draw or update
	UIOE_count--;
	if (UIOE_count == 0)	
		{
		SFUUI_Pbardraw;
		SFUUI_Pnumdraw1;
		SFUUI_Pnumdrawdot;
		SFUUI_Pnumdraw2;
		SFUUI_Pnumdraw3;
		}
	else if (UIOE_count < 0)	
		{
		FU_Pnumupdate(UIOE_num1, UIOE_Pnum1);
		FU_Pnumupdate(UIOE_num2, UIOE_Pnumdot);
		FU_Pnumupdate(UIOE_num2, UIOE_Pnum2);
		FU_Pnumupdate(UIOE_num3, UIOE_Pnum3);
		}

	// Later, we set damage rate (mainly power system here, herr?)
	// So bore.... why 'SetDamageRate' can't be apply in stage script? FUCK!
	SetCommonData("CD_PowerUse", UIOE_num1);
//	if (ng_spellcard == 0)	{	SetDamageRate(50+(10*UIOE_num1),25+(5*UIOE_num1));	}
//	if (ng_spellcard == 1)	{	SetDamageRate(5+(1*UIOE_num1),2.5+(0.5*UIOE_num1));	}


	if ((GetCommonDataDefault("CD_Power",0) >= 500) && (GetCommonDataDefault("CD_PowerMax",0) == 0))
		{
		SetCommonData("CD_PowerMax", 1);
		DeleteEnemyShotToItem(SHOT);
		SFUUI_Pmaxdraw;
		}

	if ((GetCommonDataDefault("CD_Power",0) < 500) && (GetCommonDataDefault("CD_PowerMax",0) == 1))
		{
		SetCommonData("CD_PowerMax", 0);
		}

	// If power max, auto collect every item. (except fever - config in glo_item.txt)
	if (GetCommonDataDefault("CD_Power",0) >= 500)
		{
		//CollectItems();
		}


	}



//---------------------------------------------------------------------------------------------
// Drawing
//---------------------------------------------------------------------------------------------



	sub SFUUI_Pbardraw
	{
	UIOE_Pbar = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(UIOE_Pbar, UIOE_PBarImage);
	ObjEffect_SetLayer(UIOE_Pbar, 8);
	ObjEffect_SetPrimitiveType(UIOE_Pbar, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(UIOE_Pbar, 4);
	ObjEffect_SetVertexXY(UIOE_Pbar, 0, GetClipMaxX+20, GetClipMinY+235);
	ObjEffect_SetVertexXY(UIOE_Pbar, 1, GetClipMaxX+127, GetClipMinY+235);
	ObjEffect_SetVertexXY(UIOE_Pbar, 2, GetClipMaxX+127, GetClipMinY+256);
	ObjEffect_SetVertexXY(UIOE_Pbar, 3, GetClipMaxX+20, GetClipMinY+256);
	ObjEffect_SetVertexUV(UIOE_Pbar, 0, 0, 0);
	ObjEffect_SetVertexUV(UIOE_Pbar, 1, 107, 0);
	ObjEffect_SetVertexUV(UIOE_Pbar, 2, 107, 21);
	ObjEffect_SetVertexUV(UIOE_Pbar, 3, 0, 21);
	}

	sub SFUUI_Pnumdraw1
	{
	UIOE_Pnum1 = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(UIOE_Pnum1, UIOE_PNumImage);
	ObjEffect_SetLayer(UIOE_Pnum1, 8);
	ObjEffect_SetPrimitiveType(UIOE_Pnum1, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(UIOE_Pnum1, 4);
	ObjEffect_SetVertexXY(UIOE_Pnum1, 0, GetClipMaxX+84, GetClipMinY+239);
	ObjEffect_SetVertexXY(UIOE_Pnum1, 1, GetClipMaxX+93, GetClipMinY+239);
	ObjEffect_SetVertexXY(UIOE_Pnum1, 2, GetClipMaxX+93, GetClipMinY+252);
	ObjEffect_SetVertexXY(UIOE_Pnum1, 3, GetClipMaxX+84, GetClipMinY+252);
	ObjEffect_SetVertexUV(UIOE_Pnum1, 0, 0, 0);
	ObjEffect_SetVertexUV(UIOE_Pnum1, 1, 9, 0);
	ObjEffect_SetVertexUV(UIOE_Pnum1, 2, 9, 13);
	ObjEffect_SetVertexUV(UIOE_Pnum1, 3, 0, 13);
	}

	sub SFUUI_Pnumdrawdot
	{
	UIOE_Pnumdot = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(UIOE_Pnumdot, UIOE_PNumImage);
	ObjEffect_SetLayer(UIOE_Pnumdot, 8);
	ObjEffect_SetPrimitiveType(UIOE_Pnumdot, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(UIOE_Pnumdot, 4);
	ObjEffect_SetVertexXY(UIOE_Pnumdot, 0, GetClipMaxX+94, GetClipMinY+239);
	ObjEffect_SetVertexXY(UIOE_Pnumdot, 1, GetClipMaxX+103, GetClipMinY+239);
	ObjEffect_SetVertexXY(UIOE_Pnumdot, 2, GetClipMaxX+103, GetClipMinY+252);
	ObjEffect_SetVertexXY(UIOE_Pnumdot, 3, GetClipMaxX+94, GetClipMinY+252);
	ObjEffect_SetVertexUV(UIOE_Pnumdot, 0, 90, 0);
	ObjEffect_SetVertexUV(UIOE_Pnumdot, 1, 99, 0);
	ObjEffect_SetVertexUV(UIOE_Pnumdot, 2, 99, 13);
	ObjEffect_SetVertexUV(UIOE_Pnumdot, 3, 90, 13);
	}

	sub SFUUI_Pnumdraw2
	{
	UIOE_Pnum2 = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(UIOE_Pnum2, UIOE_PNumImage);
	ObjEffect_SetLayer(UIOE_Pnum2, 8);
	ObjEffect_SetPrimitiveType(UIOE_Pnum2, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(UIOE_Pnum2, 4);
	ObjEffect_SetVertexXY(UIOE_Pnum2, 0, GetClipMaxX+104, GetClipMinY+239);
	ObjEffect_SetVertexXY(UIOE_Pnum2, 1, GetClipMaxX+113, GetClipMinY+239);
	ObjEffect_SetVertexXY(UIOE_Pnum2, 2, GetClipMaxX+113, GetClipMinY+252);
	ObjEffect_SetVertexXY(UIOE_Pnum2, 3, GetClipMaxX+104, GetClipMinY+252);
	ObjEffect_SetVertexUV(UIOE_Pnum2, 0, 0, 0);
	ObjEffect_SetVertexUV(UIOE_Pnum2, 1, 9, 0);
	ObjEffect_SetVertexUV(UIOE_Pnum2, 2, 9, 13);
	ObjEffect_SetVertexUV(UIOE_Pnum2, 3, 0, 13);
	}

	sub SFUUI_Pnumdraw3
	{
	UIOE_Pnum3 = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(UIOE_Pnum3, UIOE_PNumImage);
	ObjEffect_SetLayer(UIOE_Pnum3, 8);
	ObjEffect_SetPrimitiveType(UIOE_Pnum3, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(UIOE_Pnum3, 4);
	ObjEffect_SetVertexXY(UIOE_Pnum3, 0, GetClipMaxX+114, GetClipMinY+239);
	ObjEffect_SetVertexXY(UIOE_Pnum3, 1, GetClipMaxX+123, GetClipMinY+239);
	ObjEffect_SetVertexXY(UIOE_Pnum3, 2, GetClipMaxX+123, GetClipMinY+252);
	ObjEffect_SetVertexXY(UIOE_Pnum3, 3, GetClipMaxX+114, GetClipMinY+252);
	ObjEffect_SetVertexUV(UIOE_Pnum3, 0, 0, 0);
	ObjEffect_SetVertexUV(UIOE_Pnum3, 1, 9, 0);
	ObjEffect_SetVertexUV(UIOE_Pnum3, 2, 9, 13);
	ObjEffect_SetVertexUV(UIOE_Pnum3, 3, 0, 13);
	}

// Update Power Counter
	function FU_Pnumupdate(row,data)
	{
	alternative(row)
		case(0)	{	UIOE_PnumsizeX1 = 0;	UIOE_PnumsizeX2 = 1;	}
		case(1)	{	UIOE_PnumsizeX1 = 1;	UIOE_PnumsizeX2 = 2;	}
		case(2)	{	UIOE_PnumsizeX1 = 2;	UIOE_PnumsizeX2 = 3;	}
		case(3)	{	UIOE_PnumsizeX1 = 3;	UIOE_PnumsizeX2 = 4;	}
		case(4)	{	UIOE_PnumsizeX1 = 4;	UIOE_PnumsizeX2 = 5;	}
		case(5)	{	UIOE_PnumsizeX1 = 5;	UIOE_PnumsizeX2 = 6;	}
		case(6)	{	UIOE_PnumsizeX1 = 6;	UIOE_PnumsizeX2 = 7;	}
		case(7)	{	UIOE_PnumsizeX1 = 7;	UIOE_PnumsizeX2 = 8;	}
		case(8)	{	UIOE_PnumsizeX1 = 8;	UIOE_PnumsizeX2 = 9;	}
		case(9)	{	UIOE_PnumsizeX1 = 9;	UIOE_PnumsizeX2 = 10;	}

	if (GetCommonDataDefault("CD_Power",0) != 500)
	{	UIOE_PnumsizeY1 = 0;	UIOE_PnumsizeY2 = 13;	}
	else	{	UIOE_PnumsizeY1 = 13;	UIOE_PnumsizeY2 = 26;	}



	if (data == UIOE_Pnum1)
		{
		ObjEffect_SetVertexUV(UIOE_Pnum1, 0, (9*UIOE_PnumsizeX1)+1, UIOE_PnumsizeY1);
		ObjEffect_SetVertexUV(UIOE_Pnum1, 1, 9*UIOE_PnumsizeX2, UIOE_PnumsizeY1);
		ObjEffect_SetVertexUV(UIOE_Pnum1, 2, 9*UIOE_PnumsizeX2, UIOE_PnumsizeY2);
		ObjEffect_SetVertexUV(UIOE_Pnum1, 3, (9*UIOE_PnumsizeX1)+1, UIOE_PnumsizeY2);
		}
	else if (data == UIOE_Pnumdot)
		{
		ObjEffect_SetVertexUV(UIOE_Pnumdot, 0, 90, UIOE_PnumsizeY1);
		ObjEffect_SetVertexUV(UIOE_Pnumdot, 1, 99, UIOE_PnumsizeY1);
		ObjEffect_SetVertexUV(UIOE_Pnumdot, 2, 99, UIOE_PnumsizeY2);
		ObjEffect_SetVertexUV(UIOE_Pnumdot, 3, 90, UIOE_PnumsizeY2);
		}
	else if (data == UIOE_Pnum2)
		{
		ObjEffect_SetVertexUV(UIOE_Pnum2, 0, (9*UIOE_PnumsizeX1)+1, UIOE_PnumsizeY1);
		ObjEffect_SetVertexUV(UIOE_Pnum2, 1, 9*UIOE_PnumsizeX2, UIOE_PnumsizeY1);
		ObjEffect_SetVertexUV(UIOE_Pnum2, 2, 9*UIOE_PnumsizeX2, UIOE_PnumsizeY2);
		ObjEffect_SetVertexUV(UIOE_Pnum2, 3, (9*UIOE_PnumsizeX1)+1, UIOE_PnumsizeY2);
		}
	else if (data == UIOE_Pnum3)
		{
		ObjEffect_SetVertexUV(UIOE_Pnum3, 0, (9*UIOE_PnumsizeX1)+1, UIOE_PnumsizeY1);
		ObjEffect_SetVertexUV(UIOE_Pnum3, 1, 9*UIOE_PnumsizeX2, UIOE_PnumsizeY1);
		ObjEffect_SetVertexUV(UIOE_Pnum3, 2, 9*UIOE_PnumsizeX2, UIOE_PnumsizeY2);
		ObjEffect_SetVertexUV(UIOE_Pnum3, 3, (9*UIOE_PnumsizeX1)+1, UIOE_PnumsizeY2);
		}
	}



	task SFUUI_Pmaxdraw
	{
	let ne_temp1 = 0;
	let ne_temp2 = 0;
	let ne_temp1 = 0;
	let UIOE_Pmax = Obj_Create(OBJ_EFFECT);

	Obj_SetPosition(UIOE_Pmax, GetCenterX, GetCenterY-100);

	ObjEffect_SetTexture(UIOE_Pmax, UIOE_PBarImage);
	ObjEffect_SetLayer(UIOE_Pmax, 8);
	ObjEffect_SetRenderState(UIOE_Pmax,ADD);
	ObjEffect_SetScale(UIOE_Pmax, 1, 1);
	ObjEffect_SetPrimitiveType(UIOE_Pmax , PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(UIOE_Pmax, 4);

	ObjEffect_SetVertexXY(UIOE_Pmax, 0, -43.5, -14.5);
	ObjEffect_SetVertexXY(UIOE_Pmax, 1, +43.5, -14.5);
	ObjEffect_SetVertexXY(UIOE_Pmax, 2, +43.5, +14.5);
	ObjEffect_SetVertexXY(UIOE_Pmax, 3, -43.5, +14.5);
	ObjEffect_SetVertexUV(UIOE_Pmax, 0, 1, 22);
	ObjEffect_SetVertexUV(UIOE_Pmax, 1, 107, 22);
	ObjEffect_SetVertexUV(UIOE_Pmax, 2, 107, 49);
	ObjEffect_SetVertexUV(UIOE_Pmax, 3, 1, 49);

	while (!Obj_BeDeleted(UIOE_Pmax)) 
		{	
		
		if (ne_temp1 < 90)
			{
			ObjEffect_SetScale(UIOE_Pmax, 6-(ne_temp1/18), 3-(ne_temp1/45));
			}
		else
			{
			ObjEffect_SetScale(UIOE_Pmax, 1, 1);
			}

		ObjEffect_SetVertexColor(UIOE_Pmax,0,255-((ne_temp1-60)*2),255,255,255);
		ObjEffect_SetVertexColor(UIOE_Pmax,1,255-((ne_temp1-60)*2),255,255,255);
		ObjEffect_SetVertexColor(UIOE_Pmax,2,255-((ne_temp1-60)*2),255,255,255);
		ObjEffect_SetVertexColor(UIOE_Pmax,3,255-((ne_temp1-60)*2),255,255,255);

      		if (ne_temp1 > 185)
			{
			Obj_Delete(UIOE_Pmax);
			}	
		ne_temp1++;
      		yield;
		}
	}

