2022-07-29 21:12:54 +01:00
|
|
|
-----
|
|
|
|
--- Basic timer - after a specified number of seconds, the specified thing happens.
|
|
|
|
-- Usage:
|
2022-07-29 21:14:56 +01:00
|
|
|
-- local Timer = require("Timer")
|
2022-07-29 21:12:54 +01:00
|
|
|
--
|
|
|
|
-- LevelFuncs.FinishTimer = function(healthWhenStarted, victoryMessage)
|
|
|
|
-- DoSomething(healthWhenStarted, victoryMessage)
|
|
|
|
-- end
|
|
|
|
--
|
|
|
|
-- LevelFuncs.TriggerTimer = function(obj)
|
|
|
|
-- local myTimer = Timer.Create("my_timer", 5.0, false, "FinishTimer", Lara:GetHP(), "Well done!")
|
|
|
|
-- myTimer:Start()
|
|
|
|
-- end
|
|
|
|
--
|
2022-07-29 21:14:56 +01:00
|
|
|
-- LevelFuncs.OnControlPhase = function(dt)
|
|
|
|
-- Timer.UpdateAll(dt)
|
|
|
|
-- end
|
|
|
|
--
|
2022-07-29 21:12:54 +01:00
|
|
|
-- @luautil Timer
|
2022-07-28 22:18:20 +01:00
|
|
|
|
|
|
|
LevelVars.__TEN_timer = {timers = {}}
|
|
|
|
|
2022-07-29 21:12:54 +01:00
|
|
|
local Timer
|
|
|
|
|
2022-07-28 22:18:20 +01:00
|
|
|
Timer = {
|
2022-07-29 21:12:54 +01:00
|
|
|
--- Create (but do not start) a new timer.
|
|
|
|
-- @string name A label to give this timer; used to retrieve the timer later
|
|
|
|
-- @number totalTime The duration of the timer, in seconds
|
|
|
|
-- @bool loop if true, the timer will start again immediately after the time has elapsed
|
|
|
|
-- @string func The name of the LevelFunc member to call when the time is up
|
|
|
|
-- @param funcArgs the arguments with which the above function will be called
|
|
|
|
-- @return The timer in its paused state
|
2022-07-28 22:18:20 +01:00
|
|
|
Create = function(name, totalTime, loop, func, ...)
|
|
|
|
local obj = {}
|
|
|
|
local mt = {}
|
|
|
|
mt.__index = Timer
|
|
|
|
setmetatable(obj, mt)
|
|
|
|
|
|
|
|
obj.name = name
|
|
|
|
LevelVars.__TEN_timer.timers[name] ={}
|
|
|
|
local thisTimer = LevelVars.__TEN_timer.timers[name]
|
|
|
|
thisTimer.name = name
|
|
|
|
thisTimer.totalTime = totalTime
|
|
|
|
thisTimer.remainingTime = totalTime
|
|
|
|
thisTimer.func = func -- todo save name?
|
|
|
|
thisTimer.funcArgs = {...}
|
|
|
|
thisTimer.loop = loop
|
|
|
|
thisTimer.active = false
|
|
|
|
return obj
|
|
|
|
end;
|
|
|
|
|
2022-07-29 21:12:54 +01:00
|
|
|
--- Get a timer by its name.
|
|
|
|
-- @string name The label that was given to the timer when it was created
|
|
|
|
-- @return The timer
|
2022-07-28 22:18:20 +01:00
|
|
|
Get = function(name)
|
|
|
|
if LevelVars.__TEN_timer.timers[name] then
|
|
|
|
local obj = {}
|
|
|
|
local mt = {}
|
|
|
|
mt.__index = Timer
|
|
|
|
setmetatable(obj, mt)
|
|
|
|
obj.name = name
|
|
|
|
return obj
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end,
|
|
|
|
|
2022-07-29 21:12:54 +01:00
|
|
|
--- Give the timer a new function and args
|
|
|
|
-- @param t the timer in question
|
|
|
|
-- @string func The name of the LevelFunc member to call when the time is up
|
|
|
|
-- @param funcArgs the arguments with which the above function will be called
|
|
|
|
SetFunction = function(t, func, ...)
|
|
|
|
local thisTimer = LevelVars.__TEN_timer.timers[t.name]
|
|
|
|
thisTimer.func = func
|
|
|
|
thisTimer.funcArgs = {...}
|
|
|
|
end,
|
|
|
|
|
|
|
|
--- Begin or unpause a timer.
|
|
|
|
-- @param t the timer in question
|
|
|
|
Start = function(t)
|
|
|
|
local thisTimer = LevelVars.__TEN_timer.timers[t.name]
|
|
|
|
if not thisTimer.active then
|
|
|
|
thisTimer.active = true
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
|
|
|
|
--- Pause the timer
|
|
|
|
-- @param t the timer in question
|
2022-07-28 22:18:20 +01:00
|
|
|
Pause = function(t)
|
|
|
|
LevelVars.__TEN_timer.timers[t.name].active = false
|
|
|
|
end,
|
|
|
|
|
2022-07-29 21:12:54 +01:00
|
|
|
--- Get the remaining time for a timer
|
|
|
|
-- @param t the timer in question
|
|
|
|
-- @return the time in seconds remaining on the clock
|
2022-07-28 22:18:20 +01:00
|
|
|
GetRemainingTime = function(t)
|
|
|
|
return LevelVars.__TEN_timer.timers[t.name].remainingTime
|
|
|
|
end,
|
|
|
|
|
2022-07-29 21:12:54 +01:00
|
|
|
--- Set the remaining time for a timer
|
|
|
|
-- @param t the timer in question
|
|
|
|
-- @number remainingTime the new time remaining for the timer
|
2022-07-28 22:18:20 +01:00
|
|
|
SetRemainingTime = function(t, remainingTime)
|
|
|
|
LevelVars.__TEN_timer.timers[t.name].remainingTime = remainingTime
|
|
|
|
end,
|
|
|
|
|
2022-07-29 21:12:54 +01:00
|
|
|
--- Get the total time for a timer
|
|
|
|
-- This is the amount of time the timer will start with, as well as when starting a new loop
|
|
|
|
-- @param t the timer in question
|
|
|
|
-- @return the timer's total time
|
2022-07-28 22:18:20 +01:00
|
|
|
GetTotalTime = function(t)
|
|
|
|
return LevelVars.__TEN_timer.timers[t.name].totalTime
|
|
|
|
end,
|
|
|
|
|
2022-07-29 21:12:54 +01:00
|
|
|
--- Set the total time for a timer
|
|
|
|
-- @param t the timer in question
|
|
|
|
-- @number totalTime timer's new total time
|
2022-07-28 22:18:20 +01:00
|
|
|
SetTotalTime = function(t, totalTime)
|
|
|
|
LevelVars.__TEN_timer.timers[t.name].totalTime = totalTime
|
|
|
|
end,
|
|
|
|
|
2022-07-29 21:12:54 +01:00
|
|
|
--- Set whether or not the timer loops
|
|
|
|
-- @param t the timer in question
|
|
|
|
-- @bool looping whether or not the timer loops
|
2022-07-28 22:18:20 +01:00
|
|
|
SetLooping = function(t, looping)
|
|
|
|
LevelVars.__TEN_timer.timers[t.name].loop = looping
|
|
|
|
end,
|
|
|
|
|
2022-07-29 21:12:54 +01:00
|
|
|
--- Update all active timers.
|
|
|
|
-- Should be called in LevelFuncs.OnControlPhase
|
|
|
|
-- @number dt The time in seconds since the last frame
|
2022-07-28 22:18:20 +01:00
|
|
|
UpdateAll = function(dt)
|
|
|
|
for k, t in pairs(LevelVars.__TEN_timer.timers) do
|
|
|
|
Timer.Update(t, dt)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
|
|
|
|
Update = function(t, dt)
|
|
|
|
if t.active then
|
|
|
|
t.remainingTime = t.remainingTime - dt
|
|
|
|
|
|
|
|
if t.remainingTime <= 0 then
|
|
|
|
LevelFuncs[t.func](table.unpack(t.funcArgs))
|
2022-07-29 21:12:54 +01:00
|
|
|
|
|
|
|
local timeCarryOver = t.remainingTime
|
|
|
|
|
2022-07-28 22:18:20 +01:00
|
|
|
if not t.loop then
|
|
|
|
t.active = false
|
2022-07-29 21:12:54 +01:00
|
|
|
timeCarryOver = 0
|
2022-07-28 22:18:20 +01:00
|
|
|
end
|
2022-07-29 21:12:54 +01:00
|
|
|
|
|
|
|
t.remainingTime = t.totalTime + timeCarryOver
|
2022-07-28 22:18:20 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Timer
|
|
|
|
|