Advertisement
Aussiemon

ChargeLevelIndicator.lua

Jan 24th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. local mod = {}
  2. local mod_name = "ChargeLevelIndicator"
  3. local oi = OptionsInjector
  4.  
  5. --[[
  6. authors: walterr
  7.  
  8. Charge Level Indicator.
  9. * When charging the Bolt staff or Fireball staff, a colored marker will be shown on the HUD
  10. indicating the current 'charge level'.
  11. The Bolt staff has discrete charge levels, which are shown as: green=1, orange=2, red=3.
  12. The Fireball staff doesn't have discrete levels and charging just continuously increases
  13. damage and area, so the levels I've chosen are kind of arbitrary:
  14. green=(3 <= damage < 5, 0.75 <= area < 2.25), orange=(5 <= damage < 6, 2.25 <= area < 3),
  15. red=(damage = 6, area = 3). (Where 'damage' means AoE instantaneous damage to normal
  16. targets, not including impact damage or DoT.)
  17. --]]
  18.  
  19. mod.anim_state = {
  20. STARTING = 1,
  21. ONGOING = 2,
  22. }
  23.  
  24. mod.widget_settings = {
  25. -- The charge level indicator is just a rounded rect. The color is set dynamically.
  26. CHARGE_LEVEL = {
  27. scenegraph_id = "charge_bar",
  28. element = {
  29. passes = {
  30. {
  31. pass_type = "rounded_background",
  32. style_id = "indicator"
  33. },
  34. },
  35. },
  36. content = {},
  37. style = {
  38. indicator = {
  39. offset = { 230, -45 },
  40. size = { 48, 48 },
  41. corner_radius = 3,
  42. },
  43. },
  44. }
  45. }
  46.  
  47. mod.current_charge_level = {
  48. color = nil,
  49. fade_out = nil,
  50. }
  51.  
  52. -- Defines the charge levels for the staffs.
  53. mod.display_info = {
  54. -- Fireball staff uses this, ActionCharge is defined in
  55. -- scripts/unit_extensions/weapons/actions/action_charge.lua
  56. ActionCharge = {
  57. levels = {
  58. {
  59. min_value = 1,
  60. color = Colors.get_table("red"),
  61. },
  62. {
  63. min_value = (2/3),
  64. color = Colors.get_table("orange"),
  65. },
  66. {
  67. min_value = 0,
  68. color = Colors.get_table("green_yellow"),
  69. },
  70. },
  71. },
  72. -- Bolt staff uses this, ActionTrueFlightBowAim is defined in
  73. -- scripts/unit_extensions/weapons/actions/action_true_flight_bow_aim.lua
  74. ActionTrueFlightBowAim = {
  75. levels = {
  76. {
  77. min_value = 1,
  78. color = Colors.get_table("red"),
  79. },
  80. {
  81. min_value = (0.8 / 1.25),
  82. color = Colors.get_table("orange"),
  83. },
  84. {
  85. min_value = 0,
  86. color = Colors.get_table("yellow_green"),
  87. },
  88. },
  89. },
  90. }
  91.  
  92. -- ####################################################################################################################
  93. -- ##### Functions ####################################################################################################
  94. -- ####################################################################################################################
  95. mod.display_info.ActionCharge.compute_level_value = function(action)
  96. if action.overcharge_extension and not action.current_action.vent_overcharge then
  97. return action.charge_level
  98. end
  99. return nil
  100. end
  101. mod.display_info.ActionTrueFlightBowAim.compute_level_value = function(action)
  102. return (action.overcharge_extension and action.charge_value) or nil
  103. end
  104.  
  105. local compute_level_color = function(self, charge_value)
  106. for _, level in ipairs(self.levels) do
  107. if charge_value >= level.min_value then
  108. return level.color
  109. end
  110. end
  111. return nil
  112. end
  113. mod.display_info.ActionCharge.compute_level_color = compute_level_color
  114. mod.display_info.ActionTrueFlightBowAim.compute_level_color = compute_level_color
  115.  
  116. -- ####################################################################################################################
  117. -- ##### Hook #########################################################################################################
  118. -- ####################################################################################################################
  119. Mods.hook.set(mod_name, "ActionCharge.client_owner_post_update", function (func, self, dt, t, ...)
  120. func(self, dt, t, ...)
  121.  
  122. local display_info = mod.display_info["ActionCharge"]
  123. local charge_value = display_info.compute_level_value(self)
  124. if charge_value then
  125. mod.current_charge_level.color = display_info:compute_level_color(charge_value)
  126. mod.current_charge_level.fade_out = nil
  127. end
  128. end)
  129.  
  130. Mods.hook.set(mod_name, "ActionCharge.finish", function (func, self, reason)
  131. local result = func(self, reason)
  132.  
  133. local display_info = mod.display_info["ActionCharge"]
  134. if reason == "new_interupting_action" then
  135. local charge_value = display_info.compute_level_value(self)
  136. if charge_value then
  137. mod.current_charge_level.color = display_info:compute_level_color(charge_value)
  138. mod.current_charge_level.fade_out = mod.anim_state.STARTING
  139. end
  140. else
  141. mod.current_charge_level.color = nil
  142. end
  143.  
  144. return result
  145. end)
  146.  
  147. Mods.hook.set(mod_name, "ActionTrueFlightBowAim.client_owner_post_update", function (func, self, dt, t, ...)
  148. func(self, dt, t, ...)
  149.  
  150. local display_info = mod.display_info["ActionTrueFlightBowAim"]
  151. local charge_value = display_info.compute_level_value(self)
  152. if charge_value then
  153. mod.current_charge_level.color = display_info:compute_level_color(charge_value)
  154. mod.current_charge_level.fade_out = nil
  155. end
  156. end)
  157.  
  158. Mods.hook.set(mod_name, "ActionTrueFlightBowAim.finish", function (func, self, reason)
  159. local result = func(self, reason)
  160.  
  161. local display_info = mod.display_info["ActionTrueFlightBowAim"]
  162. if reason == "new_interupting_action" then
  163. local charge_value = display_info.compute_level_value(self)
  164. if charge_value then
  165. mod.current_charge_level.color = display_info:compute_level_color(charge_value)
  166. mod.current_charge_level.fade_out = mod.anim_state.STARTING
  167. end
  168. else
  169. mod.current_charge_level.color = nil
  170. end
  171.  
  172. return result
  173. end)
  174.  
  175. Mods.hook.set(mod_name, "OverchargeBarUI.update", function(func, self, dt, ...)
  176. -- We use drawn_test to tell whether the overcharge bar was actually drawn (this avoids
  177. -- having to duplicate all the tests in OverchargeBarUI._update_overcharge). If it was
  178. -- drawn, drawn_test.angle will be set after calling func.
  179. local drawn_test = self.charge_bar.style.white_divider_left
  180. drawn_test.angle = nil
  181. func(self, dt, ...)
  182. if not drawn_test.angle then
  183. return
  184. end
  185.  
  186. if mod.current_charge_level.color then
  187. local widget = self._hudmod_charge_level_indicator
  188. if not widget then
  189. -- First use of the charge level indicator, create it now.
  190. widget = UIWidget.init(mod.widget_settings.CHARGE_LEVEL)
  191. self._hudmod_charge_level_indicator = widget
  192. end
  193.  
  194. if mod.current_charge_level.fade_out == mod.anim_state.ONGOING and not UIWidget.has_animation(widget) then
  195. -- Fade-out animation just finished.
  196. mod.current_charge_level.color = nil
  197. mod.current_charge_level.fade_out = nil
  198. else
  199. if mod.current_charge_level.fade_out == mod.anim_state.STARTING then
  200. -- Start the fade-out animation.
  201. local color = table.clone(mod.current_charge_level.color)
  202. widget.style.indicator.color = color
  203. UIWidget.animate(widget, UIAnimation.init(UIAnimation.function_by_time, color, 1, 255, 0, 1, math.easeInCubic))
  204. mod.current_charge_level.fade_out = mod.anim_state.ONGOING
  205.  
  206. elseif not mod.current_charge_level.fade_out then
  207. if UIWidget.has_animation(widget) then
  208. -- A new charge has begun while we're still fading out the last one, cancel animation.
  209. UIWidget.stop_animations(widget)
  210. end
  211. widget.style.indicator.color = mod.current_charge_level.color
  212. end
  213.  
  214. -- Draw the charge level indicator.
  215. local input_service = self.input_manager:get_service("ingame_menu")
  216. local ui_renderer = self.ui_renderer
  217. UIRenderer.begin_pass(ui_renderer, self.ui_scenegraph, input_service, dt, nil, self.render_settings)
  218. UIRenderer.draw_widget(ui_renderer, widget)
  219. UIRenderer.end_pass(ui_renderer)
  220. end
  221. end
  222. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement