Counter Strike : Global Offensive Source Code
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.

223 lines
6.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A point entity that periodically emits sparks and "bzzt" sounds.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "IEffects.h"
  9. #include "engine/IEngineSound.h"
  10. #include "particle_parse.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. ConVar fx_new_sparks ( "fx_new_sparks", "1", FCVAR_CHEAT, "Use new style sparks.\n" );
  14. //-----------------------------------------------------------------------------
  15. // Purpose: Emits sparks from the given location and plays a random spark sound.
  16. // Input : pev -
  17. // location -
  18. //-----------------------------------------------------------------------------
  19. void DoSpark( CBaseEntity *ent, const Vector &location, int nMagnitude, int nTrailLength, bool bPlaySound, const Vector &vecDir )
  20. {
  21. g_pEffects->Sparks( location, nMagnitude, nTrailLength, &vecDir );
  22. }
  23. const int SF_SPARK_START_ON = 64;
  24. const int SF_SPARK_GLOW = 128;
  25. const int SF_SPARK_SILENT = 256;
  26. const int SF_SPARK_DIRECTIONAL = 512;
  27. class CEnvSpark : public CPointEntity
  28. {
  29. DECLARE_CLASS( CEnvSpark, CPointEntity );
  30. public:
  31. CEnvSpark( void );
  32. void Spawn(void);
  33. void Precache(void);
  34. void SparkThink(void);
  35. // Input handlers
  36. void InputStartSpark( inputdata_t &inputdata );
  37. void InputStopSpark( inputdata_t &inputdata );
  38. void InputToggleSpark( inputdata_t &inputdata );
  39. void InputSparkOnce( inputdata_t &inputdata );
  40. DECLARE_DATADESC();
  41. float m_flDelay;
  42. int m_nGlowSpriteIndex;
  43. int m_nMagnitude;
  44. int m_nTrailLength;
  45. COutputEvent m_OnSpark;
  46. };
  47. BEGIN_DATADESC( CEnvSpark )
  48. DEFINE_KEYFIELD( m_flDelay, FIELD_FLOAT, "MaxDelay" ),
  49. DEFINE_FIELD( m_nGlowSpriteIndex, FIELD_INTEGER ),
  50. DEFINE_KEYFIELD( m_nMagnitude, FIELD_INTEGER, "Magnitude" ),
  51. DEFINE_KEYFIELD( m_nTrailLength, FIELD_INTEGER, "TrailLength" ),
  52. // Function Pointers
  53. DEFINE_FUNCTION( SparkThink ),
  54. DEFINE_INPUTFUNC( FIELD_VOID, "StartSpark", InputStartSpark ),
  55. DEFINE_INPUTFUNC( FIELD_VOID, "StopSpark", InputStopSpark ),
  56. DEFINE_INPUTFUNC( FIELD_VOID, "ToggleSpark", InputToggleSpark ),
  57. DEFINE_INPUTFUNC( FIELD_VOID, "SparkOnce", InputSparkOnce ),
  58. DEFINE_OUTPUT( m_OnSpark, "OnSpark" ),
  59. END_DATADESC()
  60. LINK_ENTITY_TO_CLASS(env_spark, CEnvSpark);
  61. //-----------------------------------------------------------------------------
  62. // Purpose: Constructor! Exciting, isn't it?
  63. //-----------------------------------------------------------------------------
  64. CEnvSpark::CEnvSpark( void )
  65. {
  66. m_nMagnitude = 1;
  67. m_nTrailLength = 1;
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Purpose: Called when spawning, after keyvalues have been handled.
  71. //-----------------------------------------------------------------------------
  72. void CEnvSpark::Spawn(void)
  73. {
  74. SetThink( NULL );
  75. SetUse( NULL );
  76. if (FBitSet(m_spawnflags, SF_SPARK_START_ON))
  77. {
  78. SetThink(&CEnvSpark::SparkThink); // start sparking
  79. }
  80. SetNextThink( gpGlobals->curtime + 0.1 + random->RandomFloat( 0, 1.5 ) );
  81. // Negative delays are not allowed
  82. if (m_flDelay < 0)
  83. {
  84. m_flDelay = 0;
  85. }
  86. Precache( );
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Purpose:
  90. //-----------------------------------------------------------------------------
  91. void CEnvSpark::Precache(void)
  92. {
  93. // Unlock string tables to avoid an engine warning
  94. bool oldLock = engine->LockNetworkStringTables( false );
  95. m_nGlowSpriteIndex = PrecacheModel("sprites/glow01.vmt");
  96. engine->LockNetworkStringTables( oldLock );
  97. if ( IsPrecacheAllowed() )
  98. {
  99. PrecacheScriptSound( "DoSpark" );
  100. PrecacheParticleSystem( "env_sparks_omni" );
  101. PrecacheParticleSystem( "env_sparks_directional" );
  102. }
  103. }
  104. extern ConVar phys_pushscale;
  105. //-----------------------------------------------------------------------------
  106. // Purpose: Emits sparks at random intervals.
  107. //-----------------------------------------------------------------------------
  108. void CEnvSpark::SparkThink(void)
  109. {
  110. SetNextThink( gpGlobals->curtime + 0.1 + random->RandomFloat(0, m_flDelay) );
  111. m_OnSpark.FireOutput( this, this );
  112. if ( !( m_spawnflags & SF_SPARK_SILENT ) )
  113. {
  114. EmitSound( "DoSpark" );
  115. }
  116. if ( fx_new_sparks.GetBool() )
  117. {
  118. if ( FBitSet( m_spawnflags, SF_SPARK_DIRECTIONAL ) )
  119. DispatchParticleEffect( "env_sparks_directional", WorldSpaceCenter(), Vector ( m_nMagnitude, m_nTrailLength, FBitSet(m_spawnflags, SF_SPARK_GLOW) ), GetAbsAngles() );
  120. else
  121. DispatchParticleEffect( "env_sparks_omni", WorldSpaceCenter(), Vector ( m_nMagnitude, m_nTrailLength, FBitSet(m_spawnflags, SF_SPARK_GLOW) ), GetAbsAngles() );
  122. }
  123. else
  124. {
  125. Vector vecDir = vec3_origin;
  126. if ( FBitSet( m_spawnflags, SF_SPARK_DIRECTIONAL ) )
  127. {
  128. AngleVectors( GetAbsAngles(), &vecDir );
  129. }
  130. DoSpark( this, WorldSpaceCenter(), m_nMagnitude, m_nTrailLength, !( m_spawnflags & SF_SPARK_SILENT ), vecDir );
  131. if (FBitSet(m_spawnflags, SF_SPARK_GLOW))
  132. {
  133. CPVSFilter filter( GetAbsOrigin() );
  134. te->GlowSprite( filter, 0.0, &GetAbsOrigin(), m_nGlowSpriteIndex, 0.2, 1.5, 25 );
  135. }
  136. }
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Purpose: Input handler for starting the sparks.
  140. //-----------------------------------------------------------------------------
  141. void CEnvSpark::InputStartSpark( inputdata_t &inputdata )
  142. {
  143. SetThink(&CEnvSpark::SparkThink);
  144. SetNextThink( gpGlobals->curtime );
  145. }
  146. //-----------------------------------------------------------------------------
  147. // Purpose: Shoot one spark.
  148. //-----------------------------------------------------------------------------
  149. void CEnvSpark::InputSparkOnce( inputdata_t &inputdata )
  150. {
  151. SparkThink();
  152. SetNextThink( TICK_NEVER_THINK );
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose: Input handler for starting the sparks.
  156. //-----------------------------------------------------------------------------
  157. void CEnvSpark::InputStopSpark( inputdata_t &inputdata )
  158. {
  159. SetThink(NULL);
  160. }
  161. //-----------------------------------------------------------------------------
  162. // Purpose: Input handler for toggling the on/off state of the sparks.
  163. //-----------------------------------------------------------------------------
  164. void CEnvSpark::InputToggleSpark( inputdata_t &inputdata )
  165. {
  166. if (GetNextThink() == TICK_NEVER_THINK)
  167. {
  168. InputStartSpark( inputdata );
  169. }
  170. else
  171. {
  172. InputStopSpark( inputdata );
  173. }
  174. }