
// Patcheの初期化
// @Initializeで一度だけ呼び出す
// 引数：PatcheのパワーアップシステムをONにするか
//		 低速時のエフェクトを描写するか
//		 EXステージか否か
function initPatche( let isPowerup, let isDrawSlowEffect, let isExStage ) {
	let FLAN_CDNS		= "__FLAN_FRAMEWORK";
	let FLAN_CD_PLAYER_POWER	= "PLAYER_POWER";
	let FLAN_CD_PLAYER_POWER_ENABLE	= "PLAYER_POWER_ENABLE";
	if ( IsCommonDataAreaExists( "MZC_CDNS_PLAYER" ) == false ) {
		CreateCommonDataArea( "MZC_CDNS_PLAYER" );
	}
	if ( IsCommonDataAreaExists( FLAN_CDNS ) == false ) {
		CreateCommonDataArea( FLAN_CDNS );
	}
	SetCommonDataEx( FLAN_CDNS, FLAN_CD_PLAYER_POWER_ENABLE, isPowerup );
	SetCommonDataEx( "MZC_CDNS_PLAYER", "MZC_CD_PLAYERSLOWEFFECT_ONSTAGE", !isDrawSlowEffect );
	SetCommonDataEx( "MZC_CDNS_PLAYER", "MZC_CD_PLAYER_ISEXMODE", isExStage );
}

//プレイヤーインデックスを設定します
function setPlayerIndex( let playerIndex ) {
	if ( IsCommonDataAreaExists( "MZC_CDNS_PLAYER" ) == false ) {
		CreateCommonDataArea( "MZC_CDNS_PLAYER" );
	}
	SetCommonDataEx( "MZC_CDNS_PLAYER", "MZC_CD_PLAYERINDEX", playerIndex );
}

// 簡易パワーアップシステム起動メソッド
// 引数：Patcheのプレイヤーインデックス
//		 パワー初期値
function callSimplePowerupSystem( let playerIndex, let initPower ) {
	let m_nOldPoint = 0;
	let FLAN_CDNS	= "__FLAN_FRAMEWORK";
	let FLAN_CD_PLAYER_POWER		= "PLAYER_POWER";
	let FLAN_CD_PLAYER_POWER_ENABLE	= "PLAYER_POWER_ENABLE";

	if ( IsCommonDataAreaExists( "MZC_CDNS_PLAYER" ) == false ) {
		CreateCommonDataArea( "MZC_CDNS_PLAYER" );
	}
	if ( IsCommonDataAreaExists( FLAN_CDNS ) == false ) {
		CreateCommonDataArea( FLAN_CDNS );
	}
	SetCommonDataEx( "MZC_CDNS_PLAYER", "MZC_CD_PLAYERINDEX", playerIndex );
	initPower = [ initPower, 128 ][ initPower > 128 ];
	if ( initPower >= 0 ) {
		SetCommonDataEx( FLAN_CDNS, FLAN_CD_PLAYER_POWER_ENABLE, true );
		SetCommonDataEx( FLAN_CDNS, FLAN_CD_PLAYER_POWER, initPower );
		AddPoint( initPower );
		m_nOldPoint = initPower;
		simplePowerupSystemTask();
	}

	// 簡易パワーアップシステム本体
	task simplePowerupSystemTask() {
		let FLAN_CDNS	= "__FLAN_FRAMEWORK";
		let FLAN_CD_PLAYER_POWER		= "PLAYER_POWER";
		let FLAN_CD_PLAYER_POWER_ENABLE	= "PLAYER_POWER_ENABLE";

		loop {
			if ( OnPlayerMissed() ) {
				let power = GetCommonDataDefaultEx( FLAN_CDNS, FLAN_CD_PLAYER_POWER, 0 );
				if ( power < GetPoint() ) {
					AddPoint( power - GetPoint() );
				}
			}
			else {
				if ( GetPoint() > 128 ) {
					AddPoint( 128 - GetPoint() );
				}
				else if ( GetPoint() < 0 ) {
					AddPoint( -GetPoint() );
				}
				else {
					let inc = GetPoint() - m_nOldPoint;
					let power = GetCommonDataDefaultEx( FLAN_CDNS, FLAN_CD_PLAYER_POWER, 0 );
					if ( power + inc > 128 ) {
						power = 128;
						inc = 0;
					}
					else if ( power + inc < 0 ) {
						power = 0;
						inc = 0;
					}
					SetCommonDataEx( FLAN_CDNS, FLAN_CD_PLAYER_POWER, power + inc );
				}
			}
			m_nOldPoint = GetPoint();
			yield;
		}
	}
}
