local ScriptAddon = CreateFrame("Frame", "ScriptAddon", nil)
function ScriptAddon:OnEvent(event, ...) -- functions created in "object:method"-style have an implicit first parameter of "self", which points to object
self[event](self, ...) -- route event parameters to ScriptAddon:event methods
end
ScriptAddon:SetScript("OnEvent", ScriptAddon.OnEvent)
ScriptAddon:RegisterEvent("UNIT_SPELLCAST_START")
function ScriptAddon:UNIT_SPELLCAST_START(unit)
local spell, rank, displayName, icon, startTime, endTime, isTradeSkill = UnitCastingInfo(unit)
if not spell then return end
if displayName == "Polymorph" then
self:SetScript("OnUpdate", self.OnUpdate)
self.unit = unit
self.total = 0
self.endTime = endTime/1000 -- this is equal to GetTime() at which the cast will end
self.unitName = UnitName(unit)
end
end
function ScriptAddon:OnUpdate(elapsed)
self.total = self.total + elapsed
-- only call function every 50ms to improve performance
if self.total >= 0.05 then
self.total = 0
local spell, rank, displayName, icon, startTime, endTime, isTradeSkill = UnitCastingInfo(self.unit)
if endTime and endTime/1000 - GetTime() <= 0.2 then
-- stop using performance heavy onupdate function if no spellcast is currently going on
self:SetScript("OnUpdate", nil)
TargetUnit(self.unit)
CastSpellByName("Shadow Word: Death")
elseif self.endTime-0.2 <= GetTime() then -- lost Unit (changed target e.g.), guessing cast timer
self:SetScript("OnUpdate", nil)
TargetUnit(self.unitName)
CastSpellByName("Shadow Word: Death")
end
end
end