Timer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. namespace XGame.Framework.Time
  3. {
  4. public class Timer : ITimer
  5. {
  6. public enum TimerRunState : SByte
  7. {
  8. None,
  9. Running,
  10. Paused,
  11. Stoped,
  12. Finished,
  13. Exception,
  14. }
  15. private int _delay;
  16. private int _interval;
  17. private int _timeAxis;
  18. private int _loopTimes;
  19. private Action _action;
  20. private Action<int> _actionLoop;
  21. public float TimeScale { get; set; } = 1;
  22. private int _passTimes;
  23. private TimerRunState _runState;
  24. internal Timer(int delay, int interval, int looptimes, Action action)
  25. {
  26. #if UNITY_EDITOR
  27. UnityEngine.Assertions.Assert.IsTrue(delay >= 0, "延迟不能<0");
  28. UnityEngine.Assertions.Assert.IsTrue(!(delay == 0 && looptimes == 1), "立即执行的延迟定时器次数不能小于2");
  29. UnityEngine.Assertions.Assert.IsTrue(interval > 0, "定时器间隔不能<=0");
  30. UnityEngine.Assertions.Assert.IsTrue(looptimes != 0, "定时器执行次数不能为0,如果要无限循环请设置为-1");
  31. UnityEngine.Assertions.Assert.IsNotNull(action, "定时器回调不能为null");
  32. #endif
  33. _action = action;
  34. InnerInit(delay, interval, looptimes);
  35. }
  36. internal Timer(int delay, int interval, int looptimes, Action<int> action)
  37. {
  38. #if UNITY_EDITOR
  39. UnityEngine.Assertions.Assert.IsTrue(delay >= 0, "延迟不能<0");
  40. UnityEngine.Assertions.Assert.IsTrue(!(delay == 0 && looptimes == 1), "立即执行的延迟定时器次数不能小于2");
  41. UnityEngine.Assertions.Assert.IsTrue(interval > 0, "定时器间隔不能<=0");
  42. UnityEngine.Assertions.Assert.IsTrue(looptimes != 0, "定时器执行次数不能为0,如果要无限循环请设置为-1");
  43. UnityEngine.Assertions.Assert.IsNotNull(action, "定时器回调不能为null");
  44. #endif
  45. _actionLoop = action;
  46. InnerInit(delay, interval, looptimes);
  47. }
  48. private void InnerInit(int delay, int interval, int looptimes)
  49. {
  50. _delay = delay;
  51. _interval = interval;
  52. _loopTimes = looptimes;
  53. _passTimes = 0;
  54. _runState = TimerRunState.Running;
  55. }
  56. //校准时间
  57. private float addingValue = 0;
  58. private int CorrectTime(int deltaTime)
  59. {
  60. float updateTime = (float)deltaTime * TimeScale;
  61. int elapse = (int)updateTime;
  62. float timeDecimals = updateTime - elapse;
  63. addingValue += timeDecimals;
  64. if (addingValue >= 1.0f)
  65. {
  66. elapse += 1;
  67. addingValue -= 1.0f;
  68. }
  69. return elapse;
  70. }
  71. internal bool Update(int deltaTime)
  72. {
  73. if (_runState == TimerRunState.Stoped ||
  74. _runState == TimerRunState.Finished ||
  75. _runState == TimerRunState.Exception)
  76. {
  77. return true;
  78. }
  79. if (_runState == TimerRunState.Paused)
  80. {
  81. return false;
  82. }
  83. int correctedTime = CorrectTime(deltaTime);
  84. _timeAxis += correctedTime;
  85. var interval = _passTimes == 0 ? _delay : _interval;
  86. if (_timeAxis > interval)
  87. {
  88. _timeAxis -= interval;
  89. _passTimes += 1;
  90. try
  91. {
  92. _action?.Invoke();
  93. _actionLoop?.Invoke(_passTimes);
  94. }
  95. catch (Exception e)
  96. {
  97. _runState = TimerRunState.Exception;
  98. Log.Exception($"定时器回调执行发生异常({_passTimes}/{_loopTimes})", e);
  99. //throw new Exception($"定时器回调执行发生异常({_passTimes}/{_loopTimes}):{e}");
  100. }
  101. if (_passTimes == _loopTimes)
  102. {
  103. _runState = TimerRunState.Finished;
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. public void Cancel()
  110. {
  111. _runState = TimerRunState.Stoped;
  112. _action = null;
  113. _actionLoop = null;
  114. }
  115. public void Pause()
  116. {
  117. if (_runState == TimerRunState.Running)
  118. _runState = TimerRunState.Paused;
  119. }
  120. public void Resume()
  121. {
  122. if (_runState == TimerRunState.Paused)
  123. _runState = TimerRunState.Running;
  124. }
  125. public void Dispose()
  126. {
  127. _action = null;
  128. _actionLoop = null;
  129. }
  130. }
  131. }