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.

139 lines
4.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "particles_simple.h"
  8. #include "particles_localspace.h"
  9. #include "c_te_effect_dispatch.h"
  10. #include "clienteffectprecachesystem.h"
  11. // Precache our effects
  12. CLIENTEFFECT_REGISTER_BEGIN( PrecacheEffect_CS_MuzzleFlash )
  13. CLIENTEFFECT_MATERIAL( "effects/muzzleflashX" ) //.vmt
  14. CLIENTEFFECT_MATERIAL( "sprites/muzzleflash4" ) //.vmt
  15. CLIENTEFFECT_REGISTER_END()
  16. void TE_DynamicLight( IRecipientFilter& filter, float delay,
  17. const Vector* org, int r, int g, int b, int exponent, float radius, float time, float decay, int nLightIndex = LIGHT_INDEX_TE_DYNAMIC );
  18. void CS_MuzzleFlashCallback( const CEffectData &data )
  19. {
  20. CSmartPtr<CLocalSpaceEmitter> pEmitter =
  21. CLocalSpaceEmitter::Create( "CS_MuzzleFlash", data.m_hEntity, data.m_nAttachmentIndex, 0 );
  22. if ( !pEmitter )
  23. return;
  24. // SetBBox() manually on the particle system so it doesn't have to be recalculated more than once.
  25. Vector vCenter( 0.0f, 0.0f, 0.0f );
  26. C_BaseEntity *pEnt = data.GetEntity();
  27. if ( pEnt )
  28. {
  29. vCenter = pEnt->WorldSpaceCenter();
  30. }
  31. else
  32. {
  33. IClientRenderable *pRenderable = data.GetRenderable( );
  34. if ( pRenderable )
  35. {
  36. Vector vecMins, vecMaxs;
  37. pRenderable->GetRenderBoundsWorldspace( vecMins, vecMaxs );
  38. VectorAdd( vecMins, vecMaxs, vCenter );
  39. vCenter *= 0.5f;
  40. }
  41. }
  42. Assert( pEmitter );
  43. pEmitter->GetBinding().SetBBox( vCenter - Vector( 3, 3, 3 ), vCenter + Vector( 3, 3, 3 ) );
  44. // haxors - make the clip much shorter so the alpha is not
  45. // changed based on large clip distances
  46. pEmitter->SetNearClip( 0, 5 );
  47. PMaterialHandle hFlashMaterial = pEmitter->GetPMaterial( "sprites/muzzleflash4" );
  48. for( int i=0;i<3;i++ )
  49. {
  50. SimpleParticle *pParticle = (SimpleParticle *)pEmitter->AddParticle( sizeof( SimpleParticle ),
  51. hFlashMaterial,
  52. vec3_origin );
  53. if( pParticle )
  54. {
  55. pParticle->m_flLifetime = 0.0f;
  56. pParticle->m_flDieTime = 0.08f;
  57. pParticle->m_vecVelocity = vec3_origin;
  58. pParticle->m_uchColor[0] = 255;
  59. pParticle->m_uchColor[1] = 255;
  60. pParticle->m_uchColor[2] = 255;
  61. pParticle->m_uchStartAlpha = 80;
  62. pParticle->m_uchEndAlpha = 30;
  63. pParticle->m_uchStartSize = ( 3.0 + 3.0*i ) * data.m_flScale;
  64. pParticle->m_uchEndSize = pParticle->m_uchStartSize * 0.8;
  65. pParticle->m_flRoll = random->RandomInt( 0, 3 );
  66. pParticle->m_flRollDelta = 0.0f;
  67. }
  68. }
  69. // dynamic light temporary entity for the muzzle flash
  70. CPVSFilter filter(pEmitter->GetSortOrigin());
  71. TE_DynamicLight( filter, 0.0, &(pEmitter->GetSortOrigin()), 255, 192, 64, 5, 70, 0.05, 768 );
  72. }
  73. DECLARE_CLIENT_EFFECT( "CS_MuzzleFlash", CS_MuzzleFlashCallback );
  74. // 'X' shaped muzzleflash used by certain weapons
  75. void CS_MuzzleFlashXCallback( const CEffectData &data )
  76. {
  77. CSmartPtr<CLocalSpaceEmitter> pEmitter =
  78. CLocalSpaceEmitter::Create( "CS_MuzzleFlashX", data.m_hEntity, data.m_nAttachmentIndex, 0 );
  79. Assert( pEmitter );
  80. // haxors - make the clip much shorter so the alpha is not
  81. // changed based on large clip distances
  82. pEmitter->SetNearClip( 0, 5 );
  83. PMaterialHandle hFlashMaterial = pEmitter->GetPMaterial( "effects/muzzleflashX" );
  84. SimpleParticle *pParticle = (SimpleParticle *)pEmitter->AddParticle( sizeof( SimpleParticle ),
  85. hFlashMaterial,
  86. vec3_origin );
  87. if( pParticle )
  88. {
  89. pParticle->m_flLifetime = 0.0f;
  90. pParticle->m_flDieTime = 0.08f;
  91. pParticle->m_vecVelocity = vec3_origin;
  92. pParticle->m_uchColor[0] = 255;
  93. pParticle->m_uchColor[1] = 255;
  94. pParticle->m_uchColor[2] = 255;
  95. pParticle->m_uchStartAlpha = 130;
  96. pParticle->m_uchEndAlpha = 80;
  97. pParticle->m_uchStartSize = 6.0f * data.m_flScale * random->RandomFloat( 0.9, 1.1 );
  98. pParticle->m_uchEndSize = pParticle->m_uchStartSize * 0.8;
  99. pParticle->m_flRoll = random->RandomFloat( -0.25, 0.25 );
  100. pParticle->m_flRollDelta = 0.0f;
  101. }
  102. // dynamic light temporary entity for the muzzle flash
  103. CPVSFilter filter(pEmitter->GetSortOrigin());
  104. TE_DynamicLight( filter, 0.0, &(pEmitter->GetSortOrigin()), 255, 192, 64, 5, 70, 0.05, 768 );
  105. }
  106. DECLARE_CLIENT_EFFECT( "CS_MuzzleFlash_X", CS_MuzzleFlashXCallback );