源战役客户端
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 rivejä
4.4 KiB

4 viikkoa sitten
  1. --scale false 采用deltaTime计时,true 采用 unscaledDeltaTime计时
  2. --初略的版本 以后有时间再优化完善
  3. TimerQuest = TimerQuest or BaseClass()
  4. local TimerQuest = TimerQuest
  5. local Time = Time
  6. local Status = Status
  7. local Array = Array
  8. local table_insert = table.insert
  9. local table_sort = table.sort
  10. function TimerQuest:__init()
  11. self.timer_list = {}
  12. self.update_delta_time = 0
  13. self.timer_list2 = {}
  14. self.update_delta_time2 = 0
  15. self.timer_list3 = {}
  16. self.update_delta_time3 = 0
  17. self.update_count = 0
  18. self.waitfor_call_back_list = false
  19. self.timer_list_index = 0
  20. self.timer_cache_queue = Array.New()
  21. LateUpdateBeat:Add(TimerQuest.Update,self)
  22. end
  23. function TimerQuest:AddPeriodQuest(onLeftTimeHandler,duration,loop,smooth)
  24. self.timer_list_index = self.timer_list_index + 1
  25. local timer = nil
  26. if Array.GetSize(self.timer_cache_queue) > 0 then
  27. timer = Array.PopFront(self.timer_cache_queue)
  28. timer.add_time = self.timer_list_index
  29. timer.duration = duration
  30. timer.loop = loop or -1
  31. timer.func = onLeftTimeHandler
  32. timer.time = duration
  33. timer.count = Time.frameCount + 1
  34. timer.smooth = smooth --smooth会忽略每一帧跟duration的校正差值,保证每次回调周期都是一致的,但是会导致总时间延长
  35. -- timer.source = PrintFunctionCallPos("AddPeriodQuest")
  36. else
  37. timer = {
  38. add_time = self.timer_list_index,
  39. duration = duration,
  40. loop = loop or -1,
  41. func = onLeftTimeHandler,
  42. time = duration,
  43. -- source = PrintFunctionCallPos("AddPeriodQuest"),
  44. count = Time.frameCount + 1,
  45. smooth = false,
  46. }
  47. end
  48. if duration <= 0.6 then
  49. self.timer_list[self.timer_list_index] = timer
  50. elseif duration <= 2 then
  51. self.timer_list2[self.timer_list_index] = timer
  52. else
  53. self.timer_list3[self.timer_list_index] = timer
  54. end
  55. return self.timer_list_index
  56. end
  57. function TimerQuest:AddDelayQuest(onLeftTimeHandler,duration)
  58. self.timer_list_index = self.timer_list_index + 1
  59. local timer = nil
  60. if Array.GetSize(self.timer_cache_queue) > 0 then
  61. timer = Array.PopFront(self.timer_cache_queue)
  62. timer.add_time = self.timer_list_index
  63. timer.duration = duration
  64. timer.loop = 0
  65. timer.func = onLeftTimeHandler
  66. timer.time = duration
  67. timer.count = Time.frameCount + 1
  68. timer.smooth = false
  69. else
  70. timer = {
  71. add_time = self.timer_list_index,
  72. duration = duration,
  73. loop = 0,
  74. func = onLeftTimeHandler,
  75. time = duration,
  76. count = Time.frameCount + 1,
  77. smooth = false,
  78. }
  79. end
  80. if duration <= 0.6 then
  81. self.timer_list[self.timer_list_index] = timer
  82. elseif duration <= 2 then
  83. self.timer_list2[self.timer_list_index] = timer
  84. else
  85. self.timer_list3[self.timer_list_index] = timer
  86. end
  87. return self.timer_list_index
  88. end
  89. function TimerQuest:CancelQuest(id)
  90. if id then
  91. local delete_vo = self.timer_list[id]
  92. if not delete_vo then
  93. delete_vo = self.timer_list2[id]
  94. end
  95. if not delete_vo then
  96. delete_vo = self.timer_list3[id]
  97. end
  98. if delete_vo then
  99. if Array.GetSize(self.timer_cache_queue) <= 20 then
  100. Array.PushBack(self.timer_cache_queue, delete_vo)
  101. end
  102. self.timer_list[id] = nil
  103. self.timer_list2[id] = nil
  104. self.timer_list3[id] = nil
  105. end
  106. end
  107. end
  108. function TimerQuest:HandlerTimer(vo, i, frame_count)
  109. if vo.time <= 0 and frame_count > vo.count then
  110. if vo.loop > 0 then
  111. vo.loop = vo.loop - 1
  112. if vo.smooth then
  113. vo.time = vo.duration
  114. else
  115. vo.time = vo.time + vo.duration
  116. end
  117. end
  118. if vo.loop == 0 then
  119. self:CancelQuest(i)
  120. elseif vo.loop < 0 then
  121. if vo.smooth then
  122. vo.time = vo.duration
  123. else
  124. vo.time = vo.time + vo.duration
  125. end
  126. end
  127. vo.func()
  128. end
  129. end
  130. function TimerQuest:Update()
  131. -- if G_DEBUG_STOP_ACTION then
  132. -- return
  133. -- end
  134. local delta = Time.deltaTime
  135. local frame_count = Time.frameCount
  136. self.update_count = self.update_count + 1
  137. for i,vo in pairs(self.timer_list) do
  138. vo.time = vo.time - delta
  139. self:HandlerTimer(vo, i, frame_count)
  140. end
  141. self.update_delta_time2 = self.update_delta_time2 + delta
  142. if self.update_count % 9 == 0 then
  143. for i,vo in pairs(self.timer_list2) do
  144. vo.time = vo.time - self.update_delta_time2
  145. self:HandlerTimer(vo, i, frame_count)
  146. end
  147. self.update_delta_time2 = 0
  148. end
  149. self.update_delta_time3 = self.update_delta_time3 + delta
  150. if self.update_count % 29 == 0 then
  151. for i,vo in pairs(self.timer_list3) do
  152. vo.time = vo.time - self.update_delta_time3
  153. self:HandlerTimer(vo, i, frame_count)
  154. end
  155. self.update_delta_time3 = 0
  156. end
  157. end