forked from Bastion/Bastion
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.4 KiB
74 lines
1.4 KiB
---@type Tinkr
|
|
local Tinkr,
|
|
---@class Bastion
|
|
Bastion = ...
|
|
|
|
-- Create a new Timer class
|
|
---@class Bastion.Timer
|
|
---@field type string
|
|
---@field cb? fun():boolean
|
|
local Timer = {
|
|
startTime = nil,
|
|
resetAfterCombat = false,
|
|
}
|
|
Timer.__index = Timer
|
|
|
|
-- Constructor
|
|
---@param type string
|
|
---@param cb? fun():boolean
|
|
---@return Bastion.Timer
|
|
function Timer:New(type, cb)
|
|
---@class Bastion.Timer
|
|
local self = setmetatable({}, Timer)
|
|
self.startTime = nil
|
|
self.type = type
|
|
self.checking = false
|
|
self.cb = cb
|
|
return self
|
|
end
|
|
|
|
-- Start the timer
|
|
---@return nil
|
|
function Timer:Start()
|
|
self.startTime = GetTime()
|
|
end
|
|
|
|
-- Get the time since the timer was started
|
|
---@return number
|
|
function Timer:GetTime()
|
|
if not self:IsRunning() then
|
|
return 0
|
|
end
|
|
return GetTime() - self.startTime
|
|
end
|
|
|
|
-- Check if the timer is running
|
|
---@return boolean
|
|
function Timer:IsRunning()
|
|
return self.startTime ~= nil
|
|
end
|
|
|
|
-- Reset the timer
|
|
---@return nil
|
|
function Timer:Reset()
|
|
self.startTime = nil
|
|
end
|
|
|
|
function Timer:IsChecking()
|
|
return self.checking
|
|
end
|
|
|
|
function Timer:Check()
|
|
self.checking = true
|
|
if self.cb then
|
|
local cbResult = self.cb()
|
|
if not self:IsRunning() and cbResult then
|
|
self:Start()
|
|
elseif self:IsRunning() and not cbResult then
|
|
self:Reset()
|
|
end
|
|
end
|
|
self.checking = false
|
|
end
|
|
|
|
Bastion.Timer = Timer
|
|
|