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.

124 lines
3.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Game-specific impact effect hooks
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "fx.h"
  8. #include "c_te_effect_dispatch.h"
  9. #include "tier0/vprof.h"
  10. #include "clientsideeffects.h"
  11. #include "clienteffectprecachesystem.h"
  12. #include "view.h"
  13. #include "collisionutils.h"
  14. #include "SoundEmitterSystem/isoundemittersystembase.h"
  15. #include "engine/IEngineSound.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. CLIENTEFFECT_REGISTER_BEGIN( PrecacheTFTracers )
  19. CLIENTEFFECT_MATERIAL( "effects/spark" )
  20. CLIENTEFFECT_REGISTER_END()
  21. #define LISTENER_HEIGHT 24
  22. #define TRACER_TYPE_FAINT 4
  23. //-----------------------------------------------------------------------------
  24. // Purpose:
  25. //-----------------------------------------------------------------------------
  26. void FX_TFTracerSound( const Vector &start, const Vector &end, int iTracerType )
  27. {
  28. // don't play on very short hits
  29. if ( ( start - end ).Length() < 200 )
  30. return;
  31. const char *pszSoundName = "Bullets.DefaultNearmiss";
  32. float flWhizDist = 64;
  33. Vector vecListenOrigin = MainViewOrigin();
  34. switch( iTracerType )
  35. {
  36. case TRACER_TYPE_DEFAULT:
  37. flWhizDist = 96;
  38. // fall through !
  39. default:
  40. {
  41. Ray_t bullet, listener;
  42. bullet.Init( start, end );
  43. Vector vecLower = vecListenOrigin;
  44. vecLower.z -= LISTENER_HEIGHT;
  45. listener.Init( vecListenOrigin, vecLower );
  46. float s, t;
  47. IntersectRayWithRay( bullet, listener, s, t );
  48. t = clamp( t, 0.f, 1.f );
  49. vecListenOrigin.z -= t * LISTENER_HEIGHT;
  50. }
  51. break;
  52. }
  53. static float flNextWhizTime = 0;
  54. // Is it time yet?
  55. float dt = flNextWhizTime - gpGlobals->curtime;
  56. if ( dt > 0 )
  57. return;
  58. // Did the thing pass close enough to our head?
  59. float vDist = CalcDistanceSqrToLineSegment( vecListenOrigin, start, end );
  60. if ( vDist >= (flWhizDist * flWhizDist) )
  61. return;
  62. CSoundParameters params;
  63. if( C_BaseEntity::GetParametersForSound( pszSoundName, params, NULL ) )
  64. {
  65. // Get shot direction
  66. Vector shotDir;
  67. VectorSubtract( end, start, shotDir );
  68. VectorNormalize( shotDir );
  69. CLocalPlayerFilter filter;
  70. enginesound->EmitSound( filter, SOUND_FROM_WORLD, CHAN_STATIC, params.soundname,
  71. params.volume, SNDLVL_TO_ATTN(params.soundlevel), 0, params.pitch, 0, &start, &shotDir, NULL);
  72. }
  73. // Don't play another bullet whiz for this client until this time has run out
  74. flNextWhizTime = gpGlobals->curtime + 0.1f;
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Purpose:
  78. //-----------------------------------------------------------------------------
  79. void FX_BrightTracer( Vector& start, Vector& end )
  80. {
  81. //Don't make small tracers
  82. float dist;
  83. Vector dir;
  84. int velocity = 5000;
  85. VectorSubtract( end, start, dir );
  86. dist = VectorNormalize( dir );
  87. // Don't make short tracers.
  88. float length = random->RandomFloat( 64.0f, 128.0f );
  89. float life = ( dist + length ) / velocity; //NOTENOTE: We want the tail to finish its run as well
  90. //Add it
  91. FX_AddDiscreetLine( start, dir, velocity, length, dist, random->RandomFloat( 1, 3 ), life, "effects/spark" );
  92. FX_TFTracerSound( start, end, TRACER_TYPE_DEFAULT );
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Purpose: Bright Tracer for Machine Guns
  96. //-----------------------------------------------------------------------------
  97. void BrightTracerCallback( const CEffectData &data )
  98. {
  99. FX_BrightTracer( (Vector&)data.m_vStart, (Vector&)data.m_vOrigin );
  100. }
  101. DECLARE_CLIENT_EFFECT( "BrightTracer", BrightTracerCallback );