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.

131 lines
4.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "engine/IEngineTrace.h"
  9. #include "fx_sparks.h"
  10. #include "particles_ez.h"
  11. #include "view.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. ConVar cl_starfield_diameter( "cl_starfield_diameter", "128.0", FCVAR_NONE );
  15. ConVar cl_starfield_distance( "cl_starfield_distance", "256.0", FCVAR_NONE );
  16. //-----------------------------------------------------------------------------
  17. // Purpose:
  18. //-----------------------------------------------------------------------------
  19. class C_EnvStarfield : public C_BaseEntity
  20. {
  21. DECLARE_CLASS( C_EnvStarfield, C_BaseEntity );
  22. public:
  23. DECLARE_CLIENTCLASS();
  24. C_EnvStarfield();
  25. virtual void OnDataChanged( DataUpdateType_t updateType );
  26. virtual void ClientThink( void );
  27. private:
  28. // Emitter
  29. CSmartPtr<CTrailParticles> m_pEmitter;
  30. bool m_bOn;
  31. float m_flDensity;
  32. float m_flNumParticles;
  33. private:
  34. C_EnvStarfield( const C_EnvStarfield & );
  35. };
  36. IMPLEMENT_CLIENTCLASS_DT( C_EnvStarfield, DT_EnvStarfield, CEnvStarfield )
  37. RecvPropInt( RECVINFO(m_bOn) ),
  38. RecvPropFloat( RECVINFO(m_flDensity) ),
  39. END_RECV_TABLE()
  40. // ------------------------------------------------------------------------- //
  41. // C_EnvStarfield
  42. // ------------------------------------------------------------------------- //
  43. C_EnvStarfield::C_EnvStarfield()
  44. {
  45. m_bOn = false;
  46. m_flDensity = 1.0;
  47. m_flNumParticles = 0;
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose:
  51. // Input : updateType -
  52. //-----------------------------------------------------------------------------
  53. void C_EnvStarfield::OnDataChanged( DataUpdateType_t updateType )
  54. {
  55. if ( updateType == DATA_UPDATE_CREATED )
  56. {
  57. m_pEmitter = CTrailParticles::Create( "EnvStarfield" );
  58. Vector vecCenter = MainViewOrigin() + (MainViewForward() * cl_starfield_distance.GetFloat() );
  59. m_pEmitter->Setup( (Vector &) vecCenter,
  60. NULL,
  61. 0.0,
  62. 0,
  63. 64,
  64. 0,
  65. 0,
  66. bitsPARTICLE_TRAIL_VELOCITY_DAMPEN | bitsPARTICLE_TRAIL_FADE | bitsPARTICLE_TRAIL_FADE_IN );
  67. // Start thinking
  68. SetNextClientThink( CLIENT_THINK_ALWAYS );
  69. }
  70. BaseClass::OnDataChanged( updateType );
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose:
  74. //-----------------------------------------------------------------------------
  75. void C_EnvStarfield::ClientThink( void )
  76. {
  77. if ( !m_bOn || !m_flDensity )
  78. return;
  79. PMaterialHandle hParticleMaterial = m_pEmitter->GetPMaterial( "effects/spark_noz" );
  80. // Find a start & end point for the particle
  81. // Start particles straight ahead of the client
  82. Vector vecViewOrigin = MainViewOrigin();
  83. // Determine the number of particles
  84. m_flNumParticles += 1.0 * (m_flDensity);
  85. int iNumParticles = floor(m_flNumParticles);
  86. m_flNumParticles -= iNumParticles;
  87. // Add particles
  88. for ( int i = 0; i < iNumParticles; i++ )
  89. {
  90. float flDiameter = cl_starfield_diameter.GetFloat();
  91. Vector vecStart = vecViewOrigin + (MainViewForward() * cl_starfield_distance.GetFloat() );
  92. Vector vecEnd = vecViewOrigin + (MainViewRight() * RandomFloat(-flDiameter,flDiameter)) + (MainViewUp() * RandomFloat(-flDiameter,flDiameter));
  93. Vector vecDir = (vecEnd - vecStart);
  94. float flDistance = VectorNormalize( vecDir );
  95. float flTravelTime = 2.0;
  96. // Start a random amount along the path
  97. vecStart += vecDir * ( RandomFloat(0.1,0.3) * flDistance );
  98. TrailParticle *pParticle = (TrailParticle *) m_pEmitter->AddParticle( sizeof(TrailParticle), hParticleMaterial, vecStart );
  99. if ( pParticle )
  100. {
  101. pParticle->m_vecVelocity = vecDir * (flDistance / flTravelTime);
  102. pParticle->m_flDieTime = flTravelTime;
  103. pParticle->m_flLifetime = 0;
  104. pParticle->m_flWidth = RandomFloat( 1, 3 );
  105. pParticle->m_flLength = RandomFloat( 0.05, 0.4 );
  106. pParticle->m_color.r = 255;
  107. pParticle->m_color.g = 255;
  108. pParticle->m_color.b = 255;
  109. pParticle->m_color.a = 255;
  110. }
  111. }
  112. }