Skip to content

Commit

Permalink
Merge pull request #48 from lilith-avatar/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
endaye authored May 20, 2021
2 parents 65d0689 + 1685210 commit 3ebecce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
--- 时间管理器模块
-- @module Module Time Manager
-- @copyright Lilith Games, Avatar Team
-- @author Bingyun Chen, Yuancheng Zhang
-- @see the functions defined by JavaScript syntax
--- @module Module Time Manager
--- @copyright Lilith Games, Avatar Team
--- @author Bingyun Chen, Yuancheng Zhang
--- @see the functions defined by JavaScript syntax

local TimeUtil = {}

Expand All @@ -17,6 +17,8 @@ local running = false
-- Set update delta time
local DELTA_TIME = .05

local tid = 0

--- Find all registered events to trigger
local function CheckEvents()
-- now = os.time()
Expand Down Expand Up @@ -74,16 +76,20 @@ function TimeUtil.Start()
end

--- Stop Update()
function TimeUtil.Stop()
--- @param _clear bool clear remain events
function TimeUtil.Stop(_clear)
running = false
if _clear then
eventList = {}
end
end

--- Call a function after a specified number of milliseconds,
-- use ClearTimeout() method to prevent the function from running
-- @param _func execution function to call
-- @param _delayTime
-- @return timer id
-- @see https://www.w3schools.com/jsref/met_win_settimeout.asp
--- use ClearTimeout() method to prevent the function from running
--- @param _func execution function to call
--- @param _delayTime
--- @return timer id
--- @see https://www.w3schools.com/jsref/met_win_settimeout.asp
function TimeUtil.SetTimeout(_func, _seconds)
assert(_func, '[TimeUtil] TimeUtil.SetTimeout() _func 不能为空')
assert(type(_func) == 'function', '[TimeUtil] TimeUtil.SetTimeout() _func 类型不是function')
Expand All @@ -93,48 +99,49 @@ function TimeUtil.SetTimeout(_func, _seconds)
invoke(_func)
return
end
local id = #eventList + 1
tid = tid + 1
local timestamp = _seconds + Timer.GetTime()
table.insert(
eventList,
{
id = id,
id = tid,
func = _func,
delay = _seconds,
triggerTime = timestamp
}
)
return id
return tid
end

--- Call a function or evaluates an expression at specified intervals (in milliseconds),
-- the method will continue calling the function until ClearInterval() is called, or the game is over.
-- @param _func execution function to call
-- @param _delayTime
-- @return timer id
-- @see https://www.w3schools.com/jsref/met_win_setinterval.asp
--- the method will continue calling the function until ClearInterval() is called, or the game is over.
--- @param _func execution function to call
--- @param _delayTime
--- @return timer id
--- @see https://www.w3schools.com/jsref/met_win_setinterval.asp
function TimeUtil.SetInterval(_func, _seconds)
assert(_func, '[TimeUtil] TimeUtil.SetInterval() _func 不能为空')
assert(type(_func) == 'function', '[TimeUtil] TimeUtil.SetInterval() _func 类型不是function')
assert(_seconds > 0, '[TimeUtil] TimeUtil.SetInterval() 延迟时间需大于0')
local id = #eventList + 1
local timestamp = _seconds + Timer.GetTime()
tid = tid + 1
table.insert(
eventList,
{
id = id,
id = tid,
func = _func,
delay = _seconds,
triggerTime = timestamp,
loop = true
}
)
return id
return tid
end

--- Clear a timer set with the SetTimeout() method
-- @param _id timmer id
-- @see https://www.w3schools.com/jsref/met_win_cleartimeout.asp
--- @param _id timmer id
--- @see https://www.w3schools.com/jsref/met_win_cleartimeout.asp
function TimeUtil.ClearTimeout(_id)
for k, e in pairs(eventList) do
if e.id == _id then
Expand All @@ -145,7 +152,7 @@ function TimeUtil.ClearTimeout(_id)
end

--- Clear a timer set with the SetInterval() method, used as ClearTimeout()
-- @see https://www.w3schools.com/jsref/met_win_clearinterval.asp
--- @see https://www.w3schools.com/jsref/met_win_clearinterval.asp
TimeUtil.ClearInterval = TimeUtil.ClearTimeout

return TimeUtil
Binary file modified Smap/avatar-ava.smap
Binary file not shown.

0 comments on commit 3ebecce

Please sign in to comment.