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