定时触发器

--[[
 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值