--[[
file name : 技能模块第一版
author : Clark/陈泽丹
created : 2012-11-07
purpose :
--]]
module("程序", package.seeall)
PACK_GameAPI = require"系统接口"
--时间触发器管理者
function new_timer_manager()
local uid_manager = PACK_GameAPI.new_index_manager()
local timer_tgr_res = {}
local public = {}
--创建时间触发器
function public.new_timer_tgr( _time, _callback )
local uid = uid_manager.create_index()
local t =
{
tgr_uid = uid,
need_time = _time,
left_time = _time,
callback = _callback,
}
timer_tgr_res[ uid ] = t
return uid
end
--删除时间触发器
function public.delete_timer_tgr( _uid )
timer_tgr_res[ _uid ] = nil
uid_manager.delete_index( _uid )
end
local sta_time = os.time()
--模糊触发
function public.on_vague_time()
local time_unit = 1
local cur_time = os.time()
local dt = cur_time - sta_time
if dt > time_unit then
sta_time = cur_time
local t = PACK_GameAPI.get_t_set_by_v(timer_tgr_res)
local len = table.getn( t )
for i=1, len do
--模糊触发
t[i].left_time = t[i].left_time - dt
if t[i].left_time <= 0 then
t[i].left_time = t[i].need_time
t[i].callback( t[i].tgr_uid )
end
end
end
end
--精确触发
function public.on_exact_time()
local time_unit = 1
local cur_time = os.time()
local dt = cur_time - sta_time
if dt > time_unit then
sta_time = cur_time
local t = PACK_GameAPI.get_t_set_by_v(timer_tgr_res)
local len = table.getn( t )
for i=1, len do
--精确触发
t[i].left_time = t[i].left_time - 1
if t[i].left_time <= 0 then
t[i].left_time = t[i].need_time
t[i].callback( t[i].tgr_uid )
end
end
end
end
return public
end
G_timer_manager = new_timer_manager()
function test( _dt, _is_del )
local function do_test( _tgr_id )
print( "时间触发器在" .. _dt .. "秒后触发" )
if _is_del then
G_timer_manager.delete_timer_tgr( _tgr_id )
end
end
return do_test
end
function main()
--初始化模块
local tm_tgr_uid = G_timer_manager.new_timer_tgr(4, test(4, false))
local tm_tgr_uid = G_timer_manager.new_timer_tgr(1, test(1, false))
local tm_tgr_uid = G_timer_manager.new_timer_tgr(2, test(2, true))
while true do
G_timer_manager.on_exact_time()
end
end
main()
定时触发器
最新推荐文章于 2020-05-02 16:15:46 发布