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.

284 lines
8.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef FLASHLIGHTEFFECT_H
  7. #define FLASHLIGHTEFFECT_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. struct dlight_t;
  12. class CFlashlightEffect
  13. {
  14. public:
  15. CFlashlightEffect(int nEntIndex = 0, const char *pszTextureName = NULL, float flFov = 0.0f, float flFarZ = 0.0f, float flLinearAtten = 0.0f );
  16. ~CFlashlightEffect();
  17. void UpdateLight( int nEntIdx, const Vector &vecPos, const Vector &vecDir, const Vector &vecRight, const Vector &vecUp, float flFov,
  18. float flFarZ, float flLinearAtten, bool castsShadows, const char* pTextureName );
  19. void UpdateLight( int nEntIdx, const Vector &vecPos, const Vector &vecDir, const Vector &vecRight, const Vector &vecUp, float flFov,
  20. bool castsShadows, ITexture *pFlashlightTexture, const Vector &vecBrightness, bool bTracePlayers = true );
  21. void TurnOn();
  22. void TurnOff();
  23. void SetMuzzleFlashEnabled( bool bEnabled, float flBrightness );
  24. bool IsOn( void ) { return m_bIsOn; }
  25. ClientShadowHandle_t GetFlashlightHandle( void ) { return m_FlashlightHandle; }
  26. void SetFlashlightHandle( ClientShadowHandle_t Handle ) { m_FlashlightHandle = Handle; }
  27. const char *GetFlashlightTextureName( void ) const
  28. {
  29. return m_textureName;
  30. }
  31. int GetEntIndex( void ) const
  32. {
  33. return m_nEntIndex;
  34. }
  35. protected:
  36. bool UpdateDefaultFlashlightState( FlashlightState_t& state, const Vector &vecPos, const Vector &vecDir, const Vector &vecRight,
  37. const Vector &vecUp, bool castsShadows, bool bTracePlayers = true );
  38. bool ComputeLightPosAndOrientation( const Vector &vecPos, const Vector &vecDir, const Vector &vecRight, const Vector &vecUp,
  39. Vector& vecFinalPos, Quaternion& quatOrientation, bool bTracePlayers );
  40. void LightOff();
  41. void UpdateFlashlightTexture( const char* pTextureName );
  42. void UpdateLightTopDown(const Vector &vecPos, const Vector &vecDir, const Vector &vecRight, const Vector &vecUp);
  43. bool m_bIsOn;
  44. int m_nEntIndex;
  45. ClientShadowHandle_t m_FlashlightHandle;
  46. bool m_bMuzzleFlashEnabled;
  47. float m_flMuzzleFlashBrightness;
  48. float m_flFov;
  49. float m_flFarZ;
  50. float m_flLinearAtten;
  51. bool m_bCastsShadows;
  52. float m_flCurrentPullBackDist;
  53. // Texture for flashlight
  54. CTextureReference m_FlashlightTexture;
  55. // Texture for muzzle flash
  56. CTextureReference m_MuzzleFlashTexture;
  57. char m_textureName[64];
  58. };
  59. class CHeadlightEffect : public CFlashlightEffect
  60. {
  61. public:
  62. CHeadlightEffect();
  63. ~CHeadlightEffect();
  64. virtual void UpdateLight(const Vector &vecPos, const Vector &vecDir, const Vector &vecRight, const Vector &vecUp, int nDistance);
  65. };
  66. class CFlashlightEffectManager
  67. {
  68. private:
  69. CFlashlightEffect *m_pFlashlightEffect;
  70. const char *m_pFlashlightTextureName;
  71. int m_nFlashlightEntIndex;
  72. float m_flFov;
  73. float m_flFarZ;
  74. float m_flLinearAtten;
  75. int m_nMuzzleFlashFrameCountdown;
  76. CountdownTimer m_muzzleFlashTimer;
  77. float m_flMuzzleFlashBrightness;
  78. bool m_bFlashlightOn;
  79. int m_nFXComputeFrame;
  80. bool m_bFlashlightOverride;
  81. public:
  82. CFlashlightEffectManager() : m_pFlashlightEffect( NULL ), m_pFlashlightTextureName( NULL ), m_nFlashlightEntIndex( -1 ), m_flFov( 0.0f ),
  83. m_flFarZ( 0.0f ), m_flLinearAtten( 0.0f ), m_nMuzzleFlashFrameCountdown( 0 ), m_flMuzzleFlashBrightness( 1.0f ),
  84. m_bFlashlightOn( false ), m_nFXComputeFrame( -1 ), m_bFlashlightOverride( false ) {}
  85. void TurnOnFlashlight( int nEntIndex = 0, const char *pszTextureName = NULL, float flFov = 0.0f, float flFarZ = 0.0f, float flLinearAtten = 0.0f )
  86. {
  87. m_pFlashlightTextureName = pszTextureName;
  88. m_nFlashlightEntIndex = nEntIndex;
  89. m_flFov = flFov;
  90. m_flFarZ = flFarZ;
  91. m_flLinearAtten = flLinearAtten;
  92. m_bFlashlightOn = true;
  93. if ( m_bFlashlightOverride )
  94. {
  95. // somebody is overriding the flashlight. We're keeping around the params to restore it later.
  96. return;
  97. }
  98. if ( !m_pFlashlightEffect )
  99. {
  100. if( pszTextureName )
  101. {
  102. m_pFlashlightEffect = new CFlashlightEffect( m_nFlashlightEntIndex, pszTextureName, flFov, flFarZ, flLinearAtten );
  103. }
  104. else
  105. {
  106. m_pFlashlightEffect = new CFlashlightEffect( m_nFlashlightEntIndex );
  107. }
  108. if( !m_pFlashlightEffect )
  109. {
  110. return;
  111. }
  112. }
  113. m_pFlashlightEffect->TurnOn();
  114. }
  115. void TurnOffFlashlight( bool bForce = false )
  116. {
  117. m_pFlashlightTextureName = NULL;
  118. m_bFlashlightOn = false;
  119. if ( bForce )
  120. {
  121. m_bFlashlightOverride = false;
  122. m_nMuzzleFlashFrameCountdown = 0;
  123. m_muzzleFlashTimer.Invalidate();
  124. delete m_pFlashlightEffect;
  125. m_pFlashlightEffect = NULL;
  126. return;
  127. }
  128. if ( m_bFlashlightOverride )
  129. {
  130. // don't mess with it while it's overridden
  131. return;
  132. }
  133. if( m_nMuzzleFlashFrameCountdown == 0 && m_muzzleFlashTimer.IsElapsed() )
  134. {
  135. delete m_pFlashlightEffect;
  136. m_pFlashlightEffect = NULL;
  137. }
  138. }
  139. bool IsFlashlightOn() const { return m_bFlashlightOn; }
  140. void UpdateFlashlight( const Vector &vecPos, const Vector &vecDir, const Vector &vecRight, const Vector &vecUp, float flFov, bool castsShadows,
  141. float flFarZ, float flLinearAtten, const char* pTextureName = NULL )
  142. {
  143. if ( m_bFlashlightOverride )
  144. {
  145. // don't mess with it while it's overridden
  146. return;
  147. }
  148. bool bMuzzleFlashActive = ( m_nMuzzleFlashFrameCountdown > 0 ) || !m_muzzleFlashTimer.IsElapsed();
  149. if ( m_pFlashlightEffect )
  150. {
  151. m_flFov = flFov;
  152. m_flFarZ = flFarZ;
  153. m_flLinearAtten = flLinearAtten;
  154. m_pFlashlightEffect->UpdateLight( m_nFlashlightEntIndex, vecPos, vecDir, vecRight, vecUp, flFov, flFarZ, flLinearAtten, castsShadows, pTextureName );
  155. m_pFlashlightEffect->SetMuzzleFlashEnabled( bMuzzleFlashActive, m_flMuzzleFlashBrightness );
  156. }
  157. if ( !bMuzzleFlashActive && !m_bFlashlightOn && m_pFlashlightEffect )
  158. {
  159. delete m_pFlashlightEffect;
  160. m_pFlashlightEffect = NULL;
  161. }
  162. if ( bMuzzleFlashActive && !m_bFlashlightOn && !m_pFlashlightEffect )
  163. {
  164. m_pFlashlightEffect = new CFlashlightEffect( m_nFlashlightEntIndex );
  165. m_pFlashlightEffect->SetMuzzleFlashEnabled( bMuzzleFlashActive, m_flMuzzleFlashBrightness );
  166. }
  167. if ( bMuzzleFlashActive && m_nFXComputeFrame != gpGlobals->framecount )
  168. {
  169. m_nFXComputeFrame = gpGlobals->framecount;
  170. m_nMuzzleFlashFrameCountdown--;
  171. }
  172. }
  173. void SetEntityIndex( int index )
  174. {
  175. m_nFlashlightEntIndex = index;
  176. }
  177. void TriggerMuzzleFlash()
  178. {
  179. m_nMuzzleFlashFrameCountdown = 2;
  180. m_muzzleFlashTimer.Start( 0.066f ); // show muzzleflash for 2 frames or 66ms, whichever is longer
  181. m_flMuzzleFlashBrightness = random->RandomFloat( 0.4f, 2.0f );
  182. }
  183. const char *GetFlashlightTextureName( void ) const
  184. {
  185. return m_pFlashlightTextureName;
  186. }
  187. int GetFlashlightEntIndex( void ) const
  188. {
  189. return m_nFlashlightEntIndex;
  190. }
  191. void EnableFlashlightOverride( bool bEnable )
  192. {
  193. m_bFlashlightOverride = bEnable;
  194. if ( !m_bFlashlightOverride )
  195. {
  196. // make sure flashlight is in its original state
  197. if ( m_bFlashlightOn && m_pFlashlightEffect == NULL )
  198. {
  199. TurnOnFlashlight( m_nFlashlightEntIndex, m_pFlashlightTextureName, m_flFov, m_flFarZ, m_flLinearAtten );
  200. }
  201. else if ( !m_bFlashlightOn && m_pFlashlightEffect )
  202. {
  203. delete m_pFlashlightEffect;
  204. m_pFlashlightEffect = NULL;
  205. }
  206. }
  207. }
  208. void UpdateFlashlightOverride( bool bFlashlightOn, const Vector &vecPos, const Vector &vecDir, const Vector &vecRight, const Vector &vecUp,
  209. float flFov, bool castsShadows, ITexture *pFlashlightTexture, const Vector &vecBrightness )
  210. {
  211. Assert( m_bFlashlightOverride );
  212. if ( !m_bFlashlightOverride )
  213. {
  214. return;
  215. }
  216. if ( bFlashlightOn && !m_pFlashlightEffect )
  217. {
  218. m_pFlashlightEffect = new CFlashlightEffect( m_nFlashlightEntIndex );
  219. }
  220. else if ( !bFlashlightOn && m_pFlashlightEffect )
  221. {
  222. delete m_pFlashlightEffect;
  223. m_pFlashlightEffect = NULL;
  224. }
  225. if( m_pFlashlightEffect )
  226. {
  227. m_pFlashlightEffect->UpdateLight( m_nFlashlightEntIndex, vecPos, vecDir, vecRight, vecUp, flFov, castsShadows, pFlashlightTexture, vecBrightness, false );
  228. }
  229. }
  230. };
  231. CFlashlightEffectManager & FlashlightEffectManager( int32 nSplitscreenPlayerOverride = -1 );
  232. #endif // FLASHLIGHTEFFECT_H