commit 260375c067ca3d34c33102c5fbd64ca15d470f49 Author: Bradley Date: Sat Jul 1 11:57:30 2023 -0400 Re-initial commit after testing diff --git a/Vision/EventManager/EventManager.lua b/Vision/EventManager/EventManager.lua new file mode 100644 index 0000000..409a445 --- /dev/null +++ b/Vision/EventManager/EventManager.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 diff --git a/Vision/TimeManager/TimeManager.lua b/Vision/TimeManager/TimeManager.lua new file mode 100644 index 0000000..dc1927d --- /dev/null +++ b/Vision/TimeManager/TimeManager.lua @@ -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 \ No newline at end of file diff --git a/Vision/Timer/Timer.lua b/Vision/Timer/Timer.lua new file mode 100644 index 0000000..232e33a --- /dev/null +++ b/Vision/Timer/Timer.lua @@ -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 \ No newline at end of file diff --git a/_vision.lua b/_vision.lua new file mode 100644 index 0000000..070bce1 --- /dev/null +++ b/_vision.lua @@ -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) \ No newline at end of file