TimerBase_Tips.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ---@alias TimerBaseData {time:number,nextTime:number,count:number,func:function,args:any,isTimerStop:boolean}
  2. ---@class TimerBase
  3. local TimerBase = {}
  4. local this = TimerBase
  5. ---@class timers
  6. local timers = {}
  7. local updating = false
  8. function TimerBase.Start(time, func, ...)
  9. return this.StartLoop(time, 1, func, ...)
  10. end
  11. function TimerBase.StartLoop(time, count, func, ...)
  12. local t =
  13. {
  14. time = time,
  15. nextTime = this.GetTime() + time,
  16. count = count,
  17. func = func,
  18. args = spack(...),
  19. }
  20. table.insert(timers, t)
  21. return t
  22. end
  23. function TimerBase.StartLoopDelay(delayTime,time, count, func, ...)
  24. local t =
  25. {
  26. time = time,
  27. nextTime =delayTime<=0 and (this.GetTime() +time) or (this.GetTime()+delayTime),
  28. count = count,
  29. func = func,
  30. args = spack(...),
  31. }
  32. table.insert(timers, t)
  33. if delayTime<=0 then
  34. t.func(sunpack(t.args))
  35. end
  36. return t
  37. end
  38. function TimerBase.StartLoopForever(time, func, ...)
  39. return this.StartLoop(time, -1, func, ...)
  40. end
  41. ---@return void @调用开始的时候调用一次func
  42. function TimerBase.StartLoopForeverBeginCallOnce(time, count,func, ...)
  43. local t =
  44. {
  45. time = time,
  46. nextTime = this.GetTime() + time,
  47. count = count,
  48. func = func,
  49. args = spack(...),
  50. }
  51. table.insert(timers, t)
  52. t.func(sunpack(t.args))
  53. return t
  54. end
  55. function TimerBase.Stop(t)
  56. if not t then
  57. return
  58. end
  59. if updating then
  60. t.count = 0
  61. else
  62. local index = array.indexOf(timers, t)
  63. if index then
  64. timers[index].isTimerStop = true
  65. table.remove(timers, index)
  66. end
  67. end
  68. end
  69. function TimerBase.Update()
  70. updating = true
  71. local now = this.GetTime()
  72. local count = #timers
  73. local i = 1
  74. while i <= count do
  75. local t = timers[i]
  76. local remove = false
  77. local needCall = false
  78. if t.count == 0 then
  79. remove = true
  80. elseif t.nextTime <= now then
  81. needCall = true
  82. if t.count == 1 then
  83. remove = true
  84. else
  85. if t.count > 1 then
  86. t.count = t.count - 1
  87. end
  88. t.nextTime = t.nextTime + t.time
  89. end
  90. end
  91. if remove then
  92. table.remove(timers, i)
  93. t.isTimerStop = true
  94. count = count - 1
  95. if needCall then
  96. t.func(sunpack(t.args))
  97. end
  98. if t.completed then
  99. t.completed()
  100. end
  101. else
  102. if needCall then
  103. t.func(sunpack(t.args))
  104. end
  105. i = i + 1
  106. end
  107. end
  108. updating = false
  109. end
  110. function TimerBase.GetTime()
  111. return 0
  112. end
  113. return TimerBase