Team Fortress 2 Source Code as on 22/4/2020
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.

215 lines
5.1 KiB

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