Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
5.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "cam_thirdperson.h"
  8. #include "gamerules.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. static Vector CAM_HULL_MIN(-CAM_HULL_OFFSET,-CAM_HULL_OFFSET,-CAM_HULL_OFFSET);
  12. static Vector CAM_HULL_MAX( CAM_HULL_OFFSET, CAM_HULL_OFFSET, CAM_HULL_OFFSET);
  13. #ifdef CLIENT_DLL
  14. #include "input.h"
  15. extern const ConVar *sv_cheats;
  16. extern ConVar cam_idealdist;
  17. extern ConVar cam_idealdistright;
  18. extern ConVar cam_idealdistup;
  19. void CAM_ToThirdPerson(void);
  20. void CAM_ToFirstPerson(void);
  21. void ToggleThirdPerson( bool bValue )
  22. {
  23. if ( bValue == true )
  24. {
  25. CAM_ToThirdPerson();
  26. }
  27. else
  28. {
  29. CAM_ToFirstPerson();
  30. }
  31. }
  32. void ThirdPersonChange( IConVar *pConVar, const char *pOldValue, float flOldValue )
  33. {
  34. ConVarRef var( pConVar );
  35. ToggleThirdPerson( var.GetBool() );
  36. }
  37. ConVar cl_thirdperson( "cl_thirdperson", "0", FCVAR_NOT_CONNECTED | FCVAR_ARCHIVE | FCVAR_DEVELOPMENTONLY, "Enables/Disables third person", ThirdPersonChange );
  38. #endif
  39. CThirdPersonManager::CThirdPersonManager( void )
  40. {
  41. }
  42. void CThirdPersonManager::Init( void )
  43. {
  44. m_bOverrideThirdPerson = false;
  45. m_bForced = false;
  46. m_flUpFraction = 0.0f;
  47. m_flFraction = 1.0f;
  48. m_flUpLerpTime = 0.0f;
  49. m_flLerpTime = 0.0f;
  50. m_flUpOffset = CAMERA_UP_OFFSET;
  51. if ( input )
  52. {
  53. input->CAM_SetCameraThirdData( NULL, vec3_angle );
  54. }
  55. }
  56. void CThirdPersonManager::Update( void )
  57. {
  58. #ifdef CLIENT_DLL
  59. if ( !sv_cheats )
  60. {
  61. sv_cheats = cvar->FindVar( "sv_cheats" );
  62. }
  63. bool bIsThirdPerson = input->CAM_IsThirdPerson() != 0;
  64. // If cheats have been disabled, pull us back out of third-person view.
  65. if ( sv_cheats && !sv_cheats->GetBool() && GameRules() && GameRules()->AllowThirdPersonCamera() == false )
  66. {
  67. if ( bIsThirdPerson )
  68. {
  69. input->CAM_ToFirstPerson();
  70. }
  71. return;
  72. }
  73. if ( IsOverridingThirdPerson() == false )
  74. {
  75. if ( bIsThirdPerson != ( cl_thirdperson.GetBool() || m_bForced ) && GameRules() && GameRules()->AllowThirdPersonCamera() == true )
  76. {
  77. ToggleThirdPerson( m_bForced || cl_thirdperson.GetBool() );
  78. }
  79. }
  80. #endif
  81. }
  82. Vector CThirdPersonManager::GetDesiredCameraOffset( void )
  83. {
  84. if ( IsOverridingThirdPerson() == true )
  85. {
  86. return Vector( cam_idealdist.GetFloat(), cam_idealdistright.GetFloat(), cam_idealdistup.GetFloat() );
  87. }
  88. return m_vecDesiredCameraOffset;
  89. }
  90. Vector CThirdPersonManager::GetFinalCameraOffset( void )
  91. {
  92. Vector vDesired = GetDesiredCameraOffset();
  93. if ( m_flUpFraction != 1.0f )
  94. {
  95. vDesired.z += m_flUpOffset;
  96. }
  97. return vDesired;
  98. }
  99. Vector CThirdPersonManager::GetDistanceFraction( void )
  100. {
  101. if ( IsOverridingThirdPerson() == true )
  102. {
  103. return Vector( m_flTargetFraction, m_flTargetFraction, m_flTargetFraction );
  104. }
  105. float flFraction = m_flFraction;
  106. float flUpFraction = m_flUpFraction;
  107. float flFrac = RemapValClamped( gpGlobals->curtime - m_flLerpTime, 0, CAMERA_OFFSET_LERP_TIME, 0, 1 );
  108. flFraction = Lerp( flFrac, m_flFraction, m_flTargetFraction );
  109. if ( flFrac == 1.0f )
  110. {
  111. m_flFraction = m_flTargetFraction;
  112. }
  113. flFrac = RemapValClamped( gpGlobals->curtime - m_flUpLerpTime, 0, CAMERA_UP_OFFSET_LERP_TIME, 0, 1 );
  114. flUpFraction = 1.0f - Lerp( flFrac, m_flUpFraction, m_flTargetUpFraction );
  115. if ( flFrac == 1.0f )
  116. {
  117. m_flUpFraction = m_flTargetUpFraction;
  118. }
  119. return Vector( flFraction, flFraction, flUpFraction );
  120. }
  121. void CThirdPersonManager::PositionCamera( CBasePlayer *pPlayer, QAngle angles )
  122. {
  123. if ( pPlayer )
  124. {
  125. trace_t trace;
  126. Vector camForward, camRight, camUp;
  127. // find our player's origin, and from there, the eye position
  128. Vector origin = pPlayer->GetLocalOrigin();
  129. origin += pPlayer->GetViewOffset();
  130. AngleVectors( angles, &camForward, &camRight, &camUp );
  131. Vector endPos = origin;
  132. Vector vecCamOffset = endPos + (camForward * - GetDesiredCameraOffset()[DIST_FORWARD]) + (camRight * GetDesiredCameraOffset()[ DIST_RIGHT ]) + (camUp * GetDesiredCameraOffset()[ DIST_UP ] );
  133. // use our previously #defined hull to collision trace
  134. CTraceFilterSimple traceFilter( pPlayer, COLLISION_GROUP_NONE );
  135. UTIL_TraceHull( endPos, vecCamOffset, CAM_HULL_MIN, CAM_HULL_MAX, MASK_SOLID & ~CONTENTS_MONSTER, &traceFilter, &trace );
  136. if ( trace.fraction != m_flTargetFraction )
  137. {
  138. m_flLerpTime = gpGlobals->curtime;
  139. }
  140. m_flTargetFraction = trace.fraction;
  141. m_flTargetUpFraction = 1.0f;
  142. //If we're getting closer to a wall snap the fraction right away.
  143. if ( m_flTargetFraction < m_flFraction )
  144. {
  145. m_flFraction = m_flTargetFraction;
  146. m_flLerpTime = gpGlobals->curtime;
  147. }
  148. // move the camera closer if it hit something
  149. if( trace.fraction < 1.0 )
  150. {
  151. m_vecCameraOffset[ DIST ] *= trace.fraction;
  152. UTIL_TraceHull( endPos, endPos + (camForward * - GetDesiredCameraOffset()[DIST_FORWARD]), CAM_HULL_MIN, CAM_HULL_MAX, MASK_SOLID & ~CONTENTS_MONSTER, &traceFilter, &trace );
  153. if ( trace.fraction != 1.0f )
  154. {
  155. if ( trace.fraction != m_flTargetUpFraction )
  156. {
  157. m_flUpLerpTime = gpGlobals->curtime;
  158. }
  159. m_flTargetUpFraction = trace.fraction;
  160. if ( m_flTargetUpFraction < m_flUpFraction )
  161. {
  162. m_flUpFraction = trace.fraction;
  163. m_flUpLerpTime = gpGlobals->curtime;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. bool CThirdPersonManager::WantToUseGameThirdPerson( void )
  170. {
  171. return cl_thirdperson.GetBool() && GameRules() && GameRules()->AllowThirdPersonCamera() && IsOverridingThirdPerson() == false;
  172. }
  173. CThirdPersonManager g_ThirdPersonManager;