Manager for Timers and events, as well as an example time for tracking how long the player is dead.

Bradley 2 years ago
parent 77c7f53867
commit aa5889fb12
  1. 71
      Vision/EventManager/EventManager.lua
  2. 24
      Vision/TimeManager/TimeManager.lua
  3. 30
      _vision.lua

@ -0,0 +1,71 @@
-- Create an EventManager class
---@class EventManager
local EventManager = {
events = {},
eventHandlers = {},
wowEventHandlers = {},
frame = nil
}
EventManager.__index = EventManager
-- Constructor
---@return EventManager
function EventManager:New()
local self = setmetatable({}, EventManager)
self.events = {}
self.eventHandlers = {}
self.wowEventHandlers = {}
-- Frame for wow events
self.frame = CreateFrame("Frame")
self.frame:SetScript('OnEvent', function(f, event, ...)
if self.wowEventHandlers[event] then
for _, callback in ipairs(self.wowEventHandlers[event]) do
callback(...)
end
end
end)
return self
end
-- Register an event
---@param event string
---@param handler fun(...)
---@return nil
function EventManager:RegisterEvent(event, handler)
if not self.events[event] then
self.events[event] = {}
end
table.insert(self.events[event], handler)
end
-- Register a wow event
---@param event string
---@param handler fun(...)
---@return nil
function EventManager:RegisterWoWEvent(event, handler)
if not self.wowEventHandlers[event] then
self.wowEventHandlers[event] = {}
self.frame:RegisterEvent(event)
end
table.insert(self.wowEventHandlers[event], handler)
end
-- Trigger an event
---@param event string
---@param ... any
---@return nil
function EventManager:TriggerEvent(event, ...)
if self.events[event] then
for _, handler in pairs(self.events[event]) do
handler(...)
end
end
end
return EventManager

@ -0,0 +1,24 @@
local Tinkr, Vision = ...
local Timer = Vision.Timer
local TimeManager = {
timers = {},
}
TimeManager.__index = TimeManager
-- Constructor
---@return TimeManager
function TimeManager:New()
local self = setmetatable({}, TimeManager)
self.timers = {}
end
function TimeManager:GetTimer(label, start_events, reset_events)
if self.timers[label] == nil then
self.timers[label] = Timer:New(label, start_events, reset_events)
else
return self.timers[label]
end
end

@ -0,0 +1,30 @@
local Tinkr = ...
---@class Vision
local Vision = {
DebugMode = false
}
Vision.__index = Vision
function Vision.require(class)
return require("scripts/Vision/" .. class .. "/" .. class, Vision)
end
---@type EventManager
Vision.EventManager = Vision.require("EventManager")
---@type TimeManager
Vision.TimeManager = Vision.require("TimeManager"):New()
---@type Timer
Vision.Timer = Vision.require("Timer")
local DeathTimer = Vision.TimeManager:GetTimer("DeathTimer", { "PLAYER_DEAD" }, { "PLAYER_ALIVE", "PLAYER_UNGHOST" })
Vision.Ticker = C_Timer.NewTicker(0.1, function()
if Vision.Enabled then
if DeathTimer:GetTime() > 60 then
-- do something
end
end
end)
Loading…
Cancel
Save