/*
 * 功能: 设定引擎相机
 * 摘要: -
 * 参数: pCamera - 需要设定的引擎相机指针
 *		 fX		 - 玩家处于世界的X坐标
 *		 fY		 - 玩家处于世界的Y坐标
 * 返回值: -
 * 作者: lpf
 * 创建日期: 2008.02.02
 */
void CRegionCamera::SetCamera(render::Camera * pCamera, float fX, float fY)
{
	float x1(fY);
	float z1(fX);
	CClientRegion * pRegion = GetGame()->GetRegion();
	D3DXVECTOR3 vMainPlayerPos(x1, pRegion->GetGameMap()->CalculateHeight(x1, z1) + 0.5f - GetCameraPosModify(), z1);
	D3DXVECTOR3 vTPos;
	D3DXVECTOR3 vPos;
	D3DXVECTOR3 vViewTmp;

	GetPosition(vTPos);

	// 相机震动
	if (m_dwState & SCS_SHOCK)
		vMainPlayerPos.y += m_fShcokrOffset;

	static D3DXVECTOR3 vView(vMainPlayerPos);

	// 相机平滑
	if (m_dwState & SCS_SMOOTH)
		SmoothCamera(vView, vMainPlayerPos);
	else
	{
		vView	  = vMainPlayerPos;
		m_dwState |= SCS_SMOOTH;
	}

	vViewTmp = vView;

	// 相机移动
	if (m_dwState & SCS_MOVE)
	{
		vTPos    += m_vMoveDis;
		vViewTmp += m_vMoveDis;
	}

	// 设定相机
	vPos = vView + vTPos;
	pCamera->SetView(&vViewTmp);
	if (GetGame())
	{
		if (GetGame()->GetRegion())
		{
			if (GetGame()->GetRegion()->GetGameMap())
			{
				if (vPos.y - 0.5 < GetGame()->GetRegion()->GetGameMap()->CalculateHeight(vPos.x,vPos.z))
				{
					vPos.y = GetGame()->GetRegion()->GetGameMap()->CalculateHeight(vPos.x,vPos.z) + 2;
				}
			}
		}
	}
	pCamera->SetPosition(&vPos);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CASWInput::ASW_GetCameraLocation( C_ASW_Player *pPlayer, Vector &vecCameraLocation, QAngle &angCamera, int &nMouseX, int &nMouseY, bool bApplySmoothing )
{
	// Verify data.
	Assert( pPlayer != NULL );
	if ( !pPlayer )
		return;

	Assert( ASWInput() != NULL );
	if ( !ASWInput() )
		return;

	// If we've already calculated the camera position on this frame, then just return the previous result.
// 	if ( pPlayer->m_nLastCameraFrame == gpGlobals->framecount )
// 	{
// 		vecCameraLocation = pPlayer->m_vecLastCameraPosition;
// 		angCamera = pPlayer->m_angLastCamera;
// 		return;
// 	}

	// Get the current camera position.
	vecCameraLocation = pPlayer->EyePosition();

	// Get the camera angles and calculate the camera view directions.
	Vector vecCameraDirection;
	::input->CAM_GetCameraOffset( vecCameraDirection );

	angCamera[PITCH] = vecCameraDirection[PITCH];
	angCamera[YAW] = vecCameraDirection[YAW];
	angCamera[ROLL] = 0;

	Vector vecCamForward, vecCamRight, vecCamUp;
	AngleVectors( angCamera, &vecCamForward, &vecCamRight, &vecCamUp );

	// Get the window center.
	int nCenterX, nCenterY;
	ASWInput()->ASW_GetWindowCenter( nCenterX, nCenterY );

	// Get the position change.
	int nUnclampedX, nUnclampedY;
	ASWInput()->GetSimulatedFullscreenMousePos( &nMouseX, &nMouseY, &nUnclampedX, &nUnclampedY );

	// Calculate the movement delta - only needed for mouse control or controller with pan enabled.
	int nDeltaX = 0;
	int nDeltaY = 0;
	if ( !ASWInput()->ControllerModeActive() || joy_pan_camera.GetBool() )
	{
		nDeltaX = nMouseX - nCenterX;
		nDeltaY = nMouseY - nCenterY;

		// Calculate the camera shift and move the camera.
		float flShiftX, flShiftY;
		CalculateCameraShift( pPlayer, (float) nDeltaX / ( nCenterX * 2.0f ), (float) nDeltaY / ( nCenterY * 2.0f ), flShiftX, flShiftY );

		VectorMA( vecCameraLocation, flShiftX, vecCamRight, vecCameraLocation );
		vecCamUp.z = 0;	// don't want the camera changing z
		vecCamUp.NormalizeInPlace();
		VectorMA( vecCameraLocation, -flShiftY, vecCamUp, vecCameraLocation );
	}

	bool bDeathcam = ASWGameRules() && ( ASWGameRules()->GetMarineDeathCamInterp() > 0.0f );
	
	// Smooth the camera movement.
	if ( bApplySmoothing && !bDeathcam )
	{
		SmoothCamera( pPlayer, vecCameraLocation );
		pPlayer->m_vecLastCameraPosition = vecCameraLocation;
	}

	// Update the player camera data.
	pPlayer->m_nLastCameraFrame = gpGlobals->framecount;
	pPlayer->m_angLastCamera = angCamera;

	// Do we still need this???
	pPlayer->m_hLastMarine = pPlayer->GetMarine();
}