#東方弾幕風[Stage]
#ScriptVersion[3]
#Title["RAINS3のテスト -選択機能-"]
#Text[""]
#Image[""]
#Background["script/default_system/Default_Background_IceMountain.txt"]


#include "./RAINS_Client_Stage.dnh"
#include "./libs_enemy.dnh"
let Csd = GetCurrentScriptDirectory;
let boss = Csd ~ "test.txt";
let count = 0;

let SelectStep = 0;
let p = 0;
let w = 0;
let objT = ID_INVALID;

@Initialize()
{
	StageTask();
	Initialize_RAINS3_Client();
	TKeyCheck();
	DrawRAINS3state();
}

@MainLoop()
{
	yield;
}

@Finalize()
{

}

task StageTask
{
	RAINS3_WaitForPlayerSetup;
	loop{
		Wait(60);
		SetForbidPlayerSpell(true);
		SelectPlayer;
		SetForbidPlayerSpell(false);
		Stage();
		RAINS3_SetPlayer(0);
		RAINS3_ChangeWeapon(0);
	}
}

function Wait(let frame)
{
	loop(frame){yield;}
}

task DrawRAINS3state
{
	let objST = ObjText_Create();
	ObjText_SetFontSize(objST, 16);
	ObjText_SetFontBold(objST, false);
	ObjText_SetFontColorTop(objST, 255, 255, 255);
	ObjText_SetFontColorBottom(objST, 128, 128, 255);
	ObjText_SetFontBorderType(objST, BORDER_FULL);
	ObjText_SetFontBorderColor(objST,0, 0, 255);
	ObjText_SetFontBorderWidth(objST, 2);
	Obj_SetRenderPriorityI(objST, GetCameraFocusPermitPriorityI + 1);
	ObjRender_SetX(objST, 10);
	ObjRender_SetY(objST, 320);
	
	loop{
		let Str = "▼共通データ情報[r]PlayerID　" ~rtos("#00",RAINS3_GetPlayerID)~ "[r]WeaponType　" ~rtos("#00",RAINS3_GetWeaponType);
		if(RAINS3_GetValueEX(objMyship, "RAINS3_PreLoadFlag", 0) != 0){
			Str = Str ~"[r]Loading...";
		}
		if(Obj_IsDeleted(objMyship)){
			Str = Str ~"[r]Waiting for Player Setup...";
		}
		ObjText_SetText(objST,Str);
		yield;
	}
}

function SelectPlayer
{
	objT = ObjText_Create();
	ObjText_SetFontSize(objT, 16);
	ObjText_SetFontBold(objT, false);
	ObjText_SetFontColorTop(objT, 255, 255, 255);
	ObjText_SetFontColorBottom(objT, 128, 128, 255);
	ObjText_SetFontBorderType(objT, BORDER_FULL);
	ObjText_SetFontBorderColor(objT,0, 0, 255);
	ObjText_SetFontBorderWidth(objT, 2);
	Obj_SetRenderPriorityI(objT, GetCameraFocusPermitPriorityI + 1);
	ObjRender_SetX(objT, 10);
	ObjRender_SetY(objT, 300);
	
	
	p = 0;
	w = 0;
	
	SelectStep = 1;
	while(GetVirtualKeyState(VK_OK) != KEY_PUSH){
		ObjText_SetText(objT, "自機選択中（上下キーで変更）：" ~ RAINS_CharName[p % RAINS_MaxID + 1]);
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH && p >= 1){p--;}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH && p < RAINS_MaxID - 1){p++;}
		yield;
	}
	p += 1;
	SelectStep = 2;
	Wait(10);
	while(GetVirtualKeyState(VK_OK) != KEY_PUSH){
		ObjText_SetText(objT, "装備選択中（上下キーで変更）：装備" ~rtos("#00",w + 1));
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH && w > 0){w--;}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH && w < RAINS_WeaponNum[p] - 1){w++;}
		yield;
	}
	SelectStep = 0;
	Wait(10);
	RAINS_SetPreLoad(p);	//画像の先読み指示。RAINS3_SetPlayerよりも先に実行する必要あり。
	RAINS3_SetPlayer(p);
	RAINS3_ChangeWeapon(w);
	Obj_Delete(objT);
}

function Stage
{
	//ステージ進行処理
	//（サンプルのExルーミアをお借りしています）
	let path = "script/ExRumia/ExRumia_Plural.txt";
	let idScript = LoadScriptInThread(path);
	loop(60){yield;}//1秒くらいあれば、コンパイル完了すると思われる。
	StartScript(idScript);

	//敵ボスシーンが終了するまで待機
	count = 0;
	while(!IsCloseScript(idScript))
	{
		if(count % 30 == 0){
			let ran1 = rand(-150,150);
			let ran2 = rand(30, 180);
			RAINS_SetItem(cx + ran1, ran2, 0, 2, 150);
			CreateItemA1(ITEM_POINT_S, cx - ran1, ran2, 100);
		}
		count++;
		yield;
	}
	Wait(180);
	RAINS_WaitForChangePlayer;
}