Re-initial commit after testing

main
Bradley 2 years ago
commit 260375c067
  1. 71
      Vision/EventManager/EventManager.lua
  2. 27
      Vision/TimeManager/TimeManager.lua
  3. 49
      Vision/Timer/Timer.lua
  4. 34
      _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,27 @@
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 = {}
return self
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)
return self.timers[label]
end
end
return TimeManager

@ -0,0 +1,49 @@
local Tinkr, Vision = ...
local Timer = {
start_events = {},
reset_events = {},
time = 0,
label = nil
}
Timer.__index = Timer
-- Constructor
---@param label String
---@param start Table
---@param reset Table
---@return Timer
function Timer:New(label, start, reset)
local self = setmetatable({}, Timer)
self.start_events = start
self.reset_events = reset
self.time = 0
self.label = label
for _, event in pairs(self.start_events) do
Vision.EventManager:RegisterWoWEvent(event, function()
self.time = GetTime()
end)
end
for _, event in pairs(self.reset_events) do
Vision.EventManager:RegisterWoWEvent(event, function()
self.time = 0
end)
end
return self
end
-- Start Timer
---@return number
function Timer:GetTime()
if self.time == 0 then
return 0
else
return GetTime() - self.time
end
end
return Timer

@ -0,0 +1,34 @@
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"):New()
---@type Timer
Vision.Timer = Vision.require("Timer")
---@type TimeManager
Vision.TimeManager = Vision.require("TimeManager"):New()
local DeathTimer = Vision.TimeManager:GetTimer("DeathTimer", { "PLAYER_DEAD" }, { "PLAYER_UNGHOST" })
Vision.Enabled = true
Vision.Ticker = C_Timer.NewTicker(1, function()
if Vision.Enabled then
if DeathTimer:GetTime() > 60 then
print("We have been dead for a minute or more.")
end
end
end)
Loading…
Cancel
Save