#include "StaticData.h"

namespace MotionTest {
using namespace HPL;
using namespace E3D;
using namespace General;
using namespace MotionTestConsts;
using namespace BasicStaticDataConsts;



/** ミサイル当り判定＆移動
  *
  */
/*int StaticData::moveMissiles() {
	//ミサイル
	for( std::list<E3DObject*>::iterator it = this->m_lstMissileList.begin();
		it != this->m_lstMissileList.end();
		it ++ )
	{
		E3DObject* obj = *it;
		if( obj->get_Valid() == FALSE ) {
			continue;
		}

		// 移動
		//obj->m_Move.m_vPosition += obj->m_Move.m_vVelocity;
		float SPD = 150.f;
		E3DPosForward( obj->m_nSigID, SPD );

		//int frame ;

		//E3DSetNewPose( obj->m_nSigID, &frame) ;


		if( obj->m_Game.m_Counter->step() == General::CounterStateType::ALL_LOOP_END ) {
			// 死亡
			// 消える
			obj->set_Valid( FALSE) ;
			E3DSetValidFlag( obj->m_nSigID, -1, FALSE );
		}else {
			// チェックイン
			int status;
			//E3DLookAtQ( obj->m_Move.m_nQuoatanion, obj->m_Move.m_vVelocity, D3DXVECTOR3(0,0,1), 0, 1 );
			if( E3DChkInView( this->get_SwapChainID(), obj->m_nSigID, &status ) ){
				_ASSERT( 0 );
				return 1;
			}
		}
	}
	return 0;
}*/

int StaticData::movePlayer() {
	int ret = 0;

	int eventno = -1;

	//////////////////////////////
	// プレイヤーの移動等
	D3DXVECTOR3 vCameraPosition;
	E3DGetCameraPos( & vCameraPosition );

	D3DXVECTOR3 vPlayerPosition;
	E3DGetPos( this->get_Player()->m_nSigID, &vPlayerPosition );

	// 
	D3DXVECTOR3 vDiff = vPlayerPosition - vCameraPosition;

	// 方向固定？
	BOOL bSTRAIF = FALSE;

	if( this->get_CameraMode() == CameraMode::SHOT_BEHIND_MODE ) 
	{
		// 方向固定
		bSTRAIF = TRUE;

		// 射撃モードはプレイヤーからサブサテライト（照準）への方向を基本とする

		D3DXVECTOR3 vSatellitePosition ;

		this->get_Camera()->get_SatellitePosition( vPlayerPosition,
			&vSatellitePosition );
		vDiff = vSatellitePosition - vPlayerPosition;

	} else if( this->get_CameraMode() == CameraMode::SNIPER_MODE ) {
		// 方向固定
		bSTRAIF = TRUE;

		// すないぽんと射撃モードはプレイヤーから目標への方向を基本とする

		D3DXVECTOR3 vSatellitePosition ;

		this->get_Camera()->get_SatellitePosition( vPlayerPosition,
			&vSatellitePosition );
		vDiff = vSatellitePosition - vPlayerPosition;
	}

	// Y軸方向は水平
	vDiff.y = 0;

	// 最終的な方向
	D3DXVECTOR3 vFinalDirection(0,0,0); 

	// 移動あり？
	BOOL bToMove = FALSE;


	// 現在のフレームNoを取得
	int motid;
	int frameno;
	ret = E3DGetMotionFrameNoML( this->get_Player()->m_nSigID, this->get_Player()->m_nUpperBoneNumber, &motid, &frameno );

	// 吹っ飛び中は操作できない
	if( this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN &&
		(this->get_Player()->m_nEventNumber == Motion::Evigen::DAMAGE_A ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::DAMAGE_B ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::DAMAGE_CRASH_UP ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::DAMAGE_BOUND ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::DEATH ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::SWALLOW ) )
	{
		// 何も出来ない

	} else if(this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN &&
		this->get_Player()->m_nEventNumber == Motion::Evigen::RETURN &&
		frameno < 70)
	{
		// 何も出来ない


	} else if( this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN &&
		this->get_Player()->m_nEventNumber == Motion::Evigen::DOWN )
	{
		// 移動キーで復帰
		if( this->get_CurrentKeyBuffer( 'W' ) ||
			this->get_CurrentKeyBuffer( 'A' ) ||
			this->get_CurrentKeyBuffer( 'S' ) ||
			this->get_CurrentKeyBuffer( 'D' )
			)
		{
			if( this->get_Player()->m_Game.m_fHP > 0 ) {
				// 生きてる場合のみ

				this->get_Player()->m_nEventNumber = Motion::Evigen::RETURN;
			}
		}
	} else {

		// キャラクター移動
		if( this->get_CurrentKeyBuffer( 'W' ) != 0 ) {
			bToMove = TRUE;

			// そのままの方向へ向かせて移動させる
			D3DXVECTOR3 vDirection = vDiff;

			vFinalDirection += vDiff;

		}
		if( this->get_CurrentKeyBuffer( 'S' ) != 0 ) {
			bToMove = TRUE;
			
			// 逆方向
			D3DXVECTOR3 vDirection = vDiff * -1;

			vFinalDirection += vDirection;

		}
		if( this->get_CurrentKeyBuffer( 'D' ) != 0 ) {
			bToMove = TRUE;

			// 左へ90度回転させた方向へ向かせる＆移動させる
			D3DXMATRIX matrix;									//マトリクス用意
			D3DXMatrixRotationY( &matrix, D3DXToRadian( 90) );

			D3DXVECTOR3 vDirection;
			D3DXVec3TransformCoord( &vDirection, &vDiff, &matrix );

			vFinalDirection += vDirection;

		}
		if( this->get_CurrentKeyBuffer( 'A' ) != 0 ) {
			bToMove = TRUE;

			// 右へ90度回転させた方向へ向かせる＆移動させる
			D3DXMATRIX matrix;									//マトリクス用意
			D3DXMatrixRotationY( &matrix, D3DXToRadian( -90) );

			D3DXVECTOR3 vDirection;
			D3DXVec3TransformCoord( &vDirection, &vDiff, &matrix );

			vFinalDirection += vDirection;

		}

		//this->get_Player()->m_nEventNumber = 0;
		
		if(bToMove == TRUE) {

			// 指定した方向をクォータニオンにセットする
			if( bSTRAIF ) {
				// 射撃モードなど、方向が前方固定
				this->face( -1 * vDiff, PLAYER_ROTATE_DIVISION, this->get_Player()->m_nSigID,
					this->get_Player()->m_Move.m_nQuoatanion);
			} else {
				// 前後左右にプレイヤーも向かせる
				this->face( -1 * vFinalDirection, PLAYER_ROTATE_DIVISION, this->get_Player()->m_nSigID,
					this->get_Player()->m_Move.m_nQuoatanion);
			}

			float fSpeed = PLAYER_WALK_SPEED;


			if( !this->get_CurrentKeyBuffer(VK_SHIFT )  &&
				this->is_Runnable() == TRUE)
			{
				if( this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN ) {
					eventno = Motion::Evigen::RUN;
				} else if( this->m_nPlayerType == PlayerType::TANYA ) {
					eventno = Motion::Tanya::RUN;
				}
				fSpeed = PLAYER_RUN_SPEED;
			}else {
				// イベント(歩き)
				if( this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN ) {
					eventno = Motion::Evigen::WALK;
				} else if( this->m_nPlayerType == PlayerType::TANYA ) {
					eventno = Motion::Tanya::WALK;
				}
			}

			if( (
					(this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN && 
					(this->get_Player()->m_nEventNumber == Motion::Evigen::JUMP_DOWN ||
					this->get_Player()->m_nUnderEventNumber == Motion::Evigen::JUMP_DOWN) )
				||
					((this->get_Player()->m_Game.m_nType == PlayerType::TANYA &&
					(this->get_Player()->m_nEventNumber == Motion::Tanya::JUMP_DOWN)) )
				)
				&& 
				this->get_Player()->m_Move.get_Trigger( TriggerType::ON_THE_GROUND) == TRUE)
			{
				// 前の段階がジャンプ降下中

				if( this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN ) {
					eventno = Motion::Evigen::WALK;
				} else if( this->m_nPlayerType == PlayerType::TANYA ) {
					eventno = Motion::Tanya::WALK;
				}
			}


			// 指定した方向へ移動させる
			D3DXVECTOR3 vVelocity = this->move( 
				vFinalDirection, fSpeed , 
				this->get_Player()->m_Move.m_vPosition,
				&this->get_Player()->m_Move.m_vPosition);
			E3DSetPos( this->get_Player()->m_nSigID, this->get_Player()->m_Move.m_vPosition );
			this->get_Player()->m_Move.m_vVelocity.x = vVelocity.x;
			this->get_Player()->m_Move.m_vVelocity.z = vVelocity.z;


			// 位置設定
			ret = E3DGetPos( this->get_Player()->m_nSigID, &this->get_Player()->m_Move.m_vPosition);
			if(ret ) {
				_ASSERT(0);
				return ret;
			}

		
		}

		if( this->get_Player()->m_Move.get_Trigger( TriggerType::ON_THE_GROUND ) ) {
			// 地面に居る
			if( this->get_CurrentKeyBuffer(VK_SPACE ) && this->get_PreviousKeyBuffer( VK_SPACE ) == 0 ) {
				// 地面にいるときにジャンプボタン

				// 初速度
				this->get_Player()->m_Move.m_vVelocity.y = PLAYER_JUMP_ACCEL_Y;

				// くうちゅう に いる
				this->get_Player()->m_Move.set_Trigger( TriggerType::ON_THE_GROUND, FALSE );

				//this->get_Player()->set_SkipCheckGround(TRUE );

				// 移動させる
				this->get_Player()->m_Move.m_vPosition += this->get_Player()->m_Move.m_vVelocity;
				E3DSetPos( this->get_Player()->m_nSigID, this->get_Player()->m_Move.m_vPosition );
			}
		} else {
			// 空中に居る
			if( this->get_Player()->m_Move.m_vVelocity.y > 0.f) {
				// UP
				if( this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN ) {
					eventno = Motion::Evigen::JUMP_UP;
				} else if( this->get_Player()->m_Game.m_nType == PlayerType::TANYA ) {
					eventno = Motion::Tanya::JUMP_UP;
				}
			} else {//if( this->get_Player()->m_Move.m_vVelocity.y < 0.f ){
				// DOWN
				if( this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN ) {
					eventno = Motion::Evigen::JUMP_DOWN;
				} else if( this->get_Player()->m_Game.m_nType == PlayerType::TANYA ) {
					eventno = Motion::Tanya::JUMP_DOWN;
				}
			}
			if(this->get_Player()->m_Move.m_bJumpAccelable ){
				// ジャンプ可能状態
				if( this->get_CurrentKeyBuffer( VK_SPACE ) == 0) {
					// ボタンを離した
					this->get_Player()->m_Move.m_bJumpAccelable = ( FALSE );
				}else if( this->get_Player()->m_Move.m_vVelocity.y > PLAYER_JUMP_SPEED_Y ) {
					// 最高速度に達した
					this->get_Player()->m_Move.m_bJumpAccelable = ( FALSE );
				}else {
					// ジャンプ加速
					this->get_Player()->m_Move.m_vVelocity.y += PLAYER_JUMP_ACCEL_Y;
					// 移動させる
					this->get_Player()->m_Move.m_vPosition += this->get_Player()->m_Move.m_vVelocity;
					E3DSetPos( this->get_Player()->m_nSigID, this->get_Player()->m_Move.m_vPosition );
				}
			}
		}
		if( this->get_CurrentKeyBuffer( VK_CONTROL ) ) {
			// しゃがみ
			if( this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN ) {
				eventno = Motion::Evigen::CROUCH;
			} else if( this->get_Player()->m_Game.m_nType == PlayerType::TANYA ) {
				eventno = Motion::Tanya::CROUCH;
			}
		}


		if( this->is_MultiLayerMotion())
		{
			// 別々モーションである
			// 下半身にセット
			if( eventno == -1 ) {
				eventno = 0;
			}
			
			this->get_Player()->m_nUnderEventNumber = eventno;
		} else {
			// 全身にセット
			this->get_Player()->m_nUnderEventNumber = -1;

			if( eventno == -1 ) {
				eventno = 0;
			}

			this->get_Player()->m_nEventNumber = eventno;
			
		}
	}


	//
	//////////////////////////////	プレイヤーの移動等 end


	return 0;
}
int StaticData::shotPlayer() {
	// 吹っ飛び中は操作できない
	if( this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN &&
		(this->get_Player()->m_nEventNumber == Motion::Evigen::DAMAGE_A ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::DAMAGE_B ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::DAMAGE_CRASH_UP ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::DAMAGE_BOUND ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::DEATH ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::RETURN ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::DOWN ))
	{
		// 何も出来ない
		return 0;

	}

	DIMOUSESTATE2* mouse = this->get_DIMouse()->get_CurrentState();
	DIMOUSESTATE2* mousePrevious = this->get_DIMouse()->get_PreviousState();
	if( this->m_SceneManager.get_CurrentSceneID() == SceneType::GAME  &&
		this->m_ScenarioItem != NULL )
	{
		// イベント表示中である
		if( mouse->rgbButtons[0] && mousePrevious->rgbButtons[0] == FALSE ||
			this->m_ScenarioItem->m_nType == ScenarioType::MAP_AND_SCENE)
		{
			// 左クリック
			// 自動的に次へ進むタイプ
			// イベントを次へ進める
			this->m_ScenarioItem = this->m_ScenarioManager.get( this->m_nScenarioListID )->next();
		}
		// 銃は撃たない
		return 0;
	}
	if( this->m_SceneManager.get_CurrentSceneID() == SceneType::BRIEFING ) {
		// ブリーフィング中である
		// 銃は撃たない
		return 0;
	}
	/////////////////////
	// 弾の発射

	int ret = 0;
	int eventno = -1;

	// 現在のフレームNoを取得
	int motid;
	int frameno;
	ret = E3DGetMotionFrameNoML( this->get_Player()->m_nSigID, this->get_Player()->m_nUpperBoneNumber, &motid, &frameno );


	if( this->get_Weapon()->get_Type() == WeaponType::CROWBAR ) {
		// バールなどの弾不要の直接攻撃系エネコン
		if( mouse->rgbButtons[0] ) {

			// 攻撃
			eventno = Motion::Tanya::BAR_ATTACK;
		}

	} else {
		if( mouse->rgbButtons[0] ) {

			// 左ボタンを押下している状態
			// 押しっぱなしも含む

			BOOL bShot = FALSE;

		
			if( (this->m_cntShotAutomaticCounter->step() & General::CounterStateType::ALL_LOOP_END) && 
				this->get_Weapon()->get_LoadedAmmo() > 0)
			{
				// カウンタ状態をチェック

				// 連射間隔が終わった
				// 弾がまだある

				// カウンタリセット
				this->m_cntShotAutomaticCounter->reset();

				// ショットの高さ
				D3DXVECTOR3 vEyeHeightVector(0,BasicStaticDataConsts::PLAYER_SHOT_HEIGHT_FROM_CENTER,0);

				// プレイヤーが向いている方向
				D3DXVECTOR3 vPlayerDirection;
				E3DGetCharaDir( this->get_Player()->m_nSigID, 0,0, &vPlayerDirection );

				// プレイヤーの位置
				D3DXVECTOR3 vPlayerPosition;
				E3DGetPos( this->get_Player()->m_nSigID, &vPlayerPosition );

				////////////
				// プレイヤーが向いている方向より少し右回転した所から発射させる
				// Y軸中心に（左回転）N度
				D3DXMATRIX matrixY;
				float fDegree = 15;
				if( this->m_nWeaponIndex == WeaponType::HAND_GUN ) {
					fDegree = -15.f;
				}
				D3DXMatrixRotationY( &matrixY, D3DXToRadian( fDegree ) );

				// 行列変換
				D3DXVECTOR3 vRotatedDirection;
				D3DXVec3TransformCoord( &vRotatedDirection, &vPlayerDirection, &(matrixY) );
				///////////////

				// 位置をプレイヤーの少し前方にする
				float fDistance = SHOT_FROM_DISTANCE;
				if( this->m_nWeaponIndex == WeaponType::HAND_GUN ) {
					fDistance = SHOT_FROM_DISTANCE + 200.f;
				}
				D3DXVECTOR3 vShotFrom = vPlayerPosition + vEyeHeightVector + vRotatedDirection * fDistance;

				if( this->m_nWeaponIndex == WeaponType::MISSILE_LAUNCHER //||
					//this->m_nWeaponIndex == WeaponType::HAND_GUN
					) 
				{
					vShotFrom = vPlayerPosition + vEyeHeightVector + vPlayerDirection * SHOT_FROM_DISTANCE;

				}
				// 発射方向
				D3DXVECTOR3 vDirection;
				this->get_ShotDirection( &vDirection , vShotFrom);
				vDirection *= -1;

				if( this->m_nWeaponIndex == WeaponType::MISSILE_LAUNCHER  ) {
					E3DObject* obj = this->m_ShotManager.search( ShotType::MISSILE);
					if( obj != NULL ) {
						// プレイヤー
						obj->set_Valid( TRUE );
						E3DSetValidFlag( obj->m_nSigID, -1, TRUE );


						E3DSetPos( obj->m_nSigID, vShotFrom);

						// プレイヤーの方向にする
						//D3DXVECTOR3 vDirection;
						//this->get_ShotDirection( &vDirection, vShotFrom );
						//vDirection *= -1;
						//E3DSetDir( obj->m_nSigID, vDirection );
						//クォータニオンをその方向へ向ける
						obj->m_Move.m_vVelocity = vDirection;
						this->face( vDirection, 1, obj->m_nSigID, obj->m_Move.m_nQuoatanion );
						//E3DEulerZXY2Q( obj->m_Move.m_nQuoatanion, vDirection);
						//D3DXVECTOR3 vEulerPtr;
						//E3DQtoEuler( obj->m_Move.m_nQuoatanion, &vEulerPtr, D3DXVECTOR3(0,0, 0 ) );
						//E3DSetDir( obj->m_nSigID, vEulerPtr );

						bShot = TRUE;

					}
				} else {
					// 弾を発射
					// 弾が空いているか確認
					//E3DBillBoard* billBoard = this->m_ShotBillBoardManager.search();
					E3DObject* obj = this->m_ShotManager.search( ShotType::BULLET );

					if( obj != NULL ) {
						bShot = TRUE;
		
						E3DSetPos( obj->m_nSigID, vShotFrom);

						// プレイヤーの方向にする
						//クォータニオンをその方向へ向ける
						obj->m_Move.m_vVelocity = vDirection;
						this->face( vDirection, 1, obj->m_nSigID, obj->m_Move.m_nQuoatanion );

						// Shell 追加

						E3DObject* shell = this->m_NeutralManager.search( NeutralType::SHELL );
						if( shell != NULL ) {
							E3DSetPos( shell->m_nSigID, 
								vPlayerPosition + vEyeHeightVector + vRotatedDirection * 500.f );

							//向いている方向はプレイヤーの向き
							this->face( vDirection, 1, shell->m_nSigID, obj->m_Move.m_nQuoatanion );

							// プレイヤーの右方向を取得
							D3DXVECTOR3 vRight;
							E3DGetCharaDir( this->get_Player()->m_nSigID, -90, 0, &vRight );

							D3DXVECTOR3 vError(MathTool::nextRandom<float>(-SHELL_ERROR, SHELL_ERROR),
								0,MathTool::nextRandom<float>(-SHELL_ERROR, SHELL_ERROR ) );
							shell->m_Move.m_vVelocity = vRight * SHELL_SPEED + vError;
							shell->m_Move.m_vVelocity.y = SHELL_SPEED + MathTool::nextRandom<float>(-SHELL_ERROR, SHELL_ERROR );
							
						}
					}
				} // weapon type

				if( bShot ) {
					// 発射

					if( this->m_aryOptionList[ TitleSceneConsts::CheckType::MUGEN ] ) {
						// 無限バンダナ
					} else {
						// 弾減少
						this->get_Weapon()->set_LoadedAmmo( this->get_Weapon()->get_LoadedAmmo() - 1 );
					}

					// SE
					int playing;
					ret = E3DIsSoundPlaying( this->m_arySoundList[ Sound::GUN] , &playing );
					if( playing ) {
						ret = E3DStopSound( this->m_arySoundList[ Sound::GUN ]);
					}
					ret = E3DPlaySound( this->m_arySoundList[ Sound::GUN ], 0, 0, 0 );
				}
			}// counter step

		
		}
	}
	if( this->get_Player()->m_Game.m_nType == PlayerType::TANYA ) {
		if( this->m_nWeaponIndex == WeaponType::CROWBAR ) {
			// バール
			if( this->get_Player()->m_nEventNumber <= Motion::Tanya::JUMP_DOWN ) {
				// 通常状態
			}

			if( mouse->rgbButtons[1] ) {
				if( //eventno != Motion::Tanya::BAR_SET &&
					eventno != Motion::Tanya::BAR_ATTACK ) 
				{
					// 構え
					eventno = Motion::Tanya::BAR_SET;
				}
			}
		} 
	} else if( this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN ) {
		if( this->m_nWeaponIndex == WeaponType::MINI_GUN ) {
			// ミニガン所持

			// ミニガン所持状態にする
			eventno = Motion::Evigen::MINI_GUN_CARRY;

			if( //this->get_Player()->m_nEventNumber > Motion::Evigen::IDLE  &&
				this->get_Player()->m_nEventNumber <= Motion::Evigen::JUMP_DOWN ||
				this->get_Player()->m_nEventNumber != Motion::Evigen::MINI_GUN_UP &&
				this->get_Player()->m_nEventNumber != Motion::Evigen::MINI_GUN_CARRY
				)
			{
				
				// 持ち上げる
				eventno = Motion::Evigen::MINI_GUN_UP;

			}
			if( this->get_Player()->m_nEventNumber == Motion::Evigen::MINI_GUN_DOWN && (mouse->rgbButtons[1] || mouse->rgbButtons[0] ) ||
				this->get_Player()->m_nEventNumber == Motion::Evigen::MINI_GUN_SETTLE && (mouse->rgbButtons[1] || mouse->rgbButtons[0] ) 
				)
			{
				eventno = this->get_Player()->m_nEventNumber;

			}
			if( this->get_Player()->m_nEventNumber == Motion::Evigen::MINI_GUN_DOWN ) {
				if(// motid != 0 && //this->get_Player()->m_nOldEventNumber == this->get_Player()->m_nEventNumber &&
					frameno < 28 )
				{
					// 下がりきっていない
					eventno = this->get_Player()->m_nEventNumber;
				}
			}

			if( mouse->rgbButtons[1] ) {
				// 右ボタンを押した
				if( //this->get_Player()->m_nEventNumber == Motion::Evigen::MINI_GUN_CARRY ||
					this->get_Player()->m_nEventNumber == Motion::Evigen::MINI_GUN_DOWN &&
					motid != 0  && frameno > 28 && this->get_Player()->m_nOldEventNumber == this->get_Player()->m_nEventNumber//||
					//this->get_Player()->m_nEventNumber == Motion::Evigen::MINI_GUN_FIRE
					)
				{
					// 構え
					eventno = (Motion::Evigen::MINI_GUN_SETTLE );

					// TODO砲身回転
				} else if( this->get_Player()->m_nEventNumber == Motion::Evigen::MINI_GUN_SETTLE  ) {
					eventno = Motion::Evigen::MINI_GUN_SETTLE;
				} else {
					// 構え
					eventno = (Motion::Evigen::MINI_GUN_DOWN );
				}
			}
			if( mouse->rgbButtons[0] ) {
				// 撃つ
				eventno = (Motion::Evigen::MINI_GUN_FIRE );

				// テクスチャーを変える
				if( this->m_nPlayerMaterialIndex != Material::SHOOT ) {
					ret = this->set_Texture( this->get_Player()->m_nSigID, "atama", Material::SHOOT, TRUE );
				}
			}
		} else if( this->m_nWeaponIndex == WeaponType::HAND_GUN ){
			// ハンドガン所持状態にする
			// ミニガン所持状態にする
			eventno = Motion::Evigen::HAND_GUN_IDLE;

			if( //this->get_Player()->m_nEventNumber > Motion::Evigen::IDLE  &&
				this->get_Player()->m_nEventNumber <= Motion::Evigen::JUMP_DOWN ||
				this->get_Player()->m_nEventNumber != Motion::Evigen::HAND_GUN_UP &&
				this->get_Player()->m_nEventNumber != Motion::Evigen::HAND_GUN_IDLE
				)
			{
				
				// 持ち上げる
				eventno = Motion::Evigen::HAND_GUN_UP;

			}
			if( this->get_Player()->m_nEventNumber == Motion::Evigen::HAND_GUN_UP && (mouse->rgbButtons[1] || mouse->rgbButtons[0] )  ||
				this->get_Player()->m_nEventNumber == Motion::Evigen::HAND_GUN_SETTLE && (mouse->rgbButtons[1] || mouse->rgbButtons[0] )  
				)
			{
				eventno = this->get_Player()->m_nEventNumber;
			}

			if( mouse->rgbButtons[1] ) {
				// 右ボタンを押した
				if( //this->get_Player()->m_nEventNumber == Motion::Evigen::HAND_GUN_IDLE ||
					this->get_Player()->m_nEventNumber == Motion::Evigen::HAND_GUN_DOWN &&
					motid != 0 && frameno > 8 && this->get_Player()->m_nOldEventNumber == this->get_Player()->m_nEventNumber//||
					//this->get_Player()->m_nEventNumber == Motion::Evigen::HAND_GUN_FIRE
					)
				{
					// 構え
					eventno = (Motion::Evigen::HAND_GUN_SETTLE );

					// TODO砲身回転
				} else if( this->get_Player()->m_nEventNumber == Motion::Evigen::HAND_GUN_SETTLE ) {
					// そのまま
					eventno = Motion::Evigen::HAND_GUN_SETTLE;
				} else {
					// 構え
					eventno = (Motion::Evigen::HAND_GUN_UP );
				}
			}
			if( mouse->rgbButtons[0] ) {
				// 撃つ
				eventno = (Motion::Evigen::HAND_GUN_FIRE );

				// テクスチャーを変える
				if( this->m_nPlayerMaterialIndex != Material::SHOOT ) {
					ret = this->set_Texture( this->get_Player()->m_nSigID, "atama", Material::SHOOT, TRUE );
				}
			}
		}
	}

	if( this->m_nPlayerType == PlayerType::EVIGEN &&
		!((this->m_nWeaponIndex == WeaponType::HAND_GUN || this->m_nWeaponIndex == WeaponType::MINI_GUN)  && mouse->rgbButtons[0] ) )
	{
		// それ以外
		if( this->m_nPlayerMaterialIndex == Material::SHOOT ) {
			// 元に戻す
			ret = this->set_Texture( this->get_Player()->m_nSigID, "atama", Material::NORMAL, TRUE );
		}
	}

	/////
	// リロード
	if( this->get_Weapon()->get_Type() == WeaponType::CROWBAR ) {
	} else if( this->get_Weapon()->get_LoadedAmmo() < this->get_Weapon()->get_LoadedAmmoMax() &&
		this->get_Weapon()->get_ChargedAmmo() > 0 &&
		this->get_CurrentKeyBuffer('R' ) && this->get_PreviousKeyBuffer('R') == FALSE ) 
	{
		if( this->m_nWeaponIndex == WeaponType::MINI_GUN &&
			this->get_Player()->m_nEventNumber != Motion::Evigen::MINI_GUN_CARRY )
		{
		} else if( this->m_nWeaponIndex == WeaponType::HAND_GUN &&
			this->get_Player()->m_nEventNumber != Motion::Evigen::HAND_GUN_IDLE )
		{
		} else {
			// 弾が減少している
			// ストックがある
			// かつRキーを今押した

			// 差分取得
			int nDiff = this->get_Weapon()->get_LoadedAmmoMax() - this->get_Weapon()->get_LoadedAmmo();

			if( this->get_Weapon()->get_ChargedAmmo() < nDiff ) {
				// 残り補充するには足りない

				// 残りの数だけ補充
				nDiff = this->get_Weapon()->get_ChargedAmmo();
			}

			// リロードする
			// Loadedに弾を補充
			this->get_Weapon()->set_LoadedAmmo( this->get_Weapon()->get_LoadedAmmo() + nDiff );

			// Chargedから弾を削除
			this->get_Weapon()->set_ChargedAmmo( this->get_Weapon()->get_ChargedAmmo() - nDiff );

			if( this->m_nWeaponIndex == WeaponType::HAND_GUN) {
				// ミニガン所有時
				// リロード動作
				eventno = Motion::Evigen::HAND_GUN_RELOAD;

			} else if( this->m_nWeaponIndex == WeaponType::MINI_GUN ) {
				// 一度下げて上に戻す
				eventno = Motion::Evigen::MINI_GUN_DOWN;
			}
		}
	}

	if( eventno >= 0 ) {
		this->get_Player()->m_nEventNumber = eventno;
		/*
		if( this->get_Player()->m_nEventNumber >= 1 ) {
			// 走りながら、歩きながら動いている
	
			this->get_Player()->m_nUnderEventNumber = this->get_Player()->m_nEventNumber;

			// 上半身は銃
			this->get_Player()->m_nEventNumber = eventno;
		} else {
			// 性質状態で発射
			// ->ノーマル発射
			this->get_Player()->m_nEventNumber = eventno;
			// 別モーションにしない
			this->get_Player()->m_nUnderEventNumber = -1;
		}
	} else {
		this->get_Player()->m_nUnderEventNumber = -1;*/
	}
	return 0;
}


int StaticData::moveCamera() {
	int ret = 0;

	// 必ず呼ぶからここに書く
	ret = E3DUpdateSound();

	//////////////
	// カメラ移動

	// マウスで向きを変更
	DIMOUSESTATE2* mouse = this->get_DIMouse()->get_CurrentState();

	float fRateHorizontal = 1;
	float fRateVertical = 1;

	long lMouseX = mouse->lX;
	if( this->m_SceneManager.get_CurrentSceneID() == SceneType::TITLE ) {
		lMouseX += TITLE_ROTATE_Y_SPEED;
	}
	this->moveCameraPosition(
		lMouseX,
		mouse->lY, 
		&fRateHorizontal,
		&fRateVertical );

	if( this->get_CameraMode() == CameraMode::SHOT_BEHIND_MODE ) {
		// 左右回転する
		E3DRotateY( this->get_Player()->m_nSigID, fRateHorizontal * mouse->lX);
		/*E3DGetDirQ2( this->get_Player()->m_nSigID, this->get_Player()->m_Movem_nQuoatanion );
			E3DRotateQLocalX( this->get_Player()->m_Movem_nQuoatanion, fRateHorizontal * mouse->lX);
			E3DRotateQLocalY( this->get_Player()->m_Movem_nQuoatanion, fRateVertical * mouse->lY);
		E3DSetDirQ2( this->get_Player()->m_nSigID, this->get_Player()->m_Movem_nQuoatanion );*/
	}
	if( this->get_CameraMode() == CameraMode::SNIPER_MODE ) {
		// 左右回転する
		E3DRotateY( this->get_Player()->m_nSigID, fRateHorizontal * mouse->lX);
	}




	if(mouse->lZ ) {
		// ホイール

		if( this->get_CurrentKeyBuffer( 'C' ) ) {
			// C+Wheel →カメラズーム

			float fStep = this->get_Camera()->get_Distance() * MOUSE_LOOK_ZOOM_STEP_RATE * mouse->lZ;
			float fDistance = this->get_Camera()->get_Distance() - fStep;////MOUSE_LOOK_ZOOM_STEP * mouse->lZ;
				//this->get_CameraDistance() + MOUSE_LOOK_ZOOM_STEP * (-mouse->lZ );
			if(fDistance < MOUSE_LOOK_ZOOM_MIN ) {
				fDistance = MOUSE_LOOK_ZOOM_MIN;
			}
			if( fDistance > MOUSE_LOOK_ZOOM_MAX ) {
				fDistance = MOUSE_LOOK_ZOOM_MAX;
			}
			this->get_Camera()->set_Distance( fDistance );
		} else {
			if( this->m_SceneManager.get_CurrentSceneID() != SceneType::BRIEFING ) {
				if( this->m_nCameraTargetSigID == this->get_Player()->m_nSigID ) {
					// 武器切り替え
					if( mouse->lZ > 0 ) {
						// Next
						this->set_NextWeapon();
					} else {
						this->set_PreviousWeapon();
					}
				}
			}
		}
	}

	DIMOUSESTATE2* mousePrevious = this->get_DIMouse()->get_PreviousState();
	if( this->get_Player()->m_nEventNumber >= Motion::Evigen::DAMAGE_A &&
		this->get_Player()->m_nEventNumber <= Motion::Evigen::RETURN )
	{
		// ダメージや復帰中は射撃モード変更不可
		
		//this->set_CameraMode( CameraMode::AT_TARGET_MODE, this->get_Player()->m_nSigID );
	} else {
		if(mouse->rgbButtons[1] != 0 &&
			mousePrevious->rgbButtons[1] == 0)
		{
			// 押してない状態→押した
			// 射撃モードに変更
			this->set_CameraMode( CameraMode::SHOT_BEHIND_MODE, this->get_Player()->m_nSigID );


		} else if( mouse->rgbButtons[1] == 0 &&
			mousePrevious->rgbButtons[1] != 0 )
		{
			// 押している状態→離した
			// →通常モードに変更
			this->set_CameraMode( CameraMode::AT_TARGET_MODE, this->get_Player()->m_nSigID );
		}
	}

	if(mouse->rgbButtons[0] != 0 &&
		mousePrevious->rgbButtons[0] == 0 &&
		this->m_SceneManager.get_CurrentSceneID() == SceneType::GAME) 
	{
		// どこか左クリックしたら画面にカーソルがとらわれる
		this->set_Grab(TRUE);

		// ポーズ解除
		this->set_Pause( FALSE );
		this->m_EnemyManager.m_bPause = this->is_Pause();
		this->m_NeutralManager.m_bPause = this->is_Pause();
		this->m_ShotManager.m_bPause = this->is_Pause();
	}

	BOOL bLow = FALSE;
	BOOL bHigh = FALSE;
	if( this->get_Player()->m_nEventNumber == Motion::Evigen::DOWN ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::DEATH ||
		this->get_Player()->m_nEventNumber == Motion::Evigen::CROUCH ) 
	{
		bLow = TRUE;
	}
	if( this->get_Player()->m_nEventNumber == Motion::Evigen::SWALLOW ) {
		bHigh = TRUE;
	}

	if( this->m_nCameraTargetSigID != this->get_Player()->m_nSigID ) {
		bLow = FALSE;
		bHigh = FALSE;
	}
	D3DXVECTOR3 vPos;
	E3DGetPos( m_nCameraTargetSigID, &vPos);

	float fHeight = PLAYER_EYE_HEIGHT_FROM_CENTER;
	if( bLow ) {
		fHeight = PLAYER_EYE_HEIGHT_FROM_CENTER_LOW;
	}
	if( bHigh ) {
		fHeight = PLAYER_EYE_HEIGHT_FROM_CENTER_HIGH;
	}
	// カメラ位置更新＆位置設定
	ret = this->updateCameraPosition( m_nCameraTargetSigID,//this->get_Player()->m_nSigID,
		vPos,//		this->get_Player()->m_Move.m_vPosition ,
		fHeight );
	if(ret ) {
		_ASSERT( 0 );
		return ret;
	}

	// カメラ方向設定
	ret = this->set_CameraLookAt( m_nCameraTargetSigID,//this->get_Player()->m_nSigID,
		this->get_Player()->m_Move.m_nQuoatanion , fHeight);
	if(ret ) {
		_ASSERT( 0 );
		return ret;
	}


	// マウス座標を中心に移動
	// ウインドウサイズ取得
	if(this->get_Active() == TRUE  && this->get_Grab()) {
		POINT point;
		this->get_ClientCenter(&point );
		ClientToScreen( this->get_WindowHandle(), &point );
		SetCursorPos( point.x, point.y);
	}
	return 0;
}

void StaticData::move_BillBoard() {
	int ret = 0;

	//////
	// 血のビルボード動作
	for( std::list<E3DBillBoard*>::iterator it = this->m_BloodBillBoardManager.m_lstBillBoardList.begin();
		it != this->m_BloodBillBoardManager.m_lstBillBoardList.end(); it ++ ) 
	{
		E3DBillBoard* billBoard = *it;
		if( billBoard->get_Alive() == FALSE ) {
			// 表示していない
			continue;
		}
		
		BBINFO bbinfo;
		bbinfo.bbid = billBoard->get_BillBoardID();

		// 場所を含む情報取得
		ret = E3DGetBillboardInfo( &bbinfo );

		D3DXVECTOR3 vVelocity;
		billBoard->get_Velocity( &vVelocity );

		// 移動
		bbinfo.pos += vVelocity;

		// 重力加速
		vVelocity.y += GRAVITY;

		billBoard->set_Velocity( vVelocity );

		// 位置設定
		E3DSetBillboardPos( billBoard->get_BillBoardID(), 
			bbinfo.pos );

		if( billBoard->step() == TRUE ) {
			// 寿命が来た

			// 消える
			billBoard->set_Alive( FALSE );

			// 消える
			ret = E3DSetValidFlag( -1, billBoard->get_BillBoardID(), FALSE );
		}
	}
}

/** プレイヤーへの干渉やターゲッティングをするかどうか判断
  * @param[in]	obj		干渉してくる敵等
  * @retval		TRUE	プレイヤーへの干渉はしない。近くにいても無視する
  *				FALSE	プレイヤーへの干渉を行う。
  *
  */
BOOL StaticData::is_SkipPlayer( HPL::E3D::E3DObject* obj) {
	int ret = 0;

	if( staticData->get_Player()->m_Game.m_nHolderSigID >= 0 ) {
		// 捕まっている
		if( staticData->get_Player()->m_Game.m_nHolderSigID == obj->m_nSigID ) {
			// 干渉するが、捕まっている関係なので、処理は行う
		} else {
			// 他の敵に捕まっている
			// 何もしない
			return TRUE;
		}
	/*} else if( staticData->get_Player()->m_nEventNumber == Motion::Evigen::DAMAGE_A ||
		staticData->get_Player()->m_nEventNumber == Motion::Evigen::DAMAGE_B ||
		staticData->get_Player()->m_nEventNumber == Motion::Evigen::DAMAGE_CRASH_UP )
	{
	// ダメージ中
		// スキップ
		return TRUE;
		*/
	} else {

		if( staticData->m_nPlayerType == PlayerType::EVIGEN ) {
			int mat;
			E3DGetMaterialNoByName( staticData->get_Player()->m_nSigID, "atama", &mat );
			float fAlpha = 0;
			E3DGetMaterialAlpha( staticData->get_Player()->m_nSigID, mat, &fAlpha );

			if( fAlpha < 0.5f ) {
				// 首がすっとんでる
				// （上半身が吹っ飛んでる場合も同様）
				// 敵の攻撃が当たらない

				// 現在のフレームNoを取得
				int motid;
				int frameno;
				ret = E3DGetMotionFrameNoML( this->get_Player()->m_nSigID, this->get_Player()->m_nUpperBoneNumber, &motid, &frameno );
				if( motid != 0 && frameno < 110 ) {
					return TRUE;
				}

			}
		}

	}
	return FALSE;
}

void StaticData::faceToTarget( E3DObject* objFrom, int nToSigID, int nDivision ) {
	// プレイヤーの方向を向く
	int ret = 0;

	// プレイヤーの位置取得
	D3DXVECTOR3 vPlayerPosition;
	ret = E3DGetPos( nToSigID, &vPlayerPosition );

	D3DXVECTOR3 vPosition;
	ret = E3DGetPos( objFrom->m_nSigID, &vPosition );
	// プレイヤーへの方向取得
	D3DXVECTOR3 vDiff = vPlayerPosition - vPosition;

	
	D3DXVECTOR3 vDirection = -vDiff;
	vDirection.y = 0;
	staticData->face( vDirection,
		nDivision, objFrom->m_nSigID, objFrom->m_Move.m_nQuoatanion );
}
void StaticData::reset_Player( BOOL bIsHPMax ) {

	if( bIsHPMax ){ 
		// 回復
		staticData->get_Player()->m_Game.m_fHP = staticData->get_Player()->m_Game.m_fHPMax;
	}

	// 武器選択 TODO
	if( this->get_Player()->m_Game.m_nType == PlayerType::EVIGEN ) {
		//
		staticData->set_WeaponIndex(WeaponType::GRANADE_LAUNCHER);
	} else if( this->get_Player()->m_Game.m_nType == PlayerType::TANYA ) {
		staticData->set_WeaponIndex(WeaponType::CROWBAR);
	}

	for(int i = 0; i < PlayerType::MAX_NUM; i ++ ) {
		m_aryPlayerList[i].m_Game.m_nHolderSigID = -1;
		// 位置初期化
		m_aryPlayerList[i].m_Move.m_vVelocity = D3DXVECTOR3(0,0,0);
		m_aryPlayerList[i].m_Move.m_vPosition = D3DXVECTOR3(0,0,0);
		//E3DSetPosOnGround( staticData->get_Player()->m_nSigID, staticData->get_GroundSigID(),
		//	MAP_HEIGHT, MAP_MIN, 0, 0 );
		//E3DGetPos( staticData->get_Player()->m_nSigID, &staticData->get_Player()->m_Move.m_vPosition );
		E3DSetPos( m_aryPlayerList[i].m_nSigID, m_aryPlayerList[i].m_Move.m_vPosition );
	}

	// ポーズ解除
	staticData->set_Pause( FALSE );

}

}//	namespace MotionTest end
