let object = -1;
#include"./../Stage/Script/LoadSounds.txt"
let select = GetCurrentScriptDirectory ~ "./../Sound/select2.wav";

@Initialize{
	SaveCommonDataAreaA2("RECORDS",GetCurrentScriptDirectory ~ "./records.dat");
	SaveCommonDataAreaA2("ULTRA",GetCurrentScriptDirectory ~ "./ultra.dat");
	SetAutoDeleteObject(true);
	TBackground();
	TEffect();
	TMenu();

	LoadSound(select);}

@MainLoop{yield;}

@Finalize{}

task TEffect{
	let angle = 0;
	let object = ObjPrim_Create(OBJ_SPRITE_2D);
	let pause = GetCurrentScriptDirectory() ~ "./../Image/ornament.png";
	ObjPrim_SetTexture(object,pause);
	Obj_SetRenderPriorityI(object, 0);
	ObjSprite2D_SetSourceRect(object, 3, 3, 640, 480);
	ObjSprite2D_SetDestCenter(object);
	ObjRender_SetPosition(object,210,180,0);
	ObjRender_SetScaleXYZ(object,0.7,0.7,0);
	ObjRender_SetBlendType(object,BLEND_MULTIPLY);
	let vol = 100;
	loop(100){
		ObjSound_SetVolumeRate(GetAreaCommonData("MUSIC","Song",0), vol);
		vol--;}}

task TBackground{
	task TVertex(var index, var left, var top, var right, var bottom){
		ObjPrim_SetVertexPosition(obj, index + 0, left, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 1, left, bottom, 0);
		ObjPrim_SetVertexPosition(obj, index + 2, right, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 3, right, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 4, left, bottom, 0);
		ObjPrim_SetVertexPosition(obj, index + 5, right, bottom, 0);

		ObjPrim_SetVertexUVT(obj, index + 0, left, top);
		ObjPrim_SetVertexUVT(obj, index + 1, left, bottom);
		ObjPrim_SetVertexUVT(obj, index + 2, right, top);
		ObjPrim_SetVertexUVT(obj, index + 3, right, top);
		ObjPrim_SetVertexUVT(obj, index + 4, left, bottom);
		ObjPrim_SetVertexUVT(obj, index + 5, right, bottom);

		if(left >= 32 && right <= 416 && top >=16 && bottom <= 464){
			let alpha = 255;
			while(alpha >= 128){
				ObjPrim_SetVertexAlpha(obj, index + 0, alpha/3);
				ObjPrim_SetVertexAlpha(obj, index + 1, alpha/3);
				ObjPrim_SetVertexAlpha(obj, index + 2, alpha/3);
				ObjPrim_SetVertexAlpha(obj, index + 3, alpha/3);
				ObjPrim_SetVertexAlpha(obj, index + 4, alpha/3);
				ObjPrim_SetVertexAlpha(obj, index + 5, alpha/3);
				alpha -= 255;
				yield;}}}

	let countH = 20;
	let countV = 30;
	let width = 640/countH;
	let height = 480/countV;
	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);
	ObjPrim_SetVertexCount(obj, countH * countV * 6);
	Obj_SetRenderPriorityI(obj, 0);
	ObjPrim_SetTexture(obj, target);

	ascent(ix in 0.. countH){
		ascent(iy in 0.. countV){
			let index = (ix + iy * countH) * 6;
			let left = ix * width;
			let right = left + width;
			let top = iy * height;
			let bottom = top + height;
			TVertex(index, left, top, right, bottom);}}}

task TMenu{
	InstallFont(GetCurrentScriptDirectory ~ "./../Fonts/Blogger Sans-Light.otf");
	let selectIndex = 0;
	let ypos = 0;
	task TMenuItem(let index, let mx, let my, let text){
		function CreateTextObject(let mx, let my, let text){
			let obj = ObjText_Create();
			ObjText_SetText(obj, text);
			ObjText_SetFontSize(obj, 35);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontType(obj,"Blogger Sans");
			Obj_SetRenderPriorityI(obj, 10);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);
			return obj;}

		let objText = CreateTextObject(mx, my, text);
		let objSelect = CreateTextObject(mx, my, text);
		ObjRender_SetBlendType(objSelect, BLEND_ADD_RGB);
		loop{
			Obj_SetVisible(objSelect, index == selectIndex);
			yield;}}


		function Create_Crystal{
			object = ObjPrim_Create(OBJ_SPRITE_2D);
			let pause = GetCurrentScriptDirectory() ~ "./../Image/ruby.png";
			ObjPrim_SetTexture(object,pause);
			Obj_SetRenderPriorityI(object, 1);
			ObjSprite2D_SetSourceRect(object, 1, 1, 266, 426);
			ObjSprite2D_SetDestCenter(object);
			ObjRender_SetScaleXYZ(object,0.4,0.4,0);
			ObjRender_SetBlendType(object,BLEND_ADD_ARGB);
			Crystal_Manage(object);
			return object;}


		task Crystal_Manage(obj){
			let angle = 0;
			while(true) {
			ObjRender_SetPosition(obj,130,82+ypos,0);
			ObjRender_SetAngleZ(obj,70+10*sin(angle));
			angle+=2;
			yield;}}

	let mx = 70;
	let my = 52;
	let texts = ["Save Replay", "Return to Menu", "Retry"];
	var countMenu = length(texts);
	ascent(var iText in 0 .. countMenu){
		TMenuItem(iText, mx, my, texts[iText]);
		my += 52;}

	Create_Crystal;
	ObjRender_SetPosition(object,mx,my,0);

	while(GetVirtualKeyState(VK_OK) != KEY_FREE){yield;}

	let frameKeyHold = 0;
	loop{
		if(GetVirtualKeyState(VK_OK) == KEY_PULL){
			let listResult = [RESULT_SAVE_REPLAY, RESULT_END, RESULT_RETRY];
			SetScriptResult(listResult[selectIndex]);
			CloseScript(GetOwnScriptID());
			return;}

		if(GetVirtualKeyState(VK_UP) == KEY_PUSH){
			selectIndex--;
			ypos-=52;
			PlaySE(select);}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH){
			selectIndex++;
			ypos+=52;
			PlaySE(select);}
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0))){
				selectIndex--; ypos-=52;
				PlaySE(select);}}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0))){
				selectIndex++; ypos+=52;
				PlaySE(select);}}
		else{frameKeyHold = 0;}

		if(selectIndex < 0) {
			selectIndex = countMenu - 1;
			ypos = 104;}
		else if(selectIndex > 2){
			selectIndex = 0;
			ypos = 0;}
		else{selectIndex %= countMenu;}
		yield;}}