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.

260 lines
7.4 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. // Spawnflags
  14. enum BeamSpotlightSpawnFlags_t
  15. {
  16. SF_BEAM_SPOTLIGHT_START_LIGHT_ON = 1,
  17. SF_BEAM_SPOTLIGHT_NO_DYNAMIC_LIGHT = 2,
  18. SF_BEAM_SPOTLIGHT_START_ROTATE_ON = 4,
  19. SF_BEAM_SPOTLIGHT_REVERSE_DIRECTION = 8,
  20. SF_BEAM_SPOTLIGHT_X_AXIS = 16,
  21. SF_BEAM_SPOTLIGHT_Y_AXIS = 32,
  22. };
  23. class CBeamSpotlight : public CBaseEntity
  24. {
  25. DECLARE_CLASS( CBeamSpotlight, CBaseEntity );
  26. public:
  27. DECLARE_DATADESC();
  28. DECLARE_SERVERCLASS();
  29. CBeamSpotlight();
  30. void Spawn( void );
  31. void Precache( void );
  32. void InputTurnOn( inputdata_t &inputdata );
  33. void InputTurnOff( inputdata_t &inputdata );
  34. void InputStart( inputdata_t &inputdata );
  35. void InputStop( inputdata_t &inputdata );
  36. void InputReverse( inputdata_t &inputdata );
  37. protected:
  38. bool KeyValue( const char *szKeyName, const char *szValue );
  39. private:
  40. int UpdateTransmitState();
  41. void RecalcRotation( void );
  42. CNetworkVar( int, m_nHaloIndex );
  43. CNetworkVar( bool, m_bSpotlightOn );
  44. CNetworkVar( bool, m_bHasDynamicLight );
  45. CNetworkVar( float, m_flSpotlightMaxLength );
  46. CNetworkVar( float, m_flSpotlightGoalWidth );
  47. CNetworkVar( float, m_flHDRColorScale );
  48. CNetworkVar( int, m_nMinDXLevel );
  49. CNetworkVar( int, m_nRotationAxis );
  50. CNetworkVar( float, m_flRotationSpeed );
  51. float m_flmaxSpeed;
  52. bool m_isRotating;
  53. bool m_isReversed;
  54. public:
  55. COutputEvent m_OnOn, m_OnOff; ///< output fires when turned on, off
  56. };
  57. LINK_ENTITY_TO_CLASS( beam_spotlight, CBeamSpotlight );
  58. BEGIN_DATADESC( CBeamSpotlight )
  59. DEFINE_FIELD( m_nHaloIndex, FIELD_MODELINDEX ),
  60. DEFINE_FIELD( m_bSpotlightOn, FIELD_BOOLEAN ),
  61. DEFINE_FIELD( m_bHasDynamicLight, FIELD_BOOLEAN ),
  62. DEFINE_FIELD( m_flRotationSpeed, FIELD_FLOAT ),
  63. DEFINE_FIELD( m_isRotating, FIELD_BOOLEAN ),
  64. DEFINE_FIELD( m_isReversed, FIELD_BOOLEAN ),
  65. DEFINE_FIELD( m_nRotationAxis, FIELD_INTEGER ),
  66. DEFINE_KEYFIELD( m_flmaxSpeed, FIELD_FLOAT, "maxspeed" ),
  67. DEFINE_KEYFIELD( m_flSpotlightMaxLength,FIELD_FLOAT, "SpotlightLength"),
  68. DEFINE_KEYFIELD( m_flSpotlightGoalWidth,FIELD_FLOAT, "SpotlightWidth"),
  69. DEFINE_KEYFIELD( m_flHDRColorScale, FIELD_FLOAT, "HDRColorScale" ),
  70. DEFINE_INPUTFUNC( FIELD_VOID, "LightOn", InputTurnOn ),
  71. DEFINE_INPUTFUNC( FIELD_VOID, "LightOff", InputTurnOff ),
  72. DEFINE_INPUTFUNC( FIELD_VOID, "Start", InputStart ),
  73. DEFINE_INPUTFUNC( FIELD_VOID, "Stop", InputStop ),
  74. DEFINE_INPUTFUNC( FIELD_VOID, "Reverse", InputReverse ),
  75. DEFINE_OUTPUT( m_OnOn, "OnLightOn" ),
  76. DEFINE_OUTPUT( m_OnOff, "OnLightOff" ),
  77. END_DATADESC()
  78. IMPLEMENT_SERVERCLASS_ST(CBeamSpotlight, DT_BeamSpotlight)
  79. SendPropInt( SENDINFO(m_nHaloIndex), 16, SPROP_UNSIGNED ),
  80. SendPropBool( SENDINFO(m_bSpotlightOn) ),
  81. SendPropBool( SENDINFO(m_bHasDynamicLight) ),
  82. SendPropFloat( SENDINFO(m_flSpotlightMaxLength), 0, SPROP_NOSCALE ),
  83. SendPropFloat( SENDINFO(m_flSpotlightGoalWidth), 0, SPROP_NOSCALE ),
  84. SendPropFloat( SENDINFO(m_flHDRColorScale), 0, SPROP_NOSCALE ),
  85. SendPropFloat( SENDINFO(m_flRotationSpeed), 0, SPROP_NOSCALE ),
  86. SendPropInt( SENDINFO(m_nRotationAxis), 2, SPROP_UNSIGNED ),
  87. END_SEND_TABLE()
  88. //-----------------------------------------------------------------------------
  89. // Purpose:
  90. //-----------------------------------------------------------------------------
  91. CBeamSpotlight::CBeamSpotlight()
  92. : m_bSpotlightOn( false )
  93. , m_bHasDynamicLight( true )
  94. , m_flSpotlightMaxLength( 500.0f )
  95. , m_flSpotlightGoalWidth( 50.0f )
  96. , m_flHDRColorScale( 0.7f )
  97. , m_isRotating( false )
  98. , m_isReversed( false )
  99. , m_flmaxSpeed( 100.0f )
  100. , m_flRotationSpeed(0.0f)
  101. , m_nRotationAxis(0)
  102. {
  103. }
  104. bool CBeamSpotlight::KeyValue( const char *szKeyName, const char *szValue )
  105. {
  106. if ( !Q_stricmp( szKeyName, "SpotlightWidth" ) )
  107. {
  108. m_flSpotlightGoalWidth = Q_atof(szValue);
  109. if ( m_flSpotlightGoalWidth > MAX_BEAM_WIDTH )
  110. {
  111. Warning( "Map Bug: %s has SpotLightWidth %f > %f, clamping value\n",
  112. STRING( GetEntityName()), m_flSpotlightGoalWidth.m_Value, (float)MAX_BEAM_WIDTH );
  113. m_flSpotlightGoalWidth = Min( MAX_BEAM_WIDTH, m_flSpotlightGoalWidth.Get() );
  114. }
  115. return true;
  116. }
  117. return BaseClass::KeyValue( szKeyName, szValue );
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose:
  121. //-----------------------------------------------------------------------------
  122. void CBeamSpotlight::Precache( void )
  123. {
  124. BaseClass::Precache();
  125. // Sprites.
  126. m_nHaloIndex = PrecacheModel("sprites/light_glow03.vmt");
  127. PrecacheModel( "sprites/glow_test02.vmt" );
  128. }
  129. //-----------------------------------------------------------------------------
  130. void CBeamSpotlight::RecalcRotation( void )
  131. {
  132. if ( !m_isRotating || m_flmaxSpeed == 0.0f )
  133. {
  134. m_flRotationSpeed = 0.0f;
  135. return;
  136. }
  137. //
  138. // Build the axis of rotation based on spawnflags.
  139. //
  140. // Pitch Yaw Roll -> Y Z X
  141. m_nRotationAxis = 1;
  142. if ( HasSpawnFlags(SF_BEAM_SPOTLIGHT_Y_AXIS) )
  143. {
  144. m_nRotationAxis = 0;
  145. }
  146. else if ( HasSpawnFlags(SF_BEAM_SPOTLIGHT_X_AXIS) )
  147. {
  148. m_nRotationAxis = 2;
  149. }
  150. m_flRotationSpeed = m_flmaxSpeed;
  151. if ( m_isReversed )
  152. {
  153. m_flRotationSpeed *= -1.0f;
  154. }
  155. }
  156. //-----------------------------------------------------------------------------
  157. // Purpose:
  158. //-----------------------------------------------------------------------------
  159. void CBeamSpotlight::Spawn( void )
  160. {
  161. Precache();
  162. UTIL_SetSize( this,vec3_origin,vec3_origin );
  163. AddSolidFlags( FSOLID_NOT_SOLID );
  164. SetMoveType( MOVETYPE_NONE );
  165. AddEFlags( EFL_FORCE_CHECK_TRANSMIT );
  166. m_bHasDynamicLight = !HasSpawnFlags( SF_BEAM_SPOTLIGHT_NO_DYNAMIC_LIGHT);
  167. m_bSpotlightOn = HasSpawnFlags( SF_BEAM_SPOTLIGHT_START_LIGHT_ON );
  168. m_isRotating = HasSpawnFlags( SF_BEAM_SPOTLIGHT_START_ROTATE_ON );
  169. m_isReversed = HasSpawnFlags( SF_BEAM_SPOTLIGHT_REVERSE_DIRECTION );
  170. RecalcRotation();
  171. }
  172. //-----------------------------------------------------------------------------
  173. void CBeamSpotlight::InputTurnOn( inputdata_t &inputdata )
  174. {
  175. if ( !m_bSpotlightOn )
  176. {
  177. m_bSpotlightOn = true;
  178. }
  179. }
  180. //-----------------------------------------------------------------------------
  181. void CBeamSpotlight::InputTurnOff( inputdata_t &inputdata )
  182. {
  183. if ( m_bSpotlightOn )
  184. {
  185. m_bSpotlightOn = false;
  186. }
  187. }
  188. //-----------------------------------------------------------------------------
  189. void CBeamSpotlight::InputStart( inputdata_t &inputdata )
  190. {
  191. if ( !m_isRotating )
  192. {
  193. m_isRotating = true;
  194. RecalcRotation();
  195. }
  196. }
  197. //-----------------------------------------------------------------------------
  198. void CBeamSpotlight::InputStop( inputdata_t &inputdata )
  199. {
  200. if ( m_isRotating )
  201. {
  202. m_isRotating = false;
  203. RecalcRotation();
  204. }
  205. }
  206. //-----------------------------------------------------------------------------
  207. void CBeamSpotlight::InputReverse( inputdata_t &inputdata )
  208. {
  209. m_isReversed = !m_isReversed;
  210. RecalcRotation();
  211. }
  212. //-------------------------------------------------------------------------------------
  213. // Purpose : Send even though we don't have a model so spotlight gets proper position
  214. //-------------------------------------------------------------------------------------
  215. int CBeamSpotlight::UpdateTransmitState()
  216. {
  217. return SetTransmitState( FL_EDICT_PVSCHECK );
  218. }