源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

169 lines
5.5 KiB

--
-- Author: ZensYue
-- Date: 2017-10-25 17:51:21
--
TimeManager = TimeManager or BaseClass()
function TimeManager:__init()
TimeManager.Instance = self
self:Reset()
end
function TimeManager:Reset()
self.time_list = {}
end
function TimeManager.GetInstance()
if TimeManager.Instance == nil then
TimeManager.Instance = TimeManager.New()
end
return TimeManager.Instance
end
--[[
=================================
@Author: ZensYue
@DateTime: 2017-10-25 18:05:23
@Description: 不外部调用
=================================
]]
function TimeManager:SetTimeID(update_type,info)
self.time_list[update_type] = info
end
--获取活动定时器ID
function TimeManager:GetTimeID(update_type)
return self.time_list[update_type]
end
--取消活动定时器
function TimeManager:CancelTime(update_type)
local info = self:GetTimeID(update_type)
if not info then
return
end
if info.time_id then
GlobalTimerQuest:CancelQuest(info.time_id)
end
info.time_id = nil
info.next_update_time = 0
self:SetTimeID(update_type,info)
end
--[[
=================================
@Author: ZensYue
@DateTime: 2017-10-25 17:54:00
@param: update_type 更新类型 ps:背包... 直接用模块名就好,不用额外加配置
@param: time 几秒后执行(内部转成毫秒)
@param: callback 执行回调
@return bool 是否在保护时间内
=================================
]]
function TimeManager:StartTime(update_type,time,callback)
local info = self:GetTimeID(update_type)
local cur_time = TimeUtil:getClientTimeMs()
--当前次还未执行,不做处理
if info and info.next_update_time and info.next_update_time - cur_time > 0 then
info.count = info.count or 0
info.count = info.count + 1
return true
end
--记录转成毫秒 更准确
local next_update_time = cur_time + time*1000
self:CancelTime(update_type)
local function func()
local info = self:GetTimeID(update_type)
--期间有需要刷新,当前次结束再执行一次
if info and info.count and info.count > 0 then
if info.callback then
info.callback()
end
info.last_time = TimeUtil:getClientTimeMs()
info.count = 0
end
self:CancelTime(update_type)
end
local time_id = GlobalTimerQuest:AddPeriodQuest(func,time,1)
--如果第一次或者连续多次超过间隔时间马上执行
--或者极端情况下,上次的还未执行 info.time_id还存在
local count = 1
if not info or info.time_id or not info.last_time or not info.interval or cur_time - info.last_time > info.interval then
if callback then
callback()
end
--如果之前的定时器未关,先关掉
if info and info.time_id then
self:CancelTime(update_type)
end
count = 0
end
info = {
time_id = time_id, --定时器ID
next_update_time = next_update_time, --下次执行的时间
interval = time*1000, --间隔时间
count = count, --间隔时间内需要执行的数量
callback = callback --回调函数
}
self:SetTimeID(update_type,info)
return false
end
--[[
=================================
--一定时间内只刷一次
@param: update_type 更新类型 ps:背包... 直接用模块名就好,不用额外加配置
@param: time 几秒后执行(内部转成毫秒)
@param: callback 执行回调
@return bool 是否在保护时间内
=================================
]]
function TimeManager:StartDalayTime(update_type,time,callback)
local info = self:GetTimeID(update_type)
local cur_time = TimeUtil:getClientTimeMs()
--当前次还未执行,不做处理
if info and info.next_update_time and info.next_update_time - cur_time > 0 then
info.count = info.count or 0
info.count = info.count + 1
return true
end
--记录转成毫秒 更准确
local next_update_time = cur_time + time*1000
self:CancelTime(update_type)
local function func()
local info = self:GetTimeID(update_type)
--期间有需要刷新,当前次结束再执行一次
if info then
if info.callback then
info.callback()
end
info.last_time = TimeUtil:getClientTimeMs()
info.count = 0
end
self:CancelTime(update_type)
end
local time_id = GlobalTimerQuest:AddPeriodQuest(func,time,1)
--如果第一次或者连续多次超过间隔时间马上执行
--或者极端情况下,上次的还未执行 info.time_id还存在
local count = 1
if not info or info.time_id or not info.last_time or not info.interval or cur_time - info.last_time > info.interval then
--如果之前的定时器未关,先关掉
if info and info.time_id then
self:CancelTime(update_type)
end
count = 0
end
info = {
time_id = time_id, --定时器ID
next_update_time = next_update_time, --下次执行的时间
interval = time*1000, --间隔时间
count = count, --间隔时间内需要执行的数量
callback = callback --回调函数
}
self:SetTimeID(update_type,info)
return false
end
function TimeManager:__delete()
end