wolfadmin/luascripts/util/timers.lua

73 lines
2.1 KiB
Lua
Raw Normal View History

2016-02-13 11:19:37 +00:00
-- WolfAdmin module for Wolfenstein: Enemy Territory servers.
2016-02-16 13:10:00 +00:00
-- Copyright (C) 2015-2016 Timo 'Timothy' Smit
2016-02-13 11:19:37 +00:00
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- at your option any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
local constants = require "luascripts.wolfadmin.util.constants"
local util = require "luascripts.wolfadmin.util.util"
local events = require "luascripts.wolfadmin.util.events"
local tables = require "luascripts.wolfadmin.util.tables"
2016-02-13 11:19:37 +00:00
local timers = {}
local data = {}
local nextId = 0
function timers.add(func, interval, rep, ...)
local args = {...}
table.insert(data, {
["id"] = nextId,
["function"] = func,
["start"] = et.trap_Milliseconds(),
["interval"] = interval,
["iteration"] = 0,
["repeat"] = rep,
["args"] = args
})
nextId = nextId + 1
return nextId - 1
end
function timers.remove(id)
for i = 1, #data do
if data[i]["id"] == id then
table.remove(data, i)
return
2016-02-13 11:19:37 +00:00
end
end
end
function timers.ongameframe(levelTime)
for id, timer in pairs(data) do
if (et.trap_Milliseconds() - timer["start"]) > timer["interval"] then
timer["function"](tables.unpack(timer["args"]))
2016-02-13 11:19:37 +00:00
timer["iteration"] = timer["iteration"] + 1
if timer["repeat"] == 0 or timer["iteration"] < timer["repeat"] then
timer["start"] = et.trap_Milliseconds()
else
timers.remove(timer["id"])
end
end
end
end
events.handle("onGameFrame", timers.ongameframe)
return timers