Update Timer and EventSequence to work with new subtables system.

This commit is contained in:
hispidence 2022-09-07 19:45:35 +01:00
parent 5ffe5bf067
commit cabbe2f85b
2 changed files with 49 additions and 45 deletions

View file

@ -4,7 +4,7 @@
-- Works atop the Timer, and so is updated automatically pre-OnControlPhase, and saved automatically when the game is saved.
--
-- Example usage:
-- local EventSequence = require("EventSequence")
-- local EventSequence = require("Engine.EventSequence")
--
-- -- These will be called by the sequence
-- LevelFuncs.HealLara = function()
@ -26,13 +26,13 @@
-- false, -- does not loop
-- {seconds = true, deciseconds = true}, -- timer format, see Timer for details
-- 6, -- seconds until call the function specified in next arg
-- "HealLara", -- first function to call. If we don't need to pass any arguments, we can just give the func name as a string
-- LevelFuncs.HealLara, -- first function to call. If we don't need to pass any arguments, we can just pass the function
-- 2.1, -- seconds until the next function, after the previous one has been called
-- {"SpawnBaddy", TEN.Objects.ObjID.BADDY1, "steve", posSteve}, -- if we DO want to pass arguments to the function to be called, we give a table with the name of the function ("SpawnBaddy" in this case) followed by the args to pass to it
-- {LevelFuncs.SpawnBaddy, TEN.Objects.ObjID.BADDY1, "steve", posSteve}, -- if we DO want to pass arguments to the function to be called, we give a table with the function (LevelFuncs.SpawnBaddy in this case) followed by the args to pass to it
-- 0.5,
-- {"SpawnBaddy", TEN.Objects.ObjID.SAS_CAIRO, "chris", posChris},
-- {LevelFuncs.SpawnBaddy, TEN.Objects.ObjID.SAS_CAIRO, "chris", posChris},
-- 1,
-- "HealLara")
-- LevelFuncs.HealLara)
--
-- -- event sequences are inactive to begin with and so need to be started
-- mySeq:Start()
@ -44,11 +44,12 @@ local Timer = require("Timer")
local EventSequence
LevelVars.__TEN_eventSequence = {sequences = {}}
LevelFuncs.Engine.EventSequence = {}
LevelVars.Engine.EventSequence = {sequences = {}}
LevelFuncs.__TEN_eventSequence_callNext = function(sequenceName, nextTimerName, func, ...)
local thisES = LevelVars.__TEN_eventSequence.sequences[sequenceName]
LevelFuncs[func](...)
LevelFuncs.Engine.EventSequence.CallNext = function(sequenceName, nextTimerName, func, ...)
local thisES = LevelVars.Engine.EventSequence.sequences[sequenceName]
func(...)
thisES.currentTimer = thisES.currentTimer + 1
if thisES.currentTimer <= #thisES.timers then
@ -80,9 +81,8 @@ EventSequence = {
setmetatable(obj, mt)
obj.name = name
LevelVars.__TEN_eventSequence.sequences[name] = {}
local thisES = LevelVars.__TEN_eventSequence.sequences[name]
LevelVars.Engine.EventSequence.sequences[name] = {}
local thisES = LevelVars.Engine.EventSequence.sequences[name]
thisES.name = name
thisES.timesFuncsAndArgs = {...}
thisES.loop = loop
@ -97,7 +97,6 @@ EventSequence = {
local nextTimer = i + 2
local timerIndex = #thisES.timers + 1
local funcName = "__TEN_eventSequence_" .. name .. "_func" .. timerIndex
local timerName = "__TEN_eventSequence_" .. name .. "_timer" .. timerIndex
local nextTimerName = "__TEN_eventSequence_" .. name .. "_timer" .. timerIndex + 1
@ -108,7 +107,7 @@ EventSequence = {
thisES.firstTimerName = timerName
end
if type(funcAndArgs) == "string" then
if type(funcAndArgs) == "userdata" then
-- we only have a function
func = funcAndArgs
funcAndArgs = {}
@ -121,7 +120,7 @@ EventSequence = {
tfa[i], -- time
false,
timerFormat,
"__TEN_eventSequence_callNext",
LevelFuncs.Engine.EventSequence.CallNext,
name,
nextTimerName,
func,
@ -138,7 +137,7 @@ EventSequence = {
-- @string name The label that was given to the sequence when it was created
-- @return The sequence
Get = function(name)
if LevelVars.__TEN_eventSequence.sequences[name] then
if LevelVars.Engine.EventSequence.sequences[name] then
local obj = {}
local mt = {}
mt.__index = EventSequence
@ -153,7 +152,7 @@ EventSequence = {
-- @function mySequence:SetPaused
-- @bool p if true, the sequence will be paused; if false, it will be unpaused
SetPaused = function(t, p)
local thisES = LevelVars.__TEN_eventSequence.sequences[t.name]
local thisES = LevelVars.Engine.EventSequence.sequences[t.name]
Timer.Get(thisES.timers[thisES.currentTimer]):SetPaused(p)
end;
@ -161,21 +160,21 @@ EventSequence = {
-- @function mySequence:IsPaused
-- @return true if the timer is paused, false if otherwise
IsPaused = function(t)
local thisES = LevelVars.__TEN_eventSequence.sequences[t.name]
local thisES = LevelVars.Engine.EventSequence.sequences[t.name]
return Timer.Get(thisES.timers[thisES.currentTimer]):IsPaused()
end;
--- Begin or unpause a sequence. If showing the remaining time on-screen, its color will be set to white.
-- @function mySequence:Start
Start = function(t)
local thisES = LevelVars.__TEN_eventSequence.sequences[t.name]
local thisES = LevelVars.Engine.EventSequence.sequences[t.name]
Timer.Get(thisES.timers[thisES.currentTimer]):Start()
end;
--- Stop the sequence.
--@function mySequence:Stop
Stop = function(t)
local thisES = LevelVars.__TEN_eventSequence.sequences[t.name]
local thisES = LevelVars.Engine.EventSequence.sequences[t.name]
Timer.Get(thisES.timers[thisES.currentTimer]):Stop()
end;
@ -183,7 +182,7 @@ EventSequence = {
-- @function mySequence:IsActive
-- @return true if the sequence is active, false if otherwise
IsActive = function(t)
local thisES = LevelVars.__TEN_eventSequence.sequences[t.name]
local thisES = LevelVars.Engine.EventSequence.sequences[t.name]
return Timer.Get(thisES.timers[thisES.currentTimer]):IsActive()
end;
}

View file

@ -4,7 +4,7 @@
-- Timers are updated automatically every frame before OnControlPhase.
--
-- Example usage:
-- local Timer = require("Timer")
-- local Timer = require("Engine.Timer")
--
-- -- This will be called when the timer runs out
-- LevelFuncs.FinishTimer = function(healthWhenStarted, victoryMessage)
@ -18,7 +18,7 @@
-- 5.0,
-- false,
-- {minutes = false, seconds = true, deciseconds = true},
-- "FinishTimer",
-- LevelFuncs.FinishTimer,
-- Lara:GetHP(),
-- "Well done!")
-- myTimer:Start()
@ -26,7 +26,8 @@
--
-- @luautil Timer
LevelVars.__TEN_timer = {timers = {}}
LevelFuncs.Engine.Timer = {}
LevelVars.Engine.Timer = {timers = {}}
local Timer
@ -62,7 +63,7 @@ Timer = {
-- @number totalTime The duration of the timer, in seconds
-- @bool loop if true, the timer will start again immediately after the time has elapsed
-- @tparam ?table|bool timerFormat If a table is given, the remaining time will be shown as a string, formatted according to the values in the table. If true, the remaining seconds, rounded up, will show at the bottom of the screen. If false, the remaining time will not be shown on screen.
-- @string func The name of the LevelFunc member to call when the time is upssss
-- @function func The LevelFunc function to call when the time is up
-- @param[opt] ... a variable number of arguments with which the above function will be called
-- @return The timer in its paused state
--
@ -73,8 +74,13 @@ Timer = {
setmetatable(obj, mt)
obj.name = name
LevelVars.__TEN_timer.timers[name] ={}
local thisTimer = LevelVars.__TEN_timer.timers[name]
if LevelVars.Engine.Timer.timers[name] then
print("Warning: a timer with name " .. name .. " already exists.")
end
LevelVars.Engine.Timer.timers[name] ={}
local thisTimer = LevelVars.Engine.Timer.timers[name]
thisTimer.name = name
thisTimer.totalTime = totalTime
thisTimer.remainingTime = totalTime
@ -95,7 +101,7 @@ Timer = {
-- @string name The label that was given to the timer when it was created
-- @return The timer
Get = function(name)
if LevelVars.__TEN_timer.timers[name] then
if LevelVars.Engine.Timer.timers[name] then
local obj = {}
local mt = {}
mt.__index = Timer
@ -112,8 +118,7 @@ Timer = {
t.remainingTime = t.remainingTime - dt
if t.remainingTime <= 0 then
LevelFuncs[t.func](table.unpack(t.funcArgs))
t.func(table.unpack(t.funcArgs))
if not t.loop then
t.active = false
else
@ -196,10 +201,10 @@ Timer = {
--- Give the timer a new function and args
-- @function myTimer:SetFunction
-- @string func The name of the LevelFunc member to call when the time is up
-- @tparam function func The LevelFunc member to call when the time is up
-- @param[opt] ... a variable number of arguments with which the above function will be called
SetFunction = function(t, func, ...)
local thisTimer = LevelVars.__TEN_timer.timers[t.name]
local thisTimer = LevelVars.Engine.Timer.timers[t.name]
thisTimer.func = func
thisTimer.funcArgs = {...}
end;
@ -207,7 +212,7 @@ Timer = {
--- Begin or unpause a timer. If showing the remaining time on-screen, its color will be set to white.
-- @function myTimer:Start
Start = function(t)
local thisTimer = LevelVars.__TEN_timer.timers[t.name]
local thisTimer = LevelVars.Engine.Timer.timers[t.name]
if not thisTimer.active then
thisTimer.active = true
end
@ -222,21 +227,21 @@ Timer = {
--- Stop the timer.
-- @function myTimer:Stop
Stop = function(t)
LevelVars.__TEN_timer.timers[t.name].active = false
LevelVars.Engine.Timer.timers[t.name].active = false
end;
--- Get whether or not the timer is active
-- @function myTimer:IsActive
-- @return true if the timer is active, false if otherwise
IsActive = function(t)
return LevelVars.__TEN_timer.timers[t.name].active
return LevelVars.Engine.Timer.timers[t.name].active
end;
--- Pause or unpause the timer. If showing the remaining time on-screen, its color will be set to yellow (paused) or white (unpaused).
-- @function myTimer:SetPaused
-- @bool p if true, the timer will be paused; if false, it would be unpaused
SetPaused = function(t, p)
local thisTimer = LevelVars.__TEN_timer.timers[t.name]
local thisTimer = LevelVars.Engine.Timer.timers[t.name]
thisTimer.paused = p
if thisTimer.timerFormat then
if p then
@ -251,21 +256,21 @@ Timer = {
-- @function myTimer:IsPaused
-- @return true if the timer is paused, false if otherwise
IsPaused = function(t)
return LevelVars.__TEN_timer.timers[t.name].paused
return LevelVars.Engine.Timer.timers[t.name].paused
end;
--- Get the remaining time for a timer.
-- @function myTimer:GetRemainingTime
-- @return the time in seconds remaining on the clock
GetRemainingTime = function(t)
return LevelVars.__TEN_timer.timers[t.name].remainingTime
return LevelVars.Engine.Timer.timers[t.name].remainingTime
end;
--- Set the remaining time for a timer
-- @function myTimer:SetRemainingTime
-- @number remainingTime the new time remaining for the timer
SetRemainingTime = function(t, remainingTime)
LevelVars.__TEN_timer.timers[t.name].remainingTime = remainingTime
LevelVars.Engine.Timer.timers[t.name].remainingTime = remainingTime
end;
--- Get the total time for a timer.
@ -273,31 +278,31 @@ Timer = {
-- @function myTimer:GetRemainingTime
-- @return the timer's total time
GetTotalTime = function(t)
return LevelVars.__TEN_timer.timers[t.name].totalTime
return LevelVars.Engine.Timer.timers[t.name].totalTime
end;
--- Set the total time for a timer
-- @function myTimer:SetTotalTime
-- @number totalTime timer's new total time
SetTotalTime = function(t, totalTime)
LevelVars.__TEN_timer.timers[t.name].totalTime = totalTime
LevelVars.Engine.Timer.timers[t.name].totalTime = totalTime
end;
--- Set whether or not the timer loops
-- @function myTimer:SetLooping
-- @bool looping whether or not the timer loops
SetLooping = function(t, looping)
LevelVars.__TEN_timer.timers[t.name].loop = looping
LevelVars.Engine.Timer.timers[t.name].loop = looping
end;
}
LevelFuncs.__TEN_timer_updateAll = function(dt)
for _, t in pairs(LevelVars.__TEN_timer.timers) do
LevelFuncs.Engine.Timer.UpdateAll = function(dt)
for _, t in pairs(LevelVars.Engine.Timer.timers) do
Timer.Update(t, dt)
end
end
TEN.Logic.AddCallback(TEN.Logic.CallbackPoint.PRECONTROLPHASE, "__TEN_timer_updateAll")
TEN.Logic.AddCallback(TEN.Logic.CallbackPoint.PRECONTROLPHASE, LevelFuncs.Engine.Timer.UpdateAll)
return Timer