Skip to content

Commit 7775a01

Browse files
committed
optimisations
1 parent 6a91fe7 commit 7775a01

File tree

1 file changed

+45
-61
lines changed

1 file changed

+45
-61
lines changed

NumericUI/scripts/mods/NumericUI/HudElementDodgeCount.lua

Lines changed: 45 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,25 @@ local color_efficient = Color.terminal_text_header(255, true)
2020
local color_inefficient = Color.ui_hud_warp_charge_low(255, true)
2121
local color_limit = Color.ui_hud_warp_charge_high(255, true)
2222

23-
local timer_x_offset = (-UIWorkspaceSettings.screen.size[1]
24-
+ scenegraph_definition.container.size[1])/2
23+
local timer_x_offset = (-UIWorkspaceSettings.screen.size[1] + scenegraph_definition.container.size[1]) / 2
2524

25+
local timer_size = { 0, 0 }
26+
local timer_color = { 0, 0, 0, 0 }
2627
local timer_size_color = function(time_to_refresh, cooldown, current_dodges, force_show_max_width)
2728
-- NB: time_to_refresh will increase towards zero
2829
local t_max = cooldown
2930
if (-time_to_refresh >= t_max or force_show_max_width) and mod:get("dodge_timer_hide_full") then
3031
return
3132
end
3233
local natural_time = force_show_max_width and 0 or math.clamp((time_to_refresh + t_max) / t_max, 0, 1)
33-
local timer_size = {
34-
mod:get("dodge_timer_width") * (1 - natural_time),
35-
mod:get("dodge_timer_height"),
36-
}
37-
local timer_color = {}
34+
35+
timer_size[1] = mod:get("dodge_timer_width") * (1 - natural_time)
36+
timer_size[2] = mod:get("dodge_timer_height")
37+
3838
local color_start = Color[mod:get("color_start")](255, true)
3939
local color_end = Color[mod:get("color_end")](255, true)
4040
for i = 1, 4 do
41-
table.insert(
42-
timer_color,
43-
math.lerp(
44-
color_start[i],
45-
color_end[i],
46-
natural_time
47-
)
48-
)
41+
timer_color[i] = math.lerp(color_start[i], color_end[i], natural_time)
4942
end
5043
-- Hide timer if dodges are full (useful when playing with the Agile blessing)
5144
if current_dodges == 0 then
@@ -54,25 +47,6 @@ local timer_size_color = function(time_to_refresh, cooldown, current_dodges, for
5447
return timer_size, timer_color
5548
end
5649

57-
local timer_pass = { {
58-
style_id = "timer",
59-
pass_type = "rect",
60-
style = {
61-
color = {255, 255, 100, 100},
62-
vertical_alignment = "top",
63-
horizontal_alignment = "center",
64-
drop_shadow = true,
65-
size = {100, 8},
66-
offset = {
67-
timer_x_offset,
68-
mod:get("dodge_timer_y_offset"),
69-
},
70-
},
71-
visibility_function = function(content, style)
72-
return mod:get("dodge_timer")
73-
end,
74-
} }
75-
7650
local style = {
7751
line_spacing = 1.2,
7852
font_size = 25,
@@ -94,11 +68,26 @@ local widget_definitions = {
9468
"container"
9569
),
9670

97-
dodge_timer = UIWidget.create_definition(
98-
timer_pass
99-
,
100-
"container"
101-
),
71+
dodge_timer = UIWidget.create_definition({
72+
{
73+
style_id = "timer",
74+
pass_type = "rect",
75+
style = {
76+
color = { 255, 255, 100, 100 },
77+
vertical_alignment = "top",
78+
horizontal_alignment = "center",
79+
drop_shadow = true,
80+
size = { 100, 8 },
81+
offset = {
82+
timer_x_offset,
83+
mod:get("dodge_timer_y_offset"),
84+
},
85+
},
86+
visibility_function = function(_content, _style)
87+
return mod:get("dodge_timer")
88+
end,
89+
},
90+
}, "container"),
10291

10392
debug_dodge_count = UIWidget.create_definition({
10493
{
@@ -164,12 +153,13 @@ local function _calculate_dodge_diminishing_return(
164153
return consecutive_dodges, dr_start, dr_limit, diminishing_return
165154
end
166155

156+
local ZERO_SIZE = { 0, 0 }
167157
HudElementDodgeCount.update = function(self, dt, t, ui_renderer, render_settings, input_service)
168158
HudElementDodgeCount.super.update(self, dt, t, ui_renderer, render_settings, input_service)
169159
-- Reset to empty in case we can't fill it in
170160
self._widgets_by_name.dodge_count.content.text = ""
171161
self._widgets_by_name.debug_dodge_count.content.text = ""
172-
self._widgets_by_name.dodge_timer.style.timer.size = {0, 0}
162+
self._widgets_by_name.dodge_timer.style.timer.size = ZERO_SIZE
173163

174164
if self._is_in_hub or not mod:get("dodge_count") then
175165
return
@@ -200,23 +190,23 @@ HudElementDodgeCount.update = function(self, dt, t, ui_renderer, render_settings
200190

201191
local archetype = unit_data_extension:archetype()
202192
local base_dodge_template = archetype.dodge
203-
local weapon_consecutive_dodges_reset = weapon_dodge_template and weapon_dodge_template.consecutive_dodges_reset or 0
193+
local weapon_consecutive_dodges_reset = weapon_dodge_template and weapon_dodge_template.consecutive_dodges_reset
194+
or 0
204195
local stat_buffs = buff_extension:stat_buffs()
205196
local buff_modifier = stat_buffs.dodge_cooldown_reset_modifier
206197
local buff_dodge_cooldown_reset_modifier = buff_modifier and 1 - (buff_modifier - 1) or 1
207-
local relative_cooldown = (base_dodge_template.consecutive_dodges_reset + weapon_consecutive_dodges_reset) * buff_dodge_cooldown_reset_modifier
198+
local relative_cooldown = (base_dodge_template.consecutive_dodges_reset + weapon_consecutive_dodges_reset)
199+
* buff_dodge_cooldown_reset_modifier
208200

209-
local is_actually_dodging = (movement_state_component.method ~= "vaulting") and movement_state_component.is_dodging
201+
local is_actually_dodging = (movement_state_component.method ~= "vaulting")
202+
and movement_state_component.is_dodging
210203
local relative_time = gameplay_t - dodge_state_component.consecutive_dodges_cooldown
211-
local force_show_max_width = current_dodges ~= 0 and (is_actually_dodging or movement_state_component.method == "sliding")
212-
local timer_size, timer_color = timer_size_color(
213-
relative_time,
214-
relative_cooldown,
215-
current_dodges,
216-
force_show_max_width
217-
)
204+
local force_show_max_width = current_dodges ~= 0
205+
and (is_actually_dodging or movement_state_component.method == "sliding")
206+
local timer_size, timer_color =
207+
timer_size_color(relative_time, relative_cooldown, current_dodges, force_show_max_width)
218208

219-
self._widgets_by_name.dodge_timer.style.timer.size = timer_size or {0, 0}
209+
self._widgets_by_name.dodge_timer.style.timer.size = timer_size or ZERO_SIZE
220210
self._widgets_by_name.dodge_timer.style.timer.color = timer_color or Color.text_default(0, true)
221211

222212
if num_efficient_dodges == math.huge then
@@ -228,11 +218,8 @@ HudElementDodgeCount.update = function(self, dt, t, ui_renderer, render_settings
228218
or (math.ceil(num_efficient_dodges) - current_dodges)
229219

230220
if mod:get("show_efficient_dodges") then
231-
self._widgets_by_name.dodge_count.content.text = string.format(
232-
"%d/%d",
233-
display_dodges,
234-
math.ceil(num_efficient_dodges)
235-
)
221+
self._widgets_by_name.dodge_count.content.text =
222+
string.format("%d/%d", display_dodges, math.ceil(num_efficient_dodges))
236223
else
237224
self._widgets_by_name.dodge_count.content.text = tostring(math.ceil(display_dodges))
238225
end
@@ -247,11 +234,8 @@ HudElementDodgeCount.update = function(self, dt, t, ui_renderer, render_settings
247234
end
248235

249236
if mod:get("fade_out_max_dodges") and current_dodges == 0 then
250-
local time_since_cooldown = math.clamp(
251-
gameplay_t - dodge_state_component.consecutive_dodges_cooldown - 1,
252-
0,
253-
1
254-
)
237+
local time_since_cooldown =
238+
math.clamp(gameplay_t - dodge_state_component.consecutive_dodges_cooldown - 1, 0, 1)
255239
style.text.text_color[1] = math.lerp(255, 0, time_since_cooldown)
256240
end
257241

0 commit comments

Comments
 (0)