Add timers library
This commit is contained in:
parent
e30a49af3b
commit
c2c1bc372d
72
timers.lua
Normal file
72
timers.lua
Normal file
@ -0,0 +1,72 @@
|
||||
-- Timer (not mine lol) (i genuinely forget who i stole the original code from
|
||||
-- so even if i knew there's nothing i could do)
|
||||
-- TODO investigate if events can replace some of this
|
||||
timers={}
|
||||
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
|
||||
timers.wait=wait
|
||||
|
||||
-- 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
|
||||
timers.namedWait=namedWait
|
||||
|
||||
-- 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 timers.cooldown(ticks+1, name) then
|
||||
timers.namedWait(ticks, next, name)
|
||||
end
|
||||
end
|
||||
|
||||
timers.rateLimit=rateLimit
|
||||
|
||||
return timers
|
Loading…
x
Reference in New Issue
Block a user