-
Notifications
You must be signed in to change notification settings - Fork 0
/
Forge_TargetTracker.lua
140 lines (111 loc) · 3.99 KB
/
Forge_TargetTracker.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
Forge._TargetTracker = {}
function Forge._TargetTracker:Construct(pTargetName, pNotifyFunc, pNotifyParam)
self.TargetName = pTargetName
self.TrackingUnitID = nil
self.NotifyFunc = pNotifyFunc
self.NotifyParam = pNotifyParam
self.TargetIsInCombat = false
self.TargetLostCombatTime = nil
self.PlayerIsInCombat = false
self.Health = 0
self.HealthMax = 0
self.Mana = 0
self.ManaMax = 0
end
function Forge._TargetTracker:StartTracking()
self.TargetIsInCombat = false
self.SchedulerLib:ScheduleRepeatingTask(0.25, self.Update, self)
end
function Forge._TargetTracker:StopTracking(pTargetName, pNotifyFunc, pNotifyParam)
self.SchedulerLib:UnscheduleTask(self.Update, self)
end
function Forge._TargetTracker:Update()
-- If there's no unit or the unit is no longer tracking the target
-- then search for a new unit
if not self.TrackingUnitID
or not UnitExists(self.TrackingUnitID)
or not UnitExists(self.TrackingUnitID.."target")
or UnitName(self.TrackingUnitID.."target") ~= self.TargetName then
local vUnitID = MCRaidLib:FindUnitWithTarget(self.TargetName)
local vPresenceChanged = (vUnitID == nil) ~= (self.TrackingUnitID == nil)
self.TrackingUnitID = vUnitID
if vPresenceChanged and self.NotifyFunc then
self.NotifyFunc(self.NotifyParam, self, "FOUND", vUnitID ~= nil)
end
if not self.TrackingUnitID then
self:SetHealthMana(0, 0, 0, 0)
return
end
end
local vTargetUnitID = self.TrackingUnitID.."target"
self:SetHealthMana(
UnitHealth(vTargetUnitID),
UnitHealthMax(vTargetUnitID),
UnitPower(vTargetUnitID),
UnitPowerMax(vTargetUnitID))
local vTargetTargetUnitID = vTargetUnitID.."target"
local vTargetName = UnitName(vTargetTargetUnitID)
local vTargetIsInCombat = vTargetName ~= nil and MCRaidLib.Players[vTargetName] ~= nil
-- If the target was in combat but no longer appears to have a target, force a
-- delay before we're sure he's left combat
if vTargetIsInCombat then
self.TargetLostCombatTime = nil
elseif self.TargetIsInCombat
and UnitAffectingCombat(vTargetUnitID) then
vTargetIsInCombat = true
elseif self.TargetIsInCombat then
if not self.TargetLostCombatTime then
self.TargetLostCombatTime = GetTime()
vTargetIsInCombat = true
elseif (GetTime() - self.TargetLostCombatTime) < 5 then
vTargetIsInCombat = true
end
end
-- If the target is in combat then consider the player in combat, otherwise if the raid is out
-- of combat then don't count the player as in combat anymore
if vTargetIsInCombat then
self.PlayerIsInCombat = true
elseif not MCRaidLib.PlayerInCombat then
self.PlayerIsInCombat = false
end
-- Force the target as in combat if the player is in combat to prevent false triggers
-- from stuns/fears/etc
if not vTargetIsInCombat
and self.PlayerIsInCombat then
vTargetIsInCombat = true
end
-- Notify clients when the target enters or leaves combat
if self.TargetIsInCombat ~= vTargetIsInCombat then
self.TargetIsInCombat = vTargetIsInCombat
if self.NotifyFunc then
self.NotifyFunc(self.NotifyParam, self, "COMBAT", vTargetIsInCombat)
end
end
-- Notify clients if the target canges
if self.TrackingUnitID then
local vTargetTargetName = UnitName(self.TrackingUnitID.."targettarget")
if vTargetTargetName ~= self.TargetTargetName then
self.TargetTargetName = vTargetTargetName
if self.NotifyFunc then
self.NotifyFunc(self.NotifyParam, self, "TARGET_CHANGED", vTargetTargetName)
end
end
else
self.TargetTargetName = nil
end
end
function Forge._TargetTracker:SetHealthMana(pHealth, pHealthMax, pMana, pManaMax)
if self.Health == pHealth
and self.HealthMax == pHealthMax
and self.Mana == pMana
and self.ManaMax == pManaMax then
return
end
self.Health = pHealth
self.HealthMax = pHealthMax
self.Mana = pMana
self.ManaMax = pManaMax
if self.NotifyFunc then
self.NotifyFunc(self.NotifyParam, self, "HEALTHMANA", true)
end
end