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.

250 lines
6.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A special kind of beam effect that traces from its start position to
  4. // its end position and stops if it hits anything.
  5. //
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "EnvLaser.h"
  10. #include "Sprite.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. LINK_ENTITY_TO_CLASS( env_laser, CEnvLaser );
  14. BEGIN_DATADESC( CEnvLaser )
  15. DEFINE_KEYFIELD( m_iszLaserTarget, FIELD_STRING, "LaserTarget" ),
  16. DEFINE_FIELD( m_pSprite, FIELD_CLASSPTR ),
  17. DEFINE_KEYFIELD( m_iszSpriteName, FIELD_STRING, "EndSprite" ),
  18. DEFINE_FIELD( m_firePosition, FIELD_VECTOR ),
  19. DEFINE_KEYFIELD( m_flStartFrame, FIELD_FLOAT, "framestart" ),
  20. // Function Pointers
  21. DEFINE_FUNCTION( StrikeThink ),
  22. // Input functions
  23. DEFINE_INPUTFUNC( FIELD_VOID, "TurnOn", InputTurnOn ),
  24. DEFINE_INPUTFUNC( FIELD_VOID, "TurnOff", InputTurnOff ),
  25. DEFINE_INPUTFUNC( FIELD_VOID, "Toggle", InputToggle ),
  26. END_DATADESC()
  27. //-----------------------------------------------------------------------------
  28. // Purpose:
  29. //-----------------------------------------------------------------------------
  30. void CEnvLaser::Spawn( void )
  31. {
  32. if ( !GetModelName() )
  33. {
  34. SetThink( &CEnvLaser::SUB_Remove );
  35. return;
  36. }
  37. SetSolid( SOLID_NONE ); // Remove model & collisions
  38. SetThink( &CEnvLaser::StrikeThink );
  39. SetEndWidth( GetWidth() ); // Note: EndWidth is not scaled
  40. PointsInit( GetLocalOrigin(), GetLocalOrigin() );
  41. Precache( );
  42. if ( !m_pSprite && m_iszSpriteName != NULL_STRING )
  43. {
  44. m_pSprite = CSprite::SpriteCreate( STRING(m_iszSpriteName), GetAbsOrigin(), TRUE );
  45. }
  46. else
  47. {
  48. m_pSprite = NULL;
  49. }
  50. if ( m_pSprite )
  51. {
  52. m_pSprite->SetParent( GetMoveParent() );
  53. m_pSprite->SetTransparency( kRenderGlow, m_clrRender->r, m_clrRender->g, m_clrRender->b, m_clrRender->a, m_nRenderFX );
  54. }
  55. if ( GetEntityName() != NULL_STRING && !(m_spawnflags & SF_BEAM_STARTON) )
  56. {
  57. TurnOff();
  58. }
  59. else
  60. {
  61. TurnOn();
  62. }
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Purpose:
  66. //-----------------------------------------------------------------------------
  67. void CEnvLaser::Precache( void )
  68. {
  69. SetModelIndex( PrecacheModel( STRING( GetModelName() ) ) );
  70. if ( m_iszSpriteName != NULL_STRING )
  71. PrecacheModel( STRING(m_iszSpriteName) );
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose:
  75. //-----------------------------------------------------------------------------
  76. bool CEnvLaser::KeyValue( const char *szKeyName, const char *szValue )
  77. {
  78. if (FStrEq(szKeyName, "width"))
  79. {
  80. SetWidth( atof(szValue) );
  81. }
  82. else if (FStrEq(szKeyName, "NoiseAmplitude"))
  83. {
  84. SetNoise( atoi(szValue) );
  85. }
  86. else if (FStrEq(szKeyName, "TextureScroll"))
  87. {
  88. SetScrollRate( atoi(szValue) );
  89. }
  90. else if (FStrEq(szKeyName, "texture"))
  91. {
  92. SetModelName( AllocPooledString(szValue) );
  93. }
  94. else
  95. {
  96. BaseClass::KeyValue( szKeyName, szValue );
  97. }
  98. return true;
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Purpose: Returns whether the laser is currently active.
  102. //-----------------------------------------------------------------------------
  103. int CEnvLaser::IsOn( void )
  104. {
  105. if ( IsEffectActive( EF_NODRAW ) )
  106. return 0;
  107. return 1;
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Purpose:
  111. //-----------------------------------------------------------------------------
  112. void CEnvLaser::InputTurnOn( inputdata_t &inputdata )
  113. {
  114. if (!IsOn())
  115. {
  116. TurnOn();
  117. }
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose:
  121. //-----------------------------------------------------------------------------
  122. void CEnvLaser::InputTurnOff( inputdata_t &inputdata )
  123. {
  124. if (IsOn())
  125. {
  126. TurnOff();
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Purpose:
  131. //-----------------------------------------------------------------------------
  132. void CEnvLaser::InputToggle( inputdata_t &inputdata )
  133. {
  134. if ( IsOn() )
  135. {
  136. TurnOff();
  137. }
  138. else
  139. {
  140. TurnOn();
  141. }
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Purpose:
  145. //-----------------------------------------------------------------------------
  146. void CEnvLaser::TurnOff( void )
  147. {
  148. AddEffects( EF_NODRAW );
  149. if ( m_pSprite )
  150. m_pSprite->TurnOff();
  151. SetNextThink( TICK_NEVER_THINK );
  152. SetThink( NULL );
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose:
  156. //-----------------------------------------------------------------------------
  157. void CEnvLaser::TurnOn( void )
  158. {
  159. RemoveEffects( EF_NODRAW );
  160. if ( m_pSprite )
  161. m_pSprite->TurnOn();
  162. m_flFireTime = gpGlobals->curtime;
  163. SetThink( &CEnvLaser::StrikeThink );
  164. //
  165. // Call StrikeThink here to update the end position, otherwise we will see
  166. // the beam in the wrong place for one frame since we cleared the nodraw flag.
  167. //
  168. StrikeThink();
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Purpose:
  172. //-----------------------------------------------------------------------------
  173. void CEnvLaser::FireAtPoint( trace_t &tr )
  174. {
  175. SetAbsEndPos( tr.endpos );
  176. if ( m_pSprite )
  177. {
  178. UTIL_SetOrigin( m_pSprite, tr.endpos );
  179. }
  180. // Apply damage and do sparks every 1/10th of a second.
  181. if ( gpGlobals->curtime >= m_flFireTime + 0.1 )
  182. {
  183. BeamDamage( &tr );
  184. DoSparks( GetAbsStartPos(), tr.endpos );
  185. }
  186. }
  187. //-----------------------------------------------------------------------------
  188. // Purpose:
  189. //-----------------------------------------------------------------------------
  190. void CEnvLaser::StrikeThink( void )
  191. {
  192. CBaseEntity *pEnd = RandomTargetname( STRING( m_iszLaserTarget ) );
  193. Vector vecFireAt = GetAbsEndPos();
  194. if ( pEnd )
  195. {
  196. vecFireAt = pEnd->GetAbsOrigin();
  197. }
  198. trace_t tr;
  199. UTIL_TraceLine( GetAbsOrigin(), vecFireAt, MASK_SOLID, NULL, COLLISION_GROUP_NONE, &tr );
  200. FireAtPoint( tr );
  201. SetNextThink( gpGlobals->curtime );
  202. }