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.

194 lines
5.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A simple test entity for creating special effects
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "c_te_effect_dispatch.h"
  8. #include "particles_simple.h"
  9. // Declare the sparkler entity for the client-side
  10. class C_Sparkler : public C_BaseEntity
  11. {
  12. public:
  13. DECLARE_CLIENTCLASS();
  14. DECLARE_CLASS( C_Sparkler, C_BaseEntity );
  15. virtual void OnDataChanged( DataUpdateType_t updateType ); // Called when data changes on the server
  16. virtual void ClientThink( void ); // Client-side think function for the entity
  17. private:
  18. bool m_bEmit; // Determines whether or not we should emit particles
  19. float m_flScale; // Size of the effect
  20. CSmartPtr<CSimpleEmitter> m_hEmitter; // Particle emitter for this entity
  21. PMaterialHandle m_hMaterial; // Material handle used for this entity's particles
  22. TimedEvent m_tParticleTimer; // Timer used to control particle emission rate
  23. };
  24. // Declare the data-table for server/client communication
  25. IMPLEMENT_CLIENTCLASS_DT( C_Sparkler, DT_Sparkler, CSparkler )
  26. RecvPropInt( RECVINFO( m_bEmit ) ), // Boolean state from the server
  27. RecvPropFloat( RECVINFO( m_flScale ) ),
  28. END_RECV_TABLE()
  29. //-----------------------------------------------------------------------------
  30. // Purpose: Called when data changes on the server
  31. //-----------------------------------------------------------------------------
  32. void C_Sparkler::OnDataChanged( DataUpdateType_t updateType )
  33. {
  34. // NOTE: We MUST call the base classes' implementation of this function
  35. BaseClass::OnDataChanged( updateType );
  36. // Setup our entity's particle system on creation
  37. if ( updateType == DATA_UPDATE_CREATED )
  38. {
  39. // Creat the emitter
  40. m_hEmitter = CSimpleEmitter::Create( "env_sparkler" );
  41. // Obtain a reference handle to our particle's desired material
  42. if ( m_hEmitter.IsValid() )
  43. {
  44. m_hMaterial = m_hEmitter->GetPMaterial( "effects/yellowflare" );
  45. }
  46. // Spawn 128 particles per second
  47. m_tParticleTimer.Init( 128 );
  48. // Call our ClientThink() function once every client frame
  49. SetNextClientThink( CLIENT_THINK_ALWAYS );
  50. }
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Purpose: Client-side think function for the entity
  54. //-----------------------------------------------------------------------------
  55. void C_Sparkler::ClientThink( void )
  56. {
  57. // We must have a valid emitter
  58. if ( m_hEmitter == NULL )
  59. return;
  60. // We must be allowed to emit particles by the server
  61. if ( m_bEmit == false )
  62. return;
  63. SimpleParticle *pParticle;
  64. float curTime = gpGlobals->frametime;
  65. // Add as many particles as required this frame
  66. while ( m_tParticleTimer.NextEvent( curTime ) )
  67. {
  68. // Create the particle
  69. pParticle = m_hEmitter->AddSimpleParticle( m_hMaterial, GetAbsOrigin() );
  70. if ( pParticle == NULL )
  71. return;
  72. // Setup our size
  73. pParticle->m_uchStartSize = (unsigned char) m_flScale;
  74. pParticle->m_uchEndSize = 0;
  75. // Setup our roll
  76. pParticle->m_flRoll = random->RandomFloat( 0, 2*M_PI );
  77. pParticle->m_flRollDelta = random->RandomFloat( -DEG2RAD( 180 ), DEG2RAD( 180 ) );
  78. // Set our color
  79. pParticle->m_uchColor[0] = 255;
  80. pParticle->m_uchColor[1] = 255;
  81. pParticle->m_uchColor[2] = 255;
  82. // Setup our alpha values
  83. pParticle->m_uchStartAlpha = 255;
  84. pParticle->m_uchEndAlpha = 255;
  85. // Obtain a random direction
  86. Vector velocity = RandomVector( -1.0f, 1.0f );
  87. VectorNormalize( velocity );
  88. // Obtain a random speed
  89. float speed = random->RandomFloat( 4.0f, 8.0f ) * m_flScale;
  90. // Set our velocity
  91. pParticle->m_vecVelocity = velocity * speed;
  92. // Die in a short range of time
  93. pParticle->m_flDieTime = random->RandomFloat( 0.25f, 0.5f );
  94. }
  95. }
  96. // ============================================================================
  97. //
  98. // Dispatch Effect version
  99. //
  100. // ============================================================================
  101. //-----------------------------------------------------------------------------
  102. // Purpose: Callback to create a sparkle effect on the client
  103. // Input : &data - information about the effect
  104. //-----------------------------------------------------------------------------
  105. void SparkleCallback( const CEffectData &data )
  106. {
  107. // Create a simple particle emitter
  108. CSmartPtr<CSimpleEmitter> pSparkleEmitter = CSimpleEmitter::Create( "Sparkle" );
  109. if ( pSparkleEmitter == NULL )
  110. return;
  111. // Make local versions of our passed in data
  112. Vector origin = data.m_vOrigin;
  113. float scale = data.m_flScale;
  114. // Set our sort origin to make the system cull properly
  115. pSparkleEmitter->SetSortOrigin( origin );
  116. // Find the material handle we wish to use for these particles
  117. PMaterialHandle hMaterial = pSparkleEmitter->GetPMaterial( "effects/yellowflare" );
  118. SimpleParticle *pParticle;
  119. // Make a group of particles in the world
  120. for ( int i = 0; i < 64; i++ )
  121. {
  122. // Create a particle
  123. pParticle = pSparkleEmitter->AddSimpleParticle( hMaterial, origin );
  124. if ( pParticle == NULL )
  125. return;
  126. // Set our sizes
  127. pParticle->m_uchStartSize = (unsigned char) scale;
  128. pParticle->m_uchEndSize = 0;
  129. // Set our roll
  130. pParticle->m_flRoll = random->RandomFloat( 0, 2*M_PI );
  131. pParticle->m_flRollDelta = random->RandomFloat( -DEG2RAD( 180 ), DEG2RAD( 180 ) );
  132. // Set our color
  133. pParticle->m_uchColor[0] = 255; // Red
  134. pParticle->m_uchColor[1] = 255; // Green
  135. pParticle->m_uchColor[2] = 255; // Blue
  136. // Set our alpha
  137. pParticle->m_uchStartAlpha = 0;
  138. pParticle->m_uchEndAlpha = 255;
  139. // Create a random vector
  140. Vector velocity = RandomVector( -1.0f, 1.0f );
  141. VectorNormalize( velocity );
  142. // Find a random speed for the particle
  143. float speed = random->RandomFloat( 4.0f, 8.0f ) * scale;
  144. // Build and set the velocity of the particle
  145. pParticle->m_vecVelocity = velocity * speed;
  146. // Declare our lifetime
  147. pParticle->m_flDieTime = 1.0f;
  148. }
  149. }
  150. // This links our server-side call to a client-side function
  151. DECLARE_CLIENT_EFFECT( "Sparkle", SparkleCallback );