mirror of
https://github.com/CFC-Servers/cfc_detached_timer.git
synced 2025-03-04 03:03:18 -05:00
Initial
This commit is contained in:
parent
ba2775316a
commit
aa12e6b279
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,6 +1,9 @@
|
||||
# Compiled Lua sources
|
||||
luac.out
|
||||
|
||||
# Projects
|
||||
*.sublime-*
|
||||
|
||||
# luarocks build files
|
||||
*.src.rock
|
||||
*.zip
|
||||
|
1
lua/autorun/cfc_detached_timer_init.lua
Normal file
1
lua/autorun/cfc_detached_timer_init.lua
Normal file
@ -0,0 +1 @@
|
||||
AddCSLuaFile "includes/modules/cfc_detached_timer.lua"
|
96
lua/includes/modules/cfc_detached_timer.lua
Normal file
96
lua/includes/modules/cfc_detached_timer.lua
Normal file
@ -0,0 +1,96 @@
|
||||
dTimer = dTimer or {
|
||||
timers = {},
|
||||
idCounter = 0
|
||||
}
|
||||
|
||||
function dTimer.GetCurTime()
|
||||
return SysTime()
|
||||
end
|
||||
|
||||
function dTimer.Create( id, delay, reps, f )
|
||||
dTimer.timers[id] = { id = id, delay = delay, reps = reps, func = f, lastCall = dTimer.GetCurTime() }
|
||||
end
|
||||
|
||||
function dTimer.Adjust( id, delay, reps, f )
|
||||
local curTimer = dTimer.timers[id]
|
||||
if not curTimer then return false end
|
||||
|
||||
curTimer.delay = delay or curTimer.delay
|
||||
curTimer.reps = reps or curTimer.reps
|
||||
curTimer.func = f or curTimer.f
|
||||
return true
|
||||
end
|
||||
|
||||
function dTimer.Exists( id )
|
||||
return tobool( dTimer.timers[id] )
|
||||
end
|
||||
|
||||
function dTimer.Remove( id )
|
||||
dTimer.timers[id] = nil
|
||||
end
|
||||
|
||||
function dTimer.RepsLeft( id )
|
||||
return dTimer.timers[id] and dTimer.timers[id].reps or -1
|
||||
end
|
||||
|
||||
function dTimer.TimeLeft( id )
|
||||
local curTimer = dTimer.timers[id]
|
||||
if not curTimer then return end
|
||||
|
||||
return curTimer.delay - ( dTimer.GetCurTime() - curTimer.lastCall )
|
||||
end
|
||||
|
||||
function dTimer.Simple( delay, f )
|
||||
dTimer.Create( "SimpleTimer" .. dTimer.idCounter, delay, 1, f )
|
||||
dTimer.idCounter = dTimer.idCounter + 1
|
||||
end
|
||||
|
||||
function dTimer.Pause( id )
|
||||
local curTimer = dTimer.timers[id]
|
||||
if not curTimer or curTimer.pauseTime then return false end
|
||||
|
||||
curTimer.pauseTime = dTimer.GetCurTime()
|
||||
return true
|
||||
end
|
||||
|
||||
function dTimer.Stop( id )
|
||||
local curTimer = dTimer.timers[id]
|
||||
if not curTimer or curTimer.pauseTime then return false end
|
||||
|
||||
curTimer.pauseTime = curTimer.lastCall
|
||||
return true
|
||||
end
|
||||
|
||||
function dTimer.Start( id )
|
||||
local curTimer = dTimer.timers[id]
|
||||
if not curTimer or not curTimer.pauseTime then return false end
|
||||
|
||||
curTimer.lastCall = dTimer.GetCurTime() - ( curTimer.pauseTime - curTimer.lastCall )
|
||||
curTimer.pauseTime = nil
|
||||
return true
|
||||
end
|
||||
dTimer.UnPause = dTimer.Start
|
||||
|
||||
hook.Add( "Think", "CFC_DetatchedTimer_HandleTimers", function()
|
||||
local time = dTimer.GetCurTime()
|
||||
for id, curTimer in pairs( dTimer.timers ) do
|
||||
if curTimer.pauseTime then continue end
|
||||
|
||||
local delayPassed = time - curTimer.lastCall > curTimer.delay
|
||||
|
||||
if not delayPassed then continue end
|
||||
|
||||
curTimer.lastCall = time
|
||||
|
||||
local hasReps = curTimer.reps > 0
|
||||
|
||||
if hasReps then
|
||||
curTimer.reps = curTimer.reps - 1
|
||||
if curTimer.reps == 0 then
|
||||
dTimer.Remove( id )
|
||||
end
|
||||
end
|
||||
|
||||
curTimer.func()
|
||||
end
|
||||
end )
|
Loading…
Reference in New Issue
Block a user