summaryrefslogtreecommitdiff
path: root/init.lua
diff options
context:
space:
mode:
authorWuzzy <almikes@aol.com>2015-02-11 20:40:19 +0100
committerWuzzy <almikes@aol.com>2015-02-11 20:40:19 +0100
commit10168288770c8ae1b8002d71b00290482eda2323 (patch)
treed7c5e799ea8ea1a23b3ace4ac39fed076aafa829 /init.lua
parent8cdd144d15b314f01d516998f80f4814060bfa3d (diff)
Optimize hb.change_hudbar to change hud_change less times
Diffstat (limited to 'init.lua')
-rw-r--r--init.lua44
1 files changed, 33 insertions, 11 deletions
diff --git a/init.lua b/init.lua
index 46604e0..fc2b2c1 100644
--- a/init.lua
+++ b/init.lua
@@ -123,10 +123,11 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
alignment = {x=-1,y=-1},
offset = offset,
})
+ local text = string.format(format_string, label, start_value, start_max)
ids.text = player:hud_add({
hud_elem_type = "text",
position = pos,
- text = tostring(string.format(format_string, label, start_value, start_max)),
+ text = text,
alignment = {x=1,y=1},
number = text_color,
direction = 0,
@@ -135,6 +136,8 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
state.hidden = start_hide
state.value = start_value
state.max = start_max
+ state.text = text
+ state.barlength = hb.value_to_barlength(start_value, start_max)
hb.hudtables[identifier].hudids[name] = ids
hb.hudtables[identifier].hudstate[name] = state
@@ -162,28 +165,47 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value)
local name = player:get_player_name()
local hudtable = hb.get_hudtable(identifier)
+ local value_changed, max_changed = false, false
if new_value ~= nil then
- hudtable.hudstate[name].value = new_value
+ if new_value ~= hudtable.hudstate[name].value then
+ hudtable.hudstate[name].value = new_value
+ value_changed = true
+ end
else
new_value = hudtable.hudstate[name].value
end
if new_max_value ~= nil then
- hudtable.hudstate[name].max = new_max_value
+ if new_max_value ~= hudtable.hudstate[name].max then
+ hudtable.hudstate[name].max = new_max_value
+ max_changed = true
+ end
else
new_max_value = hudtable.hudstate[name].max
end
if hudtable.hudstate[name].hidden == false then
- if hudtable.hudstate[name].max == 0 then
- player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
- else
- player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1})
+ if max_changed then
+ if hudtable.hudstate[name].max == 0 then
+ player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
+ else
+ player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1})
+ end
+ end
+
+ if value_changed or max_changed then
+ local new_barlength = hb.value_to_barlength(new_value, new_max_value)
+ if new_barlength ~= hudtable.hudstate[name].barlength then
+ player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(new_value, new_max_value))
+ hudtable.hudstate[name].barlength = new_barlength
+ end
+
+ local new_text = string.format(hudtable.format_string, hudtable.label, new_value, new_max_value)
+ if new_text ~= hudtable.hudstate[name].text then
+ player:hud_change(hudtable.hudids[name].text, "text", new_text)
+ hudtable.hudstate[name].text = new_text
+ end
end
- player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(new_value, new_max_value))
- player:hud_change(hudtable.hudids[name].text, "text",
- tostring(string.format(hudtable.format_string, hudtable.label, new_value, new_max_value))
- )
end
end