Use elapsed time for energy tick animation

This commit is contained in:
Dusk-92
2026-08-25 22:54:17 +02:00
parent 50306189cc
commit 6d65fdc259
+88 -85
View File
@@ -1,85 +1,88 @@
local T = ShaguTweaks.T local T = ShaguTweaks.T
local module = ShaguTweaks:register({ local module = ShaguTweaks:register({
title = T["Unit Frame Energy & Mana Tick"], title = T["Unit Frame Energy & Mana Tick"],
description = T["Adds an energy & mana tick to the player frame."], description = T["Adds an energy & mana tick to the player frame."],
expansions = { ["vanilla"] = true }, expansions = { ["vanilla"] = true },
category = T["Unit Frames"], category = T["Unit Frames"],
enabled = nil, enabled = nil,
}) })
module.enable = function(self) module.enable = function(self)
local energytick = CreateFrame("Frame", nil, PlayerFrameManaBar) local energytick = CreateFrame("Frame", nil, PlayerFrameManaBar)
energytick:SetAllPoints(PlayerFrameManaBar) energytick:SetAllPoints(PlayerFrameManaBar)
energytick:RegisterEvent("PLAYER_ENTERING_WORLD") energytick:RegisterEvent("PLAYER_ENTERING_WORLD")
energytick:RegisterEvent("UNIT_DISPLAYPOWER") energytick:RegisterEvent("UNIT_DISPLAYPOWER")
energytick:RegisterEvent("UNIT_ENERGY") energytick:RegisterEvent("UNIT_ENERGY")
energytick:RegisterEvent("UNIT_MANA") energytick:RegisterEvent("UNIT_MANA")
energytick:SetScript("OnEvent", function() energytick:SetScript("OnEvent", function()
if UnitPowerType("player") == 0 then if UnitPowerType("player") == 0 then
this.mode = "MANA" this.mode = "MANA"
-- hide if full mana and not in combat -- hide if full mana and not in combat
if (UnitMana("player") == UnitManaMax("player")) and (not UnitAffectingCombat("player")) then if (UnitMana("player") == UnitManaMax("player")) and (not UnitAffectingCombat("player")) then
this:Hide() this:Hide()
else else
this:Show() this:Show()
end end
elseif UnitPowerType("player") == 3 then elseif UnitPowerType("player") == 3 then
this.mode = "ENERGY" this.mode = "ENERGY"
this:Show() this:Show()
else else
this:Hide() this:Hide()
end end
if event == "PLAYER_ENTERING_WORLD" then if event == "PLAYER_ENTERING_WORLD" then
this.lastMana = UnitMana("player") this.lastMana = UnitMana("player")
end end
if (this.mode == "ENERGY") or ((event == "UNIT_MANA" or event == "UNIT_ENERGY") and arg1 == "player") then if (this.mode == "ENERGY") or ((event == "UNIT_MANA" or event == "UNIT_ENERGY") and arg1 == "player") then
this.currentMana = UnitMana("player") this.currentMana = UnitMana("player")
local diff = 0 local diff = 0
if this.lastMana then if this.lastMana then
diff = this.currentMana - this.lastMana diff = this.currentMana - this.lastMana
end end
if this.mode == "MANA" and diff < 0 then if this.mode == "MANA" and diff < 0 then
this.target = 5 this.target = 5
elseif this.mode == "MANA" and diff > 0 then elseif this.mode == "MANA" and diff > 0 then
if this.max ~= 5 and diff > (this.badtick and this.badtick*1.2 or 5) then if this.max ~= 5 and diff > (this.badtick and this.badtick*1.2 or 5) then
this.target = 2 this.target = 2
else else
this.badtick = diff this.badtick = diff
end end
elseif this.mode == "ENERGY" and diff >= 0 then elseif this.mode == "ENERGY" and diff >= 0 then
this.target = 2 this.target = 2
end end
this.lastMana = this.currentMana this.lastMana = this.currentMana
end end
end) end)
local pheight, pwidth = PlayerFrameManaBar:GetHeight(), PlayerFrameManaBar:GetWidth() local pheight, pwidth = PlayerFrameManaBar:GetHeight(), PlayerFrameManaBar:GetWidth()
energytick:SetScript("OnUpdate", function() energytick:SetScript("OnUpdate", function()
if this.target then if this.target then
this.start, this.max = GetTime(), this.target this.max = this.target
this.target = nil this.current = 0
end this.target = nil
end
if not this.start then return end
if not this.max then return end
this.current = GetTime() - this.start
-- OnUpdate already provides the exact elapsed frame time. Accumulating arg1
if this.current > this.max then -- keeps the spark just as smooth without querying GetTime() every frame.
this.start, this.max, this.current = GetTime(), 2, 0 this.current = (this.current or 0) + arg1
end
if this.current > this.max then
local pos = (pwidth ~= "-1" and pwidth or width) * (this.current / this.max) this.max, this.current = 2, 0
if not pheight then return end end
this.spark:SetPoint("LEFT", pos-((pheight+5)/2), 0)
end) local pos = (pwidth ~= "-1" and pwidth or width) * (this.current / this.max)
if not pheight then return end
energytick.spark = energytick:CreateTexture(nil, 'OVERLAY') this.spark:SetPoint("LEFT", pos-((pheight+5)/2), 0)
energytick.spark:SetTexture("Interface\\CastingBar\\UI-CastingBar-Spark") end)
energytick.spark:SetHeight(pheight + 10)
energytick.spark:SetWidth(pheight + 4) energytick.spark = energytick:CreateTexture(nil, 'OVERLAY')
energytick.spark:SetBlendMode('ADD') energytick.spark:SetTexture("Interface\\CastingBar\\UI-CastingBar-Spark")
end energytick.spark:SetHeight(pheight + 10)
energytick.spark:SetWidth(pheight + 4)
energytick.spark:SetBlendMode('ADD')
end