#東方弾幕風[Stage]
#Title[RAINSのテスト -リアルタイム装備変更-]
#Text[]
#Image[]
#BGM[]
#Player[FREE]
#BackGround[Default]
#ScriptVersion[2]

script_stage_main
{
#include_function".\RAINS_Client_Stage.dnh"
let Csd = GetCurrentScriptDirectory;
let boss = Csd ~ "test.txt";
let count = 0;

let SelectStep = 0;
let p = 0;
let w = 0;

	@Initialize()
	{
		SetRateScoreSystemEnable(false);
		StageTask();
		Initialize_RAINS_Client;
	}
	
	@MainLoop()
	{
		yield;
	}
	
	@Finalize()
	{
	
	}

	@DrawBottomObject
	{
		DrawText("PlayerID　" ~ ToString(RAINS_GetPlayerID),40,380,16,255);
		DrawText("WeaponType　" ~ ToString(RAINS_GetWeaponType),40,400,16,255);
		DrawText("▼共通データ情報",40,360,16,255);
		if(SelectStep == 1){
			DrawText("自機選択中（上下キーで変更）：" ~ RAINS_CharName[p % RAINS_MaxID + 1],40,300,16,255);
		}
	}

	task StageTask
	{
		loop{
			ForbidBomb(true);
			SelectPlayer;
			ForbidBomb(false);
			Stage();
			RAINS_ChangePlayer(0);
			RAINS_ChangeWeapon(0);
		}
	}
	
	function Wait(let frame)
	{
		loop(frame){yield;}
	}
	
	function WaitFor_n_Enemy(let n)
	{
		while(GetEnemyNum() > n){yield;}
	}

	function SelectPlayer
	{
		p = 0;
		w = 0;
		
		Wait(60);
		SelectStep = 1;
		while(GetKeyState(VK_SHOT) != KEY_PUSH){
			if(GetKeyState(VK_UP) == KEY_PUSH && p >= 1){p--;}
			else if(GetKeyState(VK_DOWN) == KEY_PUSH && p < RAINS_MaxID - 1){p++;}
			yield;
		}
		p = (p % 8) + 1;
		SelectStep = 0;
		Wait(10);
		RAINS_ChangePlayer(p);
		RAINS_ChangeWeapon(w);
	}
	
	function SwitchWeapon{
		let wt = RAINS_GetWeaponType;
		wt++;
		if(wt > RAINS_WeaponNum[RAINS_GetPlayerID] - 1){
			wt = 0;
		}
		RAINS_ChangeWeapon(wt);
	}
	
	task ChangeTask{
		w = 300;
		while(IsBossExisting){
			w--;
			if(!OnBomb && !OnPlayerMissed && GetPlayerLife > 0){
				if(w <= 0){
					SwitchWeapon;
					w = 300;
				}
			}
			yield;
		}
	}

	function Stage
	{
		//ステージ進行処理
		Wait(60);
		CreateEnemyBossFromFile(boss, 0, 0, 0, 0, false);
		ChangeTask;
		WaitFor_n_Enemy(0);
		Wait(180);
		RAINS_WaitForChangePlayer;
	}
}