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.

249 lines
6.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "fx_quad.h"
  8. #include "fx.h"
  9. class C_MortarShell : public C_BaseEntity
  10. {
  11. public:
  12. DECLARE_CLASS( C_MortarShell, C_BaseEntity );
  13. DECLARE_CLIENTCLASS();
  14. void OnDataChanged( DataUpdateType_t updateType );
  15. int DrawModel( int flags );
  16. RenderGroup_t GetRenderGroup( void ) { return RENDER_GROUP_TRANSLUCENT_ENTITY; }
  17. private:
  18. void AddRisingParticles( float flPerc );
  19. void AddExplodingParticles( float flPerc );
  20. inline float GetStartPerc( void );
  21. inline float GetEndPerc( void );
  22. CSmartPtr<CSimpleEmitter> m_pEmitter;
  23. TimedEvent m_ParticleEvent;
  24. float m_flLifespan;
  25. float m_flRadius;
  26. float m_flStarttime;
  27. Vector m_vecSurfaceNormal;
  28. };
  29. IMPLEMENT_CLIENTCLASS_DT( C_MortarShell, DT_MortarShell, CMortarShell )
  30. RecvPropFloat( RECVINFO( m_flLifespan ) ),
  31. RecvPropFloat( RECVINFO( m_flRadius ) ),
  32. RecvPropVector( RECVINFO( m_vecSurfaceNormal ) ),
  33. END_RECV_TABLE()
  34. //-----------------------------------------------------------------------------
  35. // Purpose:
  36. // Input : updateType -
  37. //-----------------------------------------------------------------------------
  38. void C_MortarShell::OnDataChanged( DataUpdateType_t updateType )
  39. {
  40. BaseClass::OnDataChanged( updateType );
  41. if ( updateType == DATA_UPDATE_CREATED )
  42. {
  43. m_flStarttime = gpGlobals->curtime;
  44. AddToLeafSystem( RENDER_GROUP_TRANSLUCENT_ENTITY );
  45. m_pEmitter = CSimpleEmitter::Create( "C_EntityDissolve" );
  46. m_pEmitter->SetSortOrigin( GetAbsOrigin() );
  47. m_ParticleEvent.Init( 128 );
  48. }
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose:
  52. // Input : flPerc -
  53. //-----------------------------------------------------------------------------
  54. void C_MortarShell::AddRisingParticles( float flPerc )
  55. {
  56. SimpleParticle *sParticle;
  57. Vector offset;
  58. float radius = m_flRadius * 0.25f * flPerc;
  59. float val = RemapValClamped( gpGlobals->curtime, m_flStarttime, m_flStarttime + m_flLifespan, 0.0f, 1.0f );
  60. float flCur = gpGlobals->frametime;
  61. // Anime ground effects
  62. while ( m_ParticleEvent.NextEvent( flCur ) )
  63. {
  64. offset.x = random->RandomFloat( -radius, radius );
  65. offset.y = random->RandomFloat( -radius, radius );
  66. offset.z = random->RandomFloat( -8.0f, 8.0f );
  67. offset += GetAbsOrigin();
  68. sParticle = (SimpleParticle *) m_pEmitter->AddParticle( sizeof(SimpleParticle), m_pEmitter->GetPMaterial( "effects/spark" ), offset );
  69. if ( sParticle == NULL )
  70. return;
  71. sParticle->m_vecVelocity = Vector( Helper_RandomFloat( -4.0f, 4.0f ), Helper_RandomFloat( -4.0f, 4.0f ), Helper_RandomFloat( 32.0f, 256.0f ) * Bias( val, 0.25f ) );
  72. sParticle->m_uchStartSize = random->RandomFloat( 4, 8 ) * flPerc;
  73. sParticle->m_flDieTime = random->RandomFloat( 0.5f, 1.0f );
  74. sParticle->m_flLifetime = 0.0f;
  75. sParticle->m_flRoll = Helper_RandomInt( 0, 360 );
  76. float alpha = 255 * flPerc;
  77. sParticle->m_flRollDelta = Helper_RandomFloat( -8.0f * flPerc, 8.0f * flPerc );
  78. sParticle->m_uchColor[0] = alpha;
  79. sParticle->m_uchColor[1] = alpha;
  80. sParticle->m_uchColor[2] = alpha;
  81. sParticle->m_uchStartAlpha = alpha;
  82. sParticle->m_uchEndAlpha = 0;
  83. sParticle->m_uchEndSize = 0;
  84. }
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose:
  88. // Input : flPerc -
  89. //-----------------------------------------------------------------------------
  90. void C_MortarShell::AddExplodingParticles( float flPerc )
  91. {
  92. SimpleParticle *sParticle;
  93. Vector offset;
  94. float radius = 48.0f * flPerc;
  95. float flCur = gpGlobals->frametime;
  96. // Anime ground effects
  97. while ( m_ParticleEvent.NextEvent( flCur ) )
  98. {
  99. offset.x = random->RandomFloat( -radius, radius );
  100. offset.y = random->RandomFloat( -radius, radius );
  101. offset.z = random->RandomFloat( -8.0f, 8.0f );
  102. offset += GetAbsOrigin();
  103. sParticle = (SimpleParticle *) m_pEmitter->AddParticle( sizeof(SimpleParticle), m_pEmitter->GetPMaterial( "effects/spark" ), offset );
  104. if ( sParticle == NULL )
  105. return;
  106. sParticle->m_vecVelocity = RandomVector( -1.0f, 1.0f ) + Vector( 0, 0, 1 );
  107. sParticle->m_vecVelocity *= ( 750.0f * flPerc );
  108. sParticle->m_uchStartSize = random->RandomFloat( 2, 4 ) * flPerc;
  109. sParticle->m_flDieTime = random->RandomFloat( 0.25f, 0.5f );
  110. sParticle->m_flLifetime = 0.0f;
  111. sParticle->m_flRoll = Helper_RandomInt( 0, 360 );
  112. float alpha = 255 * flPerc;
  113. sParticle->m_flRollDelta = Helper_RandomFloat( -8.0f * flPerc, 8.0f * flPerc );
  114. sParticle->m_uchColor[0] = alpha;
  115. sParticle->m_uchColor[1] = alpha;
  116. sParticle->m_uchColor[2] = alpha;
  117. sParticle->m_uchStartAlpha = alpha;
  118. sParticle->m_uchEndAlpha = 0;
  119. sParticle->m_uchEndSize = 0;
  120. }
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose:
  124. // Output : inline float
  125. //-----------------------------------------------------------------------------
  126. inline float C_MortarShell::GetStartPerc( void )
  127. {
  128. float val = RemapValClamped( gpGlobals->curtime, m_flStarttime, m_flStarttime + m_flLifespan, 0.0f, 1.0f );
  129. return ( Gain( val, 0.2f ) );
  130. }
  131. //-----------------------------------------------------------------------------
  132. // Purpose:
  133. // Output : inline float
  134. //-----------------------------------------------------------------------------
  135. inline float C_MortarShell::GetEndPerc( void )
  136. {
  137. float val = RemapValClamped( gpGlobals->curtime, m_flStarttime + m_flLifespan, m_flStarttime + m_flLifespan + 1.0f, 1.0f, 0.0f );
  138. return ( Gain( val, 0.75f ) );
  139. }
  140. #define ALPHA_MIN 0.0f
  141. #define ALPHA_MAX 1.0f
  142. #define SCALE_MIN 8
  143. #define SCALE_MAX 200
  144. //-----------------------------------------------------------------------------
  145. // Purpose:
  146. //-----------------------------------------------------------------------------
  147. int C_MortarShell::DrawModel( int flags )
  148. {
  149. if ( gpGlobals->frametime <= 0.0f )
  150. return 0;
  151. float flPerc;
  152. bool ending;
  153. // See if we're in the beginning phase
  154. if ( gpGlobals->curtime < ( m_flStarttime + m_flLifespan ) )
  155. {
  156. flPerc = GetStartPerc();
  157. ending = false;
  158. }
  159. else
  160. {
  161. flPerc = GetEndPerc();
  162. ending = true;
  163. }
  164. float flAlpha = ALPHA_MIN + ( ( ALPHA_MAX - ALPHA_MIN ) * flPerc );
  165. float flScale = ( ending ) ? m_flRadius : ( (m_flRadius*0.1f)+ ( ( m_flRadius - (m_flRadius*0.1f) ) * flPerc ) );
  166. // Do the ground effect
  167. FX_AddQuad( GetAbsOrigin() + ( m_vecSurfaceNormal * 2.0f ),
  168. m_vecSurfaceNormal,
  169. flScale,
  170. flScale,
  171. 1.0f,
  172. flAlpha,
  173. flAlpha,
  174. 1.0f,
  175. 0,
  176. 0,
  177. Vector( 1.0f, 1.0f, 1.0f ),
  178. 0.0001f,
  179. "effects/combinemuzzle2_nocull",
  180. 0 );
  181. if ( !ending )
  182. {
  183. // Add extra effects on startup
  184. AddRisingParticles( flPerc );
  185. }
  186. else
  187. {
  188. // Add exploding particles after the impact
  189. AddExplodingParticles( flPerc );
  190. }
  191. return 1;
  192. }