Split off timers into library

This commit is contained in:
NullBite 2023-06-21 21:14:39 -04:00
parent 7cc97b9756
commit 12717afd32
Signed by: nullbite
GPG Key ID: 6C4D545385D4925A
2 changed files with 4 additions and 70 deletions

@ -1 +1 @@
Subproject commit e30a49af3b84499652d80fcf956e575569dc692f Subproject commit c2c1bc372d17cdda06f0971f94c334e011e4de72

View File

@ -16,73 +16,7 @@ TEXTURE_HEIGHT = 256
util = require("nulllib.util") util = require("nulllib.util")
logging = require("nulllib.logging") logging = require("nulllib.logging")
timers=require("nulllib.timers")
-- Timer (not mine lol) -- {{{
-- TODO investigate if events can replace some of this
do
local timers = {}
function wait(ticks,next)
table.insert(timers, {t=world.getTime()+ticks,n=next})
end
local function tick()
for key,timer in pairs(timers) do
if world.getTime() >= timer.t then
timer.n()
table.remove(timers,key)
end
end
end
events.TICK:register(function() if player then tick() end end, "timer")
end
-- named timers (this one is mine but heavily based on the other) --
-- if timer is armed twice before expiring it will only be called once) --
do
local timers = {}
function namedWait(ticks, next, name)
-- main difference, this will overwrite an existing timer with
-- the same name
timers[name]={t=world.getTime()+ticks,n=next}
end
local function tick()
for key, timer in pairs(timers) do
if world.getTime() >= timer.t then
timer.n()
timers[key]=nil
end
end
end
events.TICK:register(function() if player then tick() end end, "named_timer")
end
-- named cooldowns
do
local timers={}
function cooldown(ticks, name)
if timers[name] == nil then
timers[name]={t=world.getTime()+ticks}
return true
end
return false
end
local function tick()
for key, timer in pairs(timers) do
if world.getTime() >= timer.t then
timers[key]=nil
end
end
end
events.TICK:register(function() if player then tick() end end, "cooldown")
end
function rateLimit(ticks, next, name)
if cooldown(ticks+1, name) then
namedWait(ticks, next, name)
end
end
-- }}}
-- syncState {{{ -- syncState {{{
do do
@ -405,7 +339,7 @@ do
-- This one is for more explicit "flashes" such as player hurt -- This one is for more explicit "flashes" such as player hurt
-- animations, get color explicitly -- animations, get color explicitly
setColor(COLORS[expression]) setColor(COLORS[expression])
namedWait(ticks, resetExpression, "resetExpression") timers.namedWait(ticks, resetExpression, "resetExpression")
end end
function resetExpression() function resetExpression()
lock_color=false lock_color=false
@ -757,7 +691,7 @@ function tick()
if world.getTimeOfDay() % 20 == 0 then if world.getTimeOfDay() % 20 == 0 then
if player:getPose() == "SLEEPING" then if player:getPose() == "SLEEPING" then
if cooldown(20*4, "snore") then if timers.cooldown(20*4, "snore") then
snore() snore()
end end
end end