shockhandler.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. class ShockHandler
  2. {
  3. protected float m_Shock;
  4. protected float m_ShockValueMax;
  5. protected float m_ShockValueThreshold;
  6. protected PlayerBase m_Player;
  7. protected const float UPDATE_THRESHOLD = 3; //NOTE : The lower, the more precise but the more synchronization
  8. const float VALUE_CHECK_INTERVAL = 0.95; //in seconds
  9. protected float m_CumulatedShock;
  10. private float m_TimeSinceLastTick = VALUE_CHECK_INTERVAL + 1;
  11. private float m_ShockMultiplier = 1;
  12. private float m_PrevVignette; //Previous vignette shock value
  13. private float m_LerpRes; //Lerp result
  14. private const int LIGHT_SHOCK_HIT = 33;
  15. private const int MID_SHOCK_HIT = 67;
  16. private const int HEAVY_SHOCK_HIT = 100;
  17. private const int INTENSITY_FACTOR = 1; //How intense the vignette effect will be, the higher the value, the stronger the effect
  18. //Pulsing effect
  19. private const float PULSE_PERIOD = 0.5; //The time it takes for pulse to do a full cycle
  20. private const float PULSE_AMPLITUDE = 0.05; //This is a multiplier, keep below 1 or expect the unexpected
  21. private float m_PulseTimer;
  22. protected ref Param1<float> m_Param;
  23. void ShockHandler(PlayerBase player)
  24. {
  25. m_Player = player;
  26. m_Player.m_CurrentShock = m_Player.GetMaxHealth("", "Shock");
  27. m_PrevVignette = m_Player.m_CurrentShock * 0.01; //Equivalent to divided by 100
  28. m_ShockValueMax = m_Player.GetMaxHealth("", "Shock");
  29. m_ShockValueThreshold = m_ShockValueMax * 0.95;
  30. m_Param = new Param1<float>(0);
  31. }
  32. void Update(float deltaT)
  33. {
  34. m_TimeSinceLastTick += deltaT;
  35. m_PulseTimer += deltaT;
  36. if ( GetGame().IsClient() )
  37. {
  38. //Deactivate tunnel vignette when player falls unconscious
  39. if ( m_Player.IsUnconscious() )
  40. {
  41. PPERequesterBank.GetRequester(PPERequester_TunnelVisionEffects).Stop();
  42. return;
  43. }
  44. //Deactivate if above visible threshold (also stops "zero bobbing" being sent all the time
  45. if ( m_Player.m_CurrentShock >= m_ShockValueThreshold)
  46. {
  47. PPERequesterBank.GetRequester(PPERequester_TunnelVisionEffects).Stop();
  48. return;
  49. }
  50. //Add bobbing to create pulsing effect
  51. float val = 0.0;
  52. if ( m_Player.m_CurrentShock > m_ShockValueMax * 0.8)
  53. val = MiscGameplayFunctions.Bobbing( PULSE_PERIOD, PULSE_AMPLITUDE, m_PulseTimer );
  54. float val_adjusted;
  55. if ( m_Player.m_CurrentShock != (m_PrevVignette * 100) )
  56. {
  57. //Interpolate between previous level and currently synchronized shock level
  58. m_LerpRes = LerpVignette( m_PrevVignette, NormalizeShockVal(m_Player.m_CurrentShock), m_TimeSinceLastTick );
  59. val_adjusted = 1 - Easing.EaseInQuart(m_LerpRes) + val;
  60. }
  61. else
  62. {
  63. val_adjusted = 1 - Easing.EaseInQuart( NormalizeShockVal(m_Player.m_CurrentShock)) + val;
  64. }
  65. m_Param.param1 = val_adjusted;
  66. PPERequesterBank.GetRequester(PPERequester_TunnelVisionEffects).Start(m_Param);
  67. }
  68. if ( m_TimeSinceLastTick > VALUE_CHECK_INTERVAL )
  69. {
  70. //Play the shock hit event (multiply prevVignette by 100 to "Un-Normalize" value)
  71. if ( GetGame().IsClient() )
  72. {
  73. ShockHitEffect( m_PrevVignette * 100 );
  74. m_PrevVignette = m_Player.m_CurrentShock * 0.01;
  75. }
  76. CheckValue( false );
  77. m_TimeSinceLastTick = 0;
  78. }
  79. }
  80. float GetCurrentShock()
  81. {
  82. return m_Player.m_CurrentShock;
  83. }
  84. float GetShock()
  85. {
  86. return m_Shock;
  87. }
  88. void SetShock( float dealtShock )
  89. {
  90. m_Shock += dealtShock;
  91. CheckValue( false );
  92. }
  93. //Inflict shock damage
  94. private void DealShock()
  95. {
  96. if ( GetGame().IsServer() )
  97. m_Player.GiveShock( -m_CumulatedShock );
  98. }
  99. //Passing a value of FALSE will only check the values, a value of TRUE will force a synchronization (don't use too often)
  100. void CheckValue(bool forceUpdate)
  101. {
  102. m_CumulatedShock += m_Shock; // increment on both client and server
  103. if ( forceUpdate )
  104. m_PrevVignette = NormalizeShockVal( m_Player.m_CurrentShock );
  105. if ( GetGame().IsServer() )
  106. {
  107. m_Player.m_CurrentShock = m_Player.GetHealth("", "Shock");
  108. /*
  109. if (m_Player.m_CurrentShock <= 0)
  110. m_Player.SetHealthMax("", "Shock");
  111. */
  112. if ( m_CumulatedShock >= UPDATE_THRESHOLD || forceUpdate )
  113. {
  114. m_CumulatedShock *= m_ShockMultiplier;
  115. DealShock();
  116. m_CumulatedShock = 0;
  117. m_Shock = 0;
  118. Synchronize();
  119. }
  120. }
  121. }
  122. protected void Synchronize()
  123. {
  124. DayZPlayerSyncJunctures.SendShock( m_Player, m_Player.m_CurrentShock );
  125. }
  126. float SetMultiplier(float mult)
  127. {
  128. m_ShockMultiplier = mult;
  129. return m_ShockMultiplier;
  130. }
  131. private float NormalizeShockVal( float shock )
  132. {
  133. float base = m_Player.GetMaxHealth("", "Shock") * INTENSITY_FACTOR;
  134. float normShock = shock / base;
  135. return normShock;
  136. }
  137. private float LerpVignette( float x, float y, float deltaT )
  138. {
  139. float output;
  140. output = Math.Lerp( x, y, deltaT );
  141. return output;
  142. }
  143. //ONLY CLIENT SIDE
  144. private void ShockHitEffect( float compareBase )
  145. {
  146. float shockDifference = compareBase - m_Player.m_CurrentShock;
  147. //Print(shockDifference);
  148. if ( shockDifference >= UPDATE_THRESHOLD )
  149. {
  150. if ( m_CumulatedShock < 25 )
  151. m_Player.SpawnShockEffect( MiscGameplayFunctions.Normalize( LIGHT_SHOCK_HIT, 100 ) );
  152. else if ( m_CumulatedShock < 50 )
  153. m_Player.SpawnShockEffect( MiscGameplayFunctions.Normalize( MID_SHOCK_HIT, 100 ) );
  154. else
  155. m_Player.SpawnShockEffect( MiscGameplayFunctions.Normalize( HEAVY_SHOCK_HIT, 100 ) );
  156. }
  157. }
  158. };