//エンドスクリプト

#include"./txt/Save.txt"

@Initialize
{
	if(GetCommonData("GameOverFlag","None")!="None")
	{
		TBackgroundFrame;
		if(GetCommonData("GameOverFlag","None")=="Retry")
		{
		SetScriptResult(RESULT_RETRY);	
		}
		else
		{
		SetScriptResult(RESULT_END);	
		}
	}
	else
	{
	if(GetAreaCommonData("Title","StartFlag",false))
	{
		SetScriptResult(RESULT_RETRY);
	}
	else
	{
	TBackground();
	TMenu();
	}
	}
}

@MainLoop
{
	yield;
}

@Finalize
{
}

task TBackgroundFrame
{
	let imgBer = GetTransitionRenderTargetName();
	//let imgBer = GetCurrentScriptDirectory() ~ "img/system.png";
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetTexture(obj, imgBer);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(obj, 4);
//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriorityI(obj,0);
	ObjRender_SetAngleZ(obj,0);

	ObjPrim_SetVertexPosition(obj, 0, 0, 0, 0);
	ObjPrim_SetVertexPosition(obj, 1, 640,0, 0);
	ObjPrim_SetVertexPosition(obj, 2, 640, 480, 0);
	ObjPrim_SetVertexPosition(obj, 3, 0, 480, 0);

	ObjPrim_SetVertexUVT(obj, 0, 0, 0);
	ObjPrim_SetVertexUVT(obj, 1, 640, 0);
	ObjPrim_SetVertexUVT(obj, 2, 640, 480);
	ObjPrim_SetVertexUVT(obj, 3, 0, 480);

	ObjRender_SetPosition(obj,0,0,0);

	ObjRender_SetAlpha(obj, 255);
}

task TBackground
{
	let imgBer = GetTransitionRenderTargetName();
//	let imgBer = GetCurrentScriptDirectory() ~ "img/system2.png";
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetTexture(obj, imgBer);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(obj, 4);
//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriorityI(obj,0);
	ObjRender_SetAngleZ(obj,0);

	ObjPrim_SetVertexPosition(obj, 0, 0, 0, 0);
	ObjPrim_SetVertexPosition(obj, 1, 640,0, 0);
	ObjPrim_SetVertexPosition(obj, 2, 640, 480, 0);
	ObjPrim_SetVertexPosition(obj, 3, 0, 480, 0);

	ObjPrim_SetVertexUVT(obj, 0, 0, 0);
	ObjPrim_SetVertexUVT(obj, 1, 640, 0);
	ObjPrim_SetVertexUVT(obj, 2, 640, 480);
	ObjPrim_SetVertexUVT(obj, 3, 0, 480);

	ObjRender_SetPosition(obj,0,0,0);

	ObjRender_SetAlpha(obj, 255);
}

task TMenu
{
	loop(15)
	{
		DrawText("NowLoading",300+32, 420+16,20,255,[]);
		yield;
	}
	loop(15)
	{
		DrawText("NowLoading.",300+32, 420+16,20,255,[]);
		yield;
	}
	loop(15)
	{
		DrawText("NowLoading..",300+32, 420+16,20,255,[]);
		yield;
	}
	loop(15)
	{
		DrawText("NowLoading...",300+32, 420+16,20,255,[]);
		yield;
	}
	SaveScriptData;
	
	SetScriptResult(RESULT_RETRY);
}

	task DrawText(let text, let mx, let my,size,alpha,color)
	{
		let obj = ObjText_Create();
		ObjText_SetText(obj, text);
		ObjText_SetFontSize(obj, size);
		ObjText_SetFontBold(obj, true);
		ObjText_SetFontColorTop(obj, 196, 196, 255);
		ObjText_SetFontColorBottom(obj, 128, 128, 255);
		ObjText_SetFontBorderType(obj, BORDER_NONE);
		ObjText_SetFontBorderColor(obj,255, 255, 255);
		ObjText_SetFontBorderWidth(obj, 0);
		Obj_SetRenderPriorityI(obj, 80);
		ObjRender_SetX(obj, mx);
		ObjRender_SetY(obj, my);
		ObjText_SetFontType(obj,"ＭＳ 明朝");

	//	ObjText_SetMaxWidth(obj,200);

		if(length(text)>=24)
		{
			ObjRender_SetY(obj, my-8);
		}

		if(length(color)!=0)
		{
		ObjText_SetFontColorTop(obj, (color[0]+255)/2, (color[1]+255)/2,(color[2]+255)/2);
		ObjText_SetFontColorBottom(obj, color[0], color[1], color[2]);
		}

	//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		ObjRender_SetAlpha(obj,alpha);
		yield;

		Obj_Delete(obj);
	}