ingamehudheatbuffer.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //#define HEATBUFFER_INDICATOR_DEBUG // Uncomment for heat buffer indicator debug logs
  2. class IngameHudHeatBuffer
  3. {
  4. protected Widget m_Root;
  5. protected float m_CurrentValue;
  6. protected bool m_IsActive;
  7. protected int m_CurrentDirection; // -1 = none | 0 = decreasing | 1 = increasing
  8. protected int m_PreviousDirection; // -1 = none | 0 = decreasing | 1 = increasing
  9. protected bool m_Update;
  10. protected PlayerBase m_Player;
  11. protected float m_EffectTime;
  12. protected float m_EffectDuration;
  13. protected bool m_FadeIn;
  14. protected float m_FlashingTime;
  15. protected int m_FlashingStage;
  16. protected ref map<int, float> m_FlashingThresholds;
  17. void IngameHudHeatBuffer(Widget root, notnull PlayerBase player)
  18. {
  19. m_Root = root;
  20. m_Player = player;
  21. player.GetOnDeathStart().Insert(OnPlayerNegativeCondition);
  22. player.GetOnUnconsciousStart().Insert(OnPlayerNegativeCondition);
  23. player.GetOnUnconsciousStop().Insert(OnPlayerConditionChanged);
  24. m_CurrentDirection = -1;
  25. m_PreviousDirection = -1;
  26. m_Update = true;
  27. m_FlashingStage = -1;
  28. m_FlashingThresholds = new map<int, float>;
  29. m_FlashingThresholds.Insert(1, 0.002);
  30. m_FlashingThresholds.Insert(2, 0.332);
  31. m_FlashingThresholds.Insert(3, 0.662);
  32. }
  33. void OnPlayerNegativeCondition(PlayerBase player)
  34. {
  35. #ifdef HEATBUFFER_INDICATOR_DEBUG
  36. Print("IngameHudHeatBuffer::OnPlayerNegativeCondition");
  37. #endif
  38. if (player == m_Player)
  39. m_Update = false;
  40. }
  41. void OnPlayerConditionChanged(PlayerBase player)
  42. {
  43. #ifdef HEATBUFFER_INDICATOR_DEBUG
  44. Print("IngameHudHeatBuffer::OnPlayerConditionChanged");
  45. #endif
  46. if (player == m_Player)
  47. m_Update = true;
  48. }
  49. bool CanUpdate()
  50. {
  51. return m_Player && m_Update;
  52. }
  53. void Update(float timeslice)
  54. {
  55. int heatBufferStage = m_Player.GetHeatBufferStage();
  56. float heatBufferVal = m_Player.GetStatHeatBuffer().Get();
  57. float heatBufferDynMax = m_Player.GetHeatBufferDynamicMax();
  58. float heatBufferPercent = heatBufferVal / m_Player.GetStatHeatBuffer().GetMax();
  59. //heatBufferPercent = Math.Floor(heatBufferPercent * 100) * 0.01; // Buff value percentage with micro optimization
  60. float heatBufferDiff = heatBufferVal - m_CurrentValue;
  61. m_CurrentValue = heatBufferVal;
  62. #ifdef HEATBUFFER_INDICATOR_DEBUG
  63. Print("-----------------------------------------------------------------------");
  64. Print("Buff stage=" + heatBufferStage);
  65. Print("Buff value=" + heatBufferVal);
  66. Print("Buff percent=" + heatBufferPercent);
  67. Print("Buff dynamic max=" + heatBufferDynMax);
  68. Print("Effect active=" + m_IsActive);
  69. Print("Last buff value=" + m_CurrentValue);
  70. Print("Buff differance=" + heatBufferDiff);
  71. Print("Prev Heat buffer direction=" + m_PreviousDirection);
  72. Print("Flashing time=" + m_FlashingTime);
  73. Print("Flashing stage=" + m_FlashingStage);
  74. #endif
  75. for (int i = 1; i < HeatBufferMdfr.NUMBER_OF_STAGES; ++i)
  76. {
  77. Widget hbw = m_Root.FindAnyWidget("HeatBuffer" + i);
  78. float stageThreshold = HeatBufferMdfr.STAGE_THRESHOLDS[i];
  79. #ifdef HEATBUFFER_INDICATOR_DEBUG
  80. Print("Stage treshold=" + stageThreshold);
  81. #endif
  82. // Determine heat buffer direction.
  83. // When heat buffer percentage value is higher the the heat buffer dynamic max value we prevent increasing for the indactor logic.
  84. // Heat buffer Dynamic max value determines the max value the character can currently reach with his current heat insolation
  85. if (heatBufferDiff > 0)
  86. {
  87. // If heat buffer percentage value is below or at current heat buffer dynamic max range value the direction cant change
  88. if (heatBufferPercent < heatBufferDynMax)
  89. {
  90. m_CurrentDirection = 1;
  91. }
  92. else
  93. {
  94. #ifdef HEATBUFFER_INDICATOR_DEBUG
  95. Print("HEAT BUFFER - DYNAMIC MAX REACHED - DONT CHANGE DIRECTION");
  96. #endif
  97. m_CurrentDirection = -1;
  98. }
  99. }
  100. else if (heatBufferDiff < 0)
  101. {
  102. m_CurrentDirection = 0;
  103. }
  104. #ifdef HEATBUFFER_INDICATOR_DEBUG
  105. Print("Heat buffer direction=" + m_CurrentDirection);
  106. #endif
  107. // Hide visibility of flashing stages
  108. if (heatBufferStage < i && m_FlashingStage != i || m_FlashingStage == i && m_FlashingTime >= 3.0)
  109. {
  110. if (m_FlashingStage == i)
  111. {
  112. m_IsActive = false;
  113. m_FlashingTime = 0;
  114. m_FlashingStage = -1;
  115. }
  116. hbw.SetAlpha(0);
  117. hbw.Show(false);
  118. }
  119. // Handle widget visibility and alpha based on stage and buffer percent
  120. if (heatBufferStage >= i)
  121. {
  122. hbw.Show(true);
  123. if (heatBufferPercent < stageThreshold)
  124. {
  125. if (m_CurrentDirection == 1)
  126. {
  127. #ifdef HEATBUFFER_INDICATOR_DEBUG
  128. Print("HEAT BUFFER - STAGE " + i + " - INCREASING");
  129. hbw.SetColor(ARGB(hbw.GetAlpha() * 255, 220, 220, 0)); // COLOR SET ON INDICATOR IS ONLY HERE FOR DEBUG TESTING FOR NOW
  130. #endif
  131. SetBaseAlpha(hbw, heatBufferPercent, stageThreshold);
  132. if (m_PreviousDirection == 0 && m_IsActive)
  133. {
  134. m_IsActive = false;
  135. m_FlashingTime = 0;
  136. m_FlashingStage = -1;
  137. }
  138. UpdateEffect(hbw, 2.0, timeslice);
  139. }
  140. else if (m_CurrentDirection == 0)
  141. {
  142. #ifdef HEATBUFFER_IND ICATOR_DEBUG
  143. Print("HEAT BUFFER - STAGE " + i + " - DECREASING");
  144. #endif
  145. SetBaseAlpha(hbw, heatBufferPercent, stageThreshold);
  146. if (m_PreviousDirection == 1 && m_IsActive)
  147. {
  148. m_IsActive = false;
  149. }
  150. float flashingThreshold = m_FlashingThresholds.Get(i);
  151. if (heatBufferDynMax < 0.5)
  152. {
  153. flashingThreshold = flashingThreshold + 0.002;
  154. }
  155. #ifdef HEATBUFFER_INDICATOR_DEBUG
  156. Print("HEAT BUFFER - STAGE " + i + " - DECREASING - Flashing threshold=" + flashingThreshold);
  157. hbw.SetColor(ARGB(hbw.GetAlpha() * 255, 0, 206, 209)); // COLOR SET ON INDICATOR IS ONLY HERE FOR DEBUG TESTING FOR NOW
  158. #endif
  159. if (heatBufferPercent <= flashingThreshold)
  160. {
  161. #ifdef HEATBUFFER_INDICATOR_DEBUG
  162. Print("HEAT BUFFER - STAGE " + i + " - FLASHING");
  163. #endif
  164. if (m_FlashingStage == i && m_FlashingTime <= 3.0)
  165. m_FlashingStage = i;
  166. #ifdef HEATBUFFER_INDICATOR_DEBUG
  167. hbw.SetColor(ARGB(hbw.GetAlpha() * 255, 255, 0, 0)); // COLOR SET ON INDICATOR IS ONLY HERE FOR DEBUG TESTING FOR NOW
  168. #endif
  169. UpdateEffect(hbw, 0.25, timeslice);
  170. m_FlashingTime += timeslice;
  171. if (m_FlashingTime >= 3.0)
  172. {
  173. m_IsActive = false;
  174. m_FlashingTime = 0;
  175. m_FlashingStage = -1;
  176. hbw.SetAlpha(0);
  177. hbw.Show(false);
  178. }
  179. }
  180. }
  181. else if (m_CurrentDirection == -1)
  182. {
  183. #ifdef HEATBUFFER_INDICATOR_DEBUG
  184. Print("HEAT BUFFER - STAGE " + i + " - STAL");
  185. #endif
  186. SetBaseAlpha(hbw, heatBufferPercent, stageThreshold);
  187. }
  188. }
  189. else
  190. {
  191. #ifdef HEATBUFFER_INDICATOR_DEBUG
  192. Print("HEAT BUFFER - STAGE " + i + " - MAXED");
  193. #endif
  194. hbw.SetAlpha(1.0);
  195. #ifdef HEATBUFFER_INDICATOR_DEBUG
  196. hbw.SetColor(ARGB(hbw.GetAlpha() * 255, 220, 220, 220)); // COLOR SET ON INDICATOR IS ONLY HERE FOR DEBUG TESTING FOR NOW
  197. #endif
  198. }
  199. }
  200. }
  201. if (m_PreviousDirection != m_CurrentDirection)
  202. m_PreviousDirection = m_CurrentDirection;
  203. }
  204. // Function that calculates and sets the base alpha value for the current heat buffer widget depending on the given heat buffer percentage value and stage threshold
  205. void SetBaseAlpha(Widget hbw, float valuePercent, float stageThreshold)
  206. {
  207. float baseAlpha = Math.Lerp(0.05, 1.00, (valuePercent / stageThreshold));
  208. baseAlpha = Math.Floor(baseAlpha * 100) / 100;
  209. #ifdef HEATBUFFER_INDICATOR_DEBUG
  210. Print("HEAT BUFFER - Set base alpha=" + baseAlpha + " | Widget=" + hbw.GetName());
  211. #endif
  212. hbw.SetAlpha(baseAlpha); // Set the current alpha to the base alpha
  213. }
  214. void UpdateEffect(Widget hbw, float duration, float timeslice)
  215. {
  216. #ifdef HEATBUFFER_INDICATOR_DEBUG
  217. Print("HEAT BUFFER - EFFECT - Widget=" + hbw.GetName());
  218. #endif
  219. float baseAlpha = hbw.GetAlpha();
  220. float opacity;
  221. // Initialize the effect if it's not already active
  222. if (!m_IsActive)
  223. {
  224. m_EffectDuration = duration;
  225. m_EffectTime = 0;
  226. m_FadeIn = true;
  227. m_IsActive = true;
  228. }
  229. // Update the effect time
  230. m_EffectTime += timeslice;
  231. // Calculate the time fraction
  232. float timeFraction = m_EffectTime / m_EffectDuration;
  233. // Calculate opacity
  234. if (m_FadeIn)
  235. {
  236. opacity = Math.Lerp(0.0, 1.0, timeFraction);
  237. if (timeFraction >= 1.0)
  238. {
  239. m_FadeIn = false;
  240. m_EffectTime = 0; // Reset for fade-out
  241. }
  242. }
  243. else
  244. {
  245. opacity = Math.Lerp(1.0, 0.0, timeFraction);
  246. if (timeFraction >= 1.0)
  247. {
  248. m_FadeIn = true;
  249. m_EffectTime = 0; // Reset for fade-in
  250. }
  251. }
  252. // Clamp the opacity to ensure it's within the valid range
  253. opacity = Math.Clamp(opacity, 0.0, 1.0);
  254. // Set the widget's alpha (opacity)
  255. hbw.SetAlpha(opacity);
  256. // Debug print statements
  257. #ifdef HEATBUFFER_INDICATOR_DEBUG
  258. Print("HEAT BUFFER - EFFECT - Opacity=" + opacity + " | Time Fraction=" + timeFraction + " | FadeIn=" + m_FadeIn + " | Effect time=" + m_EffectTime + " | Effect duration=" + m_EffectDuration);
  259. #endif
  260. }
  261. }