ingamehudheatbuffer.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. float heatBufferDiff = heatBufferVal - m_CurrentValue;
  60. m_CurrentValue = heatBufferVal;
  61. #ifdef HEATBUFFER_INDICATOR_DEBUG
  62. Print("-----------------------------------------------------------------------");
  63. Print("Buff stage=" + heatBufferStage);
  64. Print("Buff value=" + heatBufferVal);
  65. Print("Buff percent=" + heatBufferPercent);
  66. Print("Buff dynamic max=" + heatBufferDynMax);
  67. Print("Effect active=" + m_IsActive);
  68. Print("Last buff value=" + m_CurrentValue);
  69. Print("Buff differance=" + heatBufferDiff);
  70. Print("Prev Heat buffer direction=" + m_PreviousDirection);
  71. Print("Flashing time=" + m_FlashingTime);
  72. Print("Flashing stage=" + m_FlashingStage);
  73. #endif
  74. for (int i = 1; i < HeatBufferMdfr.NUMBER_OF_STAGES; ++i)
  75. {
  76. Widget hbw = m_Root.FindAnyWidget("HeatBuffer" + i);
  77. if (!hbw)
  78. continue;
  79. float stageThreshold = HeatBufferMdfr.STAGE_THRESHOLDS[i];
  80. #ifdef HEATBUFFER_INDICATOR_DEBUG
  81. Print("Stage treshold=" + stageThreshold);
  82. #endif
  83. // Determine heat buffer direction.
  84. // When heat buffer percentage value is higher the the heat buffer dynamic max value we prevent increasing for the indactor logic.
  85. // Heat buffer Dynamic max value determines the max value the character can currently reach with his current heat insolation
  86. if (heatBufferDiff > 0)
  87. {
  88. // If heat buffer percentage value is below or at current heat buffer dynamic max range value the direction cant change
  89. if (heatBufferPercent < heatBufferDynMax)
  90. {
  91. m_CurrentDirection = 1;
  92. }
  93. else
  94. {
  95. #ifdef HEATBUFFER_INDICATOR_DEBUG
  96. Print("HEAT BUFFER - DYNAMIC MAX REACHED - DONT CHANGE DIRECTION");
  97. #endif
  98. m_CurrentDirection = -1;
  99. }
  100. }
  101. else if (heatBufferDiff < 0)
  102. {
  103. m_CurrentDirection = 0;
  104. }
  105. #ifdef HEATBUFFER_INDICATOR_DEBUG
  106. Print("Heat buffer direction=" + m_CurrentDirection);
  107. #endif
  108. // Hide visibility of flashing stages
  109. if (heatBufferStage < i && m_FlashingStage != i || m_FlashingStage == i && m_FlashingTime >= 2.9)
  110. {
  111. if (m_FlashingStage == i)
  112. {
  113. m_IsActive = false;
  114. m_FlashingTime = 0;
  115. m_FlashingStage = -1;
  116. }
  117. hbw.SetAlpha(0);
  118. hbw.Show(false);
  119. }
  120. // Handle widget visibility and alpha based on stage and buffer percent
  121. if (heatBufferStage >= i)
  122. {
  123. hbw.Show(true);
  124. if (heatBufferPercent < stageThreshold)
  125. {
  126. if (m_CurrentDirection == 1)
  127. {
  128. #ifdef HEATBUFFER_INDICATOR_DEBUG
  129. Print("HEAT BUFFER - STAGE " + i + " - INCREASING");
  130. hbw.SetColor(ARGB(hbw.GetAlpha() * 255, 220, 220, 0)); // COLOR SET ON INDICATOR IS ONLY HERE FOR DEBUG TESTING FOR NOW
  131. #endif
  132. SetBaseAlpha(hbw, heatBufferPercent, stageThreshold);
  133. if (m_PreviousDirection == 0 && m_IsActive)
  134. {
  135. m_IsActive = false;
  136. m_FlashingTime = 0;
  137. m_FlashingStage = -1;
  138. }
  139. UpdateEffect(hbw, 2.0, timeslice);
  140. }
  141. else if (m_CurrentDirection == 0)
  142. {
  143. #ifdef HEATBUFFER_IND ICATOR_DEBUG
  144. Print("HEAT BUFFER - STAGE " + i + " - DECREASING");
  145. #endif
  146. SetBaseAlpha(hbw, heatBufferPercent, stageThreshold);
  147. if (m_PreviousDirection == 1 && m_IsActive)
  148. {
  149. m_IsActive = false;
  150. }
  151. float flashingThreshold = m_FlashingThresholds.Get(i);
  152. if (heatBufferDynMax < 0.5)
  153. {
  154. flashingThreshold = flashingThreshold + 0.002;
  155. }
  156. #ifdef HEATBUFFER_INDICATOR_DEBUG
  157. Print("HEAT BUFFER - STAGE " + i + " - DECREASING - Flashing threshold=" + flashingThreshold);
  158. hbw.SetColor(ARGB(hbw.GetAlpha() * 255, 0, 206, 209)); // COLOR SET ON INDICATOR IS ONLY HERE FOR DEBUG TESTING FOR NOW
  159. #endif
  160. if (heatBufferPercent <= flashingThreshold)
  161. {
  162. #ifdef HEATBUFFER_INDICATOR_DEBUG
  163. Print("HEAT BUFFER - STAGE " + i + " - FLASHING");
  164. #endif
  165. #ifdef HEATBUFFER_INDICATOR_DEBUG
  166. hbw.SetColor(ARGB(hbw.GetAlpha() * 255, 255, 0, 0)); // COLOR SET ON INDICATOR IS ONLY HERE FOR DEBUG TESTING FOR NOW
  167. #endif
  168. UpdateEffect(hbw, 0.25, timeslice);
  169. m_FlashingTime += timeslice;
  170. if (m_FlashingTime >= 2.9)
  171. {
  172. m_IsActive = false;
  173. m_FlashingTime = 0;
  174. m_FlashingStage = -1;
  175. hbw.SetAlpha(0);
  176. hbw.Show(false);
  177. }
  178. }
  179. }
  180. else if (m_CurrentDirection == -1)
  181. {
  182. #ifdef HEATBUFFER_INDICATOR_DEBUG
  183. Print("HEAT BUFFER - STAGE " + i + " - STAL");
  184. #endif
  185. SetBaseAlpha(hbw, heatBufferPercent, stageThreshold);
  186. }
  187. }
  188. else
  189. {
  190. #ifdef HEATBUFFER_INDICATOR_DEBUG
  191. Print("HEAT BUFFER - STAGE " + i + " - MAXED");
  192. #endif
  193. hbw.SetAlpha(1.0);
  194. #ifdef HEATBUFFER_INDICATOR_DEBUG
  195. hbw.SetColor(ARGB(hbw.GetAlpha() * 255, 220, 220, 220)); // COLOR SET ON INDICATOR IS ONLY HERE FOR DEBUG TESTING FOR NOW
  196. #endif
  197. }
  198. }
  199. }
  200. if (m_PreviousDirection != m_CurrentDirection)
  201. m_PreviousDirection = m_CurrentDirection;
  202. }
  203. // 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
  204. void SetBaseAlpha(Widget hbw, float valuePercent, float stageThreshold)
  205. {
  206. float baseAlpha = Math.Lerp(0.05, 1.00, (valuePercent / stageThreshold));
  207. baseAlpha = Math.Floor(baseAlpha * 100) / 100;
  208. #ifdef HEATBUFFER_INDICATOR_DEBUG
  209. Print("HEAT BUFFER - Set base alpha=" + baseAlpha + " | Widget=" + hbw.GetName());
  210. #endif
  211. hbw.SetAlpha(baseAlpha); // Set the current alpha to the base alpha
  212. }
  213. void UpdateEffect(Widget hbw, float duration, float timeslice)
  214. {
  215. #ifdef HEATBUFFER_INDICATOR_DEBUG
  216. Print("HEAT BUFFER - EFFECT - Widget=" + hbw.GetName());
  217. #endif
  218. float baseAlpha = hbw.GetAlpha();
  219. float opacity;
  220. // Initialize the effect if it's not already active
  221. if (!m_IsActive)
  222. {
  223. m_EffectDuration = duration;
  224. m_EffectTime = 0;
  225. m_FadeIn = true;
  226. m_IsActive = true;
  227. }
  228. // Update the effect time
  229. m_EffectTime += timeslice;
  230. // Calculate the time fraction
  231. float timeFraction = m_EffectTime / m_EffectDuration;
  232. // Calculate opacity
  233. if (m_FadeIn)
  234. {
  235. opacity = Math.Lerp(0.0, 1.0, timeFraction);
  236. if (timeFraction >= 1.0)
  237. {
  238. m_FadeIn = false;
  239. m_EffectTime = 0; // Reset for fade-out
  240. }
  241. }
  242. else
  243. {
  244. opacity = Math.Lerp(1.0, 0.0, timeFraction);
  245. if (timeFraction >= 1.0)
  246. {
  247. m_FadeIn = true;
  248. m_EffectTime = 0; // Reset for fade-in
  249. }
  250. }
  251. // Clamp the opacity to ensure it's within the valid range
  252. opacity = Math.Clamp(opacity, 0.0, 1.0);
  253. // Set the widget's alpha (opacity)
  254. hbw.SetAlpha(opacity);
  255. // Debug print statements
  256. #ifdef HEATBUFFER_INDICATOR_DEBUG
  257. Print("HEAT BUFFER - EFFECT - Opacity=" + opacity + " | Time Fraction=" + timeFraction + " | FadeIn=" + m_FadeIn + " | Effect time=" + m_EffectTime + " | Effect duration=" + m_EffectDuration);
  258. #endif
  259. }
  260. }