flashbangeffect.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. class FlashbangEffect
  2. {
  3. protected const float ALPHA_MIN = 0.0;
  4. protected const float ALPHA_MAX = 1.0;
  5. protected const float SOUND_DEFER_TIME = 0.4; //! SFX will be played ~0.5s AFTER VFX
  6. protected float m_HitDuration;
  7. protected float m_BreakPoint;
  8. protected float m_TimeActive;
  9. protected float m_DayTimeToggle;
  10. protected float m_AlphaMaxActual; //actual max alpha of the effect
  11. protected float m_SoundMaxActual; //actual max volume of the sound
  12. protected float m_ProgressMultiplier;
  13. protected bool m_Visual;
  14. protected bool m_Initialized;
  15. protected PlayerBase m_Player;
  16. protected EffectSound m_FlashbangEffectSound;
  17. protected float m_SoundStopTime;
  18. protected ref Timer m_DeferAttenuation;
  19. protected PPERequester_FlashbangEffects m_Requester;
  20. void FlashbangEffect(PlayerBase player, bool visual = true)
  21. {
  22. m_Player = player;
  23. m_Visual = visual;
  24. m_Initialized = false;
  25. m_HitDuration = 8.0;
  26. m_BreakPoint = 2.5;
  27. m_AlphaMaxActual = ALPHA_MAX;
  28. m_SoundMaxActual = 1.0;
  29. m_ProgressMultiplier = 1.0;
  30. m_FlashbangEffectSound = null;
  31. if (m_Visual)
  32. {
  33. Class.CastTo(m_Requester,PPERequesterBank.GetRequester(PPERequester_FlashbangEffects));
  34. m_Requester.Start();
  35. }
  36. m_DeferAttenuation = new ref Timer();
  37. m_DeferAttenuation.Run(SOUND_DEFER_TIME, this, "PlaySound", null, false);
  38. //! naive time of the day selector
  39. m_DayTimeToggle = 5; //! -1: night; 1: day
  40. if ( g_Game.GetDayTime() >= 22.0 || g_Game.GetDayTime() < 7.0 )
  41. {
  42. m_DayTimeToggle = 10;
  43. }
  44. }
  45. void ~FlashbangEffect()
  46. {
  47. if ( m_Visual )
  48. {
  49. ClearVisual();
  50. }
  51. if ( m_Player )
  52. {
  53. m_Player.OnPlayerReceiveFlashbangHitEnd();
  54. }
  55. if ( m_DeferAttenuation.IsRunning() )
  56. {
  57. m_DeferAttenuation.Stop();
  58. }
  59. m_DeferAttenuation = null;
  60. SEffectManager.DestroyEffect(m_FlashbangEffectSound);
  61. }
  62. void SetupFlashbangValues(float progress_mult = 1.0, float visual_value_max = 1.0, float sound_value_max = 1.0)
  63. {
  64. if ( !m_Initialized )
  65. {
  66. m_Initialized = true;
  67. m_ProgressMultiplier = progress_mult;
  68. m_AlphaMaxActual = visual_value_max;
  69. m_SoundMaxActual = sound_value_max;
  70. m_HitDuration *= m_ProgressMultiplier;
  71. m_BreakPoint *= m_ProgressMultiplier;
  72. }
  73. }
  74. protected void PlaySound()
  75. {
  76. if ( !m_Initialized )
  77. {
  78. Error("" + this + " not initialized");
  79. return;
  80. }
  81. vector pos;
  82. MiscGameplayFunctions.GetHeadBonePos(m_Player, pos);
  83. if (!m_FlashbangEffectSound)
  84. {
  85. m_FlashbangEffectSound = SEffectManager.CreateSound("Tinnitus_SoundSet", pos);
  86. }
  87. if (!m_FlashbangEffectSound.IsPlaying())
  88. {
  89. m_FlashbangEffectSound.SetParent(m_Player);
  90. m_FlashbangEffectSound.SetAttachedLocalPos(m_Player.WorldToModel(pos));
  91. m_FlashbangEffectSound.SetSoundWaveKind(WaveKind.WAVEEFFECTEX);
  92. m_FlashbangEffectSound.SetSoundFadeIn(4 * Math.Clamp(m_ProgressMultiplier,0.5,1.0)); //TODO
  93. m_SoundStopTime = 2 * Math.Clamp(m_ProgressMultiplier,0.5,1.0);
  94. m_FlashbangEffectSound.SetSoundFadeOut(m_SoundStopTime); //TODO
  95. m_FlashbangEffectSound.SetSoundMaxVolume(Math.Clamp(m_SoundMaxActual,0.1,1.0)); //TODO
  96. m_FlashbangEffectSound.SetSoundLoop(true);
  97. m_FlashbangEffectSound.SoundPlay();
  98. m_FlashbangEffectSound.SetAutodestroy(true);
  99. SetAttenuationFilter();
  100. }
  101. }
  102. protected void SetAttenuationFilter()
  103. {
  104. if ( !m_DeferAttenuation.IsRunning() || m_Player.GetMasterAttenuation() != "FlashbangAttenuation" )
  105. {
  106. m_Player.SetMasterAttenuation("FlashbangAttenuation");
  107. }
  108. }
  109. protected void ResetAttenuationFilter()
  110. {
  111. m_Player.SetMasterAttenuation("");
  112. }
  113. protected void StopSound()
  114. {
  115. if (m_FlashbangEffectSound)
  116. {
  117. m_FlashbangEffectSound.SoundStop();
  118. SEffectManager.DestroyEffect(m_FlashbangEffectSound);
  119. }
  120. }
  121. protected void ClearVisual()
  122. {
  123. if (m_Requester)
  124. {
  125. m_Requester.Stop();
  126. }
  127. }
  128. protected void SetVisual(float val)
  129. {
  130. if (m_Requester && m_Requester.IsRequesterRunning())
  131. {
  132. m_Requester.SetFlashbangIntensity(val, m_DayTimeToggle);
  133. }
  134. }
  135. void Stop()
  136. {
  137. StopSound();
  138. }
  139. void Update(float deltatime)
  140. {
  141. if ( !m_Initialized )
  142. {
  143. Error("" + this + " not initialized");
  144. }
  145. else if ( m_Visual )
  146. {
  147. float value;
  148. if ( m_TimeActive <= m_BreakPoint )
  149. {
  150. value = m_AlphaMaxActual;
  151. //Print("Flashbango | m_AlphaMaxActual: " + value);
  152. }
  153. else
  154. {
  155. value = Math.InverseLerp(m_HitDuration - m_BreakPoint, m_HitDuration, m_TimeActive);
  156. value = Math.Clamp(value,0.0,1.0);
  157. value = m_AlphaMaxActual - value * m_AlphaMaxActual;
  158. //Print("Flashbango | tmp_value: " + value);
  159. }
  160. SetVisual(value);
  161. }
  162. m_TimeActive += deltatime;
  163. if (m_TimeActive >= m_HitDuration - m_SoundStopTime)
  164. {
  165. StopSound();
  166. }
  167. if (m_TimeActive >= m_HitDuration)
  168. {
  169. ResetAttenuationFilter();
  170. delete this;
  171. }
  172. }
  173. }