First and foremost, stop doing this:
Code:
for _, event in next, {
"UNIT_COMBO_POINTS",
"UNIT_POWER",
} do
caelUI.powersound:RegisterUnitEvent(event, "player")
end
There is absolutely no benefit to registering events this way. You're just wasting memory on a table and CPU on a loop. If you were dynamically altering the list of events to register this
might make sense, but for a fixed list of 2 events, just do it normally:
Code:
caelUI.powersound:RegisterUnitEvent("UNIT_COMBO_POINTS", "player")
caelUI.powersound:RegisterUnitEvent("UNIT_POWER", "player")
Other than that, my only complaints are stylistic:
(1) Rather than POWER_TYPE and SPELL_POWER_TYPE, I'd use variable names that more clearly identified what they are, such as POWER_TYPE_NAME and POWER_TYPE_ID.
(2) Mixing upvalues and local variables like this seems sloppy:
Code:
local floor, numPrevious = math.floor, 0
Plus you missed a few upvalues:
Code:
local floor, UnitPower, UnitPowerMax = math.floor, UnitPower, UnitPowerMax
local numPrevious = 0
Actually there is an actual problem; you forgot to define MAX_POWER as a local variable at the top there, so you're leaking a global.
Lua Code:
local _, caelUI = ...
-- Upvalues
local floor, UnitPower, UnitPowerMax = math.floor, UnitPower, UnitPowerMax
-- Variables
local numPrevious = 0
local POWER_TYPE_NAME, POWER_TYPE_ID, MAX_POWER
-- Create frame and register global events
caelUI.powersound = caelUI.createModule("PowerSound")
caelUI.powersound:RegisterUnitEvent("UNIT_COMBO_POINTS", "player")
caelUI.powersound:RegisterUnitEvent("UNIT_POWER", "player")
-- Define class variables and register class events
if caelUI.playerClass == "MONK" then
POWER_TYPE_NAME, POWER_TYPE_ID = "CHI", SPELL_POWER_CHI
elseif caelUI.playerClass == "PALADIN" then
POWER_TYPE_NAME, POWER_TYPE_ID = "HOLY_POWER", SPELL_POWER_HOLY_POWER
elseif caelUI.playerClass == "PRIEST" then
POWER_TYPE_NAME, POWER_TYPE_ID, MAX_POWER = "SHADOW_ORBS", SPELL_POWER_SHADOW_ORBS, PRIEST_BAR_NUM_ORBS
elseif caelUI.playerClass == "WARLOCK" then
caelUI.powersound:RegisterEvent("PLAYER_LOGIN")
caelUI.powersound:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
end
-- Many events, all sides, handle it!
caelUI.powersound:SetScript("OnEvent", function(self, event, unit, powerType)
if event == "UNIT_POWER" and powerType ~= POWER_TYPE_NAME then return end
if event == "PLAYER_LOGIN" or event == "PLAYER_SPECIALIZATION_CHANGED" then
local spec = GetSpecialization()
if spec == SPEC_WARLOCK_AFFLICTION then
POWER_TYPE_NAME, POWER_TYPE_ID = "SOUL_SHARDS", SPELL_POWER_SOUL_SHARDS
-- elseif spec == SPEC_WARLOCK_DEMONOLOGY then
-- POWER_TYPE_NAME, POWER_TYPE_ID = "DEMONIC_FURY", SPELL_POWER_DEMONIC_FURY
elseif spec == SPEC_WARLOCK_DESTRUCTION then
POWER_TYPE_NAME, POWER_TYPE_ID = "BURNING_EMBERS", SPELL_POWER_BURNING_EMBERS
end
return
end
local num, numMax
if event == "UNIT_COMBO_POINTS" then
num = GetComboPoints("player", "target")
numMax = MAX_COMBO_POINTS
else
num = UnitPower(unit, POWER_TYPE_ID)
numMax = MAX_POWER or UnitPowerMax(unit, POWER_TYPE_ID)
end
if num > numPrevious then
if num == numMax then
PlaySoundFile(caelMedia.files.soundComboMax, "Master")
else
PlaySoundFile(caelMedia.files.soundCombo, "Master")
end
end
numPrevious = num
end)