//////////////////////////////////////////////////////////////////////
//	プレイヤースクリプト
//////////////////////////////////////////////////////////////////////

let RUMIA_0_objShot = NULL;//誘導弾

//敵を追尾してダメージを与える低速移動時の誘導弾
task slowShot() {
	let myPlayerIndex = 0;
	//1個しか存在しない
	if ( Obj_BeDeleted( RUMIA_0_objShot ) == false ) {
		return;
	}
	RUMIA_0_objShot = Obj_Create( OBJ_SHOT );
	Obj_SetX( RUMIA_0_objShot, GetPlayerX() );				//x座標設定
	Obj_SetY( RUMIA_0_objShot, GetPlayerY() );				//y座標設定
	Obj_SetSpeed( RUMIA_0_objShot, 5 );						//速度設定
	ObjShot_SetGraphic( RUMIA_0_objShot, 2 );				//画像設定
	ObjShot_SetDamage( RUMIA_0_objShot, 0 );				//ダメージを設定
	ObjShot_SetPenetration( RUMIA_0_objShot, 1000000 );		//貫通力は適当に大きく

	//let dmgAry = [ 1.0, 1.0, 1.0, 1.2, 1.4, 1.6, 1.8, 1.8, 2.0 ];
	let dmg = 2.0;
	//削除されるまでループを実行
	while ( Obj_BeDeleted(RUMIA_0_objShot) == false ) {
		if ( m_nPlayerIndex != myPlayerIndex ) {
			Obj_Delete( RUMIA_0_objShot );
			return;
		}
		if ( OnEvent() == true ) {
			Obj_Delete( RUMIA_0_objShot );
			return;
		}
		//ダメージ増加はいらない？
		//dmg = updatePower( dmgAry );
		
		ObjShot_SetDamage( RUMIA_0_objShot, dmg );
		//ホーミング
		let toAngle = atan2( GetEnemyY() - Obj_GetY( RUMIA_0_objShot ),
							 GetEnemyX() - Obj_GetX( RUMIA_0_objShot ) );
		Obj_SetAngle( RUMIA_0_objShot, toAngle );
		yield;
	}
}


//スクリプト初期化
function onInitialize00() {
}

//メインタスク
task MainTask00() {
	let myPlayerIndex = 0;
	TNormalShot();

	task TNormalShot() {
		let limit = 30;
		let shotPowerMax = 1.7;
		//let shotPowerMaxAry = [ 1.2, 1.2, 1.3, 1.3, 1.4, 1.4, 1.5, 1.6, 1.7 ];
		let w = 5;
		let wAry = [ 5, 5, 5, 5, 5, 5, 5, 5, 5 ];
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) {
				return;
			}
			while ( OnEvent() == true ) {
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			w = updatePower( wAry );
			//shotPowerMax = updatePower( shotPowerMaxAry );

			if( m_bIsSlowMove ) {	//低速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {

					if ( m_nPower >= 8 ) {
						slowShot();
					}
					if ( m_nPower >= 128 ) {
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 270, shotPowerMax - 0.2, 3, 1 );
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 265, shotPowerMax - 0.4, 2, 1 );
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 275, shotPowerMax - 0.4, 2, 1 );
					}
					else if ( m_nPower >= 32 ) {
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 268, shotPowerMax - 0.3, 2, 1 );
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 272, shotPowerMax - 0.3, 2, 1 );
					}
					else {
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 270, shotPowerMax - 0.3, 2, 1 );
					}
				}
			}
			else {	//高速移動時の自弾発射
				Obj_Delete( RUMIA_0_objShot );//高速移動時は低速移動時の特殊弾を削除
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {

					if ( m_nPower >= 32 ) {
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 270, shotPowerMax, 3, 1 );
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 275, shotPowerMax - 0.2, 2, 1 );
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 265, shotPowerMax - 0.2, 2, 1 );
					}
					else if ( m_nPower >= 8 ){
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 272, shotPowerMax - 0.2, 2, 1 );
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 268, shotPowerMax - 0.2, 2, 1 );
					}
					else {
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 270, shotPowerMax - 0.2, 2, 1 );
					}

					if ( m_nPower >= 128 ) {
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 255, shotPowerMax - 0.4, 1, 1 );
						CreatePlayerShot01( GetPlayerX(), GetPlayerY(),
											15, 285, shotPowerMax - 0.4, 1, 1 );
					}
				}
			}
			loop ( w ) { yield; }
		}
	}

}

//平常時ループ
function onMainLoop00() {
}

//ミス時ループ
function onMissedLoop00() {
}

//スペカ宣言時
function onSpellCard00() {
	let imgRumiaCutIn = players ~ "Rumia\img\cutin_rumia.png";
	//カットイン
	CutIn( KOUMA,"闇符「ディマーケイション」", imgRumiaCutIn );
	if ( m_bIsSlowMove == true ) {
		//低速移動時
		UseSpellCard( "Demarcation", true );
	}
	else {
		//高速移動時
		UseSpellCard( "Demarcation", false );
	}
}

//描画ループ
function onDrawLoop00() {
	let imgRumia = players ~ "Rumia\img\player_rumia.png";
	SetColor( 255, 255, 255 );
	SetRenderState( ALPHA );
	SetAlpha( 255 );
	SetTexture( imgRumia );
	if ( m_bIsMoveLeft == true ) {
		SetGraphicRect( 0, 96, 48, 144 );
	}
	else if( m_bIsMoveRight == true ) {
		SetGraphicRect( 0, 48, 48, 96 );
	}
	else if ( m_bIsMoveStraight == true ) {
		SetGraphicRect( 0, 0, 48, 48 );
	}
	DrawGraphic( GetPlayerX(), GetPlayerY() );
}

//スクリプト終了
function onFinalize00() {
}
