源战役客户端
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

  1. --
  2. -- Author: ZensYue
  3. -- Date: 2017-10-25 17:51:21
  4. --
  5. TimeManager = TimeManager or BaseClass()
  6. function TimeManager:__init()
  7. TimeManager.Instance = self
  8. self:Reset()
  9. end
  10. function TimeManager:Reset()
  11. self.time_list = {}
  12. end
  13. function TimeManager.GetInstance()
  14. if TimeManager.Instance == nil then
  15. TimeManager.Instance = TimeManager.New()
  16. end
  17. return TimeManager.Instance
  18. end
  19. --[[
  20. =================================
  21. @Author: ZensYue
  22. @DateTime: 2017-10-25 18:05:23
  23. @Description:
  24. =================================
  25. ]]
  26. function TimeManager:SetTimeID(update_type,info)
  27. self.time_list[update_type] = info
  28. end
  29. --获取活动定时器ID
  30. function TimeManager:GetTimeID(update_type)
  31. return self.time_list[update_type]
  32. end
  33. --取消活动定时器
  34. function TimeManager:CancelTime(update_type)
  35. local info = self:GetTimeID(update_type)
  36. if not info then
  37. return
  38. end
  39. if info.time_id then
  40. GlobalTimerQuest:CancelQuest(info.time_id)
  41. end
  42. info.time_id = nil
  43. info.next_update_time = 0
  44. self:SetTimeID(update_type,info)
  45. end
  46. --[[
  47. =================================
  48. @Author: ZensYue
  49. @DateTime: 2017-10-25 17:54:00
  50. @param: update_type ps...
  51. @param: time
  52. @param: callback
  53. @return bool
  54. =================================
  55. ]]
  56. function TimeManager:StartTime(update_type,time,callback)
  57. local info = self:GetTimeID(update_type)
  58. local cur_time = TimeUtil:getClientTimeMs()
  59. --当前次还未执行,不做处理
  60. if info and info.next_update_time and info.next_update_time - cur_time > 0 then
  61. info.count = info.count or 0
  62. info.count = info.count + 1
  63. return true
  64. end
  65. --记录转成毫秒 更准确
  66. local next_update_time = cur_time + time*1000
  67. self:CancelTime(update_type)
  68. local function func()
  69. local info = self:GetTimeID(update_type)
  70. --期间有需要刷新,当前次结束再执行一次
  71. if info and info.count and info.count > 0 then
  72. if info.callback then
  73. info.callback()
  74. end
  75. info.last_time = TimeUtil:getClientTimeMs()
  76. info.count = 0
  77. end
  78. self:CancelTime(update_type)
  79. end
  80. local time_id = GlobalTimerQuest:AddPeriodQuest(func,time,1)
  81. --如果第一次或者连续多次超过间隔时间马上执行
  82. --或者极端情况下,上次的还未执行 info.time_id还存在
  83. local count = 1
  84. 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
  85. if callback then
  86. callback()
  87. end
  88. --如果之前的定时器未关,先关掉
  89. if info and info.time_id then
  90. self:CancelTime(update_type)
  91. end
  92. count = 0
  93. end
  94. info = {
  95. time_id = time_id, --定时器ID
  96. next_update_time = next_update_time, --下次执行的时间
  97. interval = time*1000, --间隔时间
  98. count = count, --间隔时间内需要执行的数量
  99. callback = callback --回调函数
  100. }
  101. self:SetTimeID(update_type,info)
  102. return false
  103. end
  104. --[[
  105. =================================
  106. --一定时间内只刷一次
  107. @param: update_type ps...
  108. @param: time
  109. @param: callback
  110. @return bool
  111. =================================
  112. ]]
  113. function TimeManager:StartDalayTime(update_type,time,callback)
  114. local info = self:GetTimeID(update_type)
  115. local cur_time = TimeUtil:getClientTimeMs()
  116. --当前次还未执行,不做处理
  117. if info and info.next_update_time and info.next_update_time - cur_time > 0 then
  118. info.count = info.count or 0
  119. info.count = info.count + 1
  120. return true
  121. end
  122. --记录转成毫秒 更准确
  123. local next_update_time = cur_time + time*1000
  124. self:CancelTime(update_type)
  125. local function func()
  126. local info = self:GetTimeID(update_type)
  127. --期间有需要刷新,当前次结束再执行一次
  128. if info then
  129. if info.callback then
  130. info.callback()
  131. end
  132. info.last_time = TimeUtil:getClientTimeMs()
  133. info.count = 0
  134. end
  135. self:CancelTime(update_type)
  136. end
  137. local time_id = GlobalTimerQuest:AddPeriodQuest(func,time,1)
  138. --如果第一次或者连续多次超过间隔时间马上执行
  139. --或者极端情况下,上次的还未执行 info.time_id还存在
  140. local count = 1
  141. 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
  142. --如果之前的定时器未关,先关掉
  143. if info and info.time_id then
  144. self:CancelTime(update_type)
  145. end
  146. count = 0
  147. end
  148. info = {
  149. time_id = time_id, --定时器ID
  150. next_update_time = next_update_time, --下次执行的时间
  151. interval = time*1000, --间隔时间
  152. count = count, --间隔时间内需要执行的数量
  153. callback = callback --回调函数
  154. }
  155. self:SetTimeID(update_type,info)
  156. return false
  157. end
  158. function TimeManager:__delete()
  159. end