lua 定时器以及应用

时间:2022-11-26 00:22:11
 
 
function update_timer_fun()
	for k,v in pairs(update_timer.m_process_time_fun) do
		v(k,update_timer.m_porcess_run_time[k]);
	end
end
--注册一个新的update定时器 经过over_time时间后销毁 销毁后回调over_fun(手动消耗也会回调over_fun方法)
update_timer.new_update = function(over_time,process_fun,over_fun)
	local t_over_time = over_time;
	if t_over_time == nil or t_over_time == 0 then
		t_over_time = update_timer_sd.over_time;
	end
	local t_over_time_id = gtime.create("update_timer.time_del_update",t_over_time,1);
	update_timer.m_process_time_fun[t_over_time_id] = process_fun;
	update_timer.m_over_time_fun[t_over_time_id] = over_fun;
	update_timer.m_porcess_run_time[t_over_time_id] = t_over_time;
	if update_timer.m_is_run_update == false then
		gscript.add_script(gscript.FUNCTION_UPDATE,"update_timer_fun");
		update_timer.m_is_run_update = true;
	end
	return t_over_time_id;
end

update_timer.time_del_update = function(time_id)
	if update_timer.m_process_time_fun[time_id] ~= nil then
		update_timer.m_process_time_fun[time_id] = nil;
	end
	if update_timer.m_over_time_fun[time_id] ~= nil and update_timer.m_porcess_run_time[time_id] ~= nil then
		update_timer.m_over_time_fun[time_id](time_id,update_timer.m_porcess_run_time[time_id]);
		update_timer.m_over_time_fun[time_id] = nil;
		update_timer.m_porcess_run_time[time_id] = nil;
	end
	if table.getall(update_timer.m_process_time_fun) == 0 then
		gscript.remove_script(gscript.FUNCTION_UPDATE,"update_timer_fun");
		update_timer.m_is_run_update = false;
	end
end

--旋转角度 从t_eff_time时间旋转 angle
panel_fun.eff_rotation = function(t_ctl,angle,t_eff_time)
	if t_ctl == nil then
		--sprint("\n eff_set_text error : not find t_ctl ! \n");
		return;
	end
	if angle == nil then
		--sprint("\n eff_set_text error : old num or new num is nil !!! \n");
		return;
	end
	panel_fun.rotation_ctl[t_ctl:get_pid()] = t_ctl;
	if panel_fun.rotation_time_id[t_ctl:get_pid()] ~= nil then
		if panel_fun.rotation_time_id[t_ctl:get_pid()] > 0 then
			update_timer.stop_one(panel_fun.rotation_time_id[t_ctl:get_pid()]);
			
		end
	end
	if t_eff_time ~= nil then
		panel_fun.rotation_eff_time[t_ctl:get_pid()] = t_eff_time;
	else
		panel_fun.rotation_eff_time[t_ctl:get_pid()] = gld.text_eff_time;
	end
	panel_fun.rotation_angle_num[t_ctl:get_pid()] = angle;
	
	panel_fun.rotation_time_id[t_ctl:get_pid()] = update_timer.new_update(panel_fun.rotation_eff_time[t_ctl:get_pid()],panel_fun.eff_rotation_start,panel_fun.eff_rotation_end);
	panel_fun.rotation_pid[panel_fun.rotation_time_id[t_ctl:get_pid()]] = t_ctl:get_pid();
end


panel_fun.eff_rotation_start = function(time_id)
	local t_pid = panel_fun.rotation_pid[time_id];
	if t_pid == nil or t_pid == 0 then
		return;
	end
	if panel_fun.rotation_angle_num[t_pid] == nil then
		return;
	end
	if panel_fun.rotation_ctl[t_pid] == nil then
		return;
	end
	if panel_fun.text_befor_time[t_pid] == nil or panel_fun.text_befor_time[t_pid] == 0 then
		panel_fun.text_befor_time[t_pid] = guntil.GetNowMsTime();
	end
	local t_interval_time =  guntil.GetNowMsTime() - panel_fun.text_befor_time[t_pid];
	panel_fun.text_befor_time[t_pid] = guntil.GetNowMsTime();
	if panel_fun.rotation_percent[t_pid] == nil then
		panel_fun.rotation_percent[t_pid] = (t_interval_time/panel_fun.rotation_eff_time[t_pid])*100;
	else
		panel_fun.rotation_percent[t_pid] = panel_fun.rotation_percent[t_pid] + (t_interval_time/panel_fun.rotation_eff_time[t_pid])*100;
	end
	if panel_fun.rotation_percent[t_pid] > 100 then
		panel_fun.rotation_percent[t_pid] = 100;
	end
	local t_angle = panel_fun.rotation_angle_num[t_pid]*panel_fun.rotation_percent[t_pid]/100;
	-- add_num = add_num - add_num % 1;
	local hand_x = panel_fun.rotation_ctl[t_pid]:get_width();
	local hand_y = panel_fun.rotation_ctl[t_pid]:get_height();
	panel_fun.rotation_ctl[t_pid]:set_rotation_anchor(hand_x / 2, hand_y / 2);
	panel_fun.rotation_ctl[t_pid]:set_rotation(t_angle);
end


panel_fun.eff_rotation_end = function(time_id)
	local t_pid = panel_fun.rotation_pid[time_id];
	if t_pid == nil or t_pid == 0 then
		return;
	end
	if panel_fun.rotation_angle_num[t_pid] == nil or then
		return;
	end
	if panel_fun.rotation_ctl[t_pid] == nil then
		return;
	end
	panel_fun.rotation_ctl[t_pid]:set_rotation(panel_fun.rotation_angle_num[t_pid]);
	--释放
	panel_fun.rotation_ctl[t_pid] = nil;
	panel_fun.rotation_time_id[t_pid] = nil;
	panel_fun.rotation_angle_num[t_pid] = nil;
	
	panel_fun.rotation_eff_time[t_pid] = nil;
	panel_fun.text_befor_time[t_pid] = nil;
	panel_fun.rotation_percent[t_pid] = nil;
	panel_fun.rotation_pid[t_pid] = nil;
end