定时触发器

来源:互联网 发布:tomcat7域名绑定项目 编辑:程序博客网 时间:2024/06/06 05:48
--[[ 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 ] = treturn uidend--删除时间触发器function public.delete_timer_tgr( _uid )timer_tgr_res[ _uid ] = niluid_manager.delete_index( _uid )endlocal sta_time = os.time()--模糊触发function public.on_vague_time()local time_unit = 1local cur_time = os.time()local dt = cur_time - sta_timeif dt > time_unit thensta_time = cur_timelocal 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 - dtif t[i].left_time <= 0 thent[i].left_time = t[i].need_timet[i].callback( t[i].tgr_uid )endendendend--精确触发function public.on_exact_time()local time_unit = 1local cur_time = os.time()local dt = cur_time - sta_timeif dt > time_unit thensta_time = cur_timelocal 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 - 1if t[i].left_time <= 0 thent[i].left_time = t[i].need_timet[i].callback( t[i].tgr_uid )endendendendreturn publicendG_timer_manager = new_timer_manager()function test( _dt, _is_del )local function do_test( _tgr_id )print( "时间触发器在" .. _dt .. "秒后触发" )if _is_del thenG_timer_manager.delete_timer_tgr( _tgr_id )endendreturn do_testendfunction 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 doG_timer_manager.on_exact_time()endendmain()

原创粉丝点击