From aa5889fb127fdb712ff2839f8204d21b2f014f6f Mon Sep 17 00:00:00 2001 From: Bradley <117424218+decentbradley@users.noreply.github.com> Date: Fri, 30 Jun 2023 13:45:13 -0400 Subject: [PATCH] Manager for Timers and events, as well as an example time for tracking how long the player is dead. --- Vision/EventManager/EventManager.lua | 71 ++++++++++++++++++++++++++++ Vision/TimeManager/TimeManager.lua | 24 ++++++++++ _vision.lua | 30 ++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 Vision/EventManager/EventManager.lua create mode 100644 Vision/TimeManager/TimeManager.lua create mode 100644 _vision.lua 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..c64c79b --- /dev/null +++ b/Vision/TimeManager/TimeManager.lua @@ -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 \ No newline at end of file diff --git a/_vision.lua b/_vision.lua new file mode 100644 index 0000000..6f3c6f4 --- /dev/null +++ b/_vision.lua @@ -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) \ No newline at end of file