CombatTimer.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using Common.Utility.CombatTimer;
  3. using Fort23.UTool;
  4. #if !COMBAT_SERVER
  5. using UnityEngine;
  6. #endif
  7. namespace Common.Utility.CombatTimer
  8. {
  9. public class CombatTimer
  10. {
  11. public enum TimerType
  12. {
  13. GameTimeUpdate, //游戏时间更新,受制于游戏时间
  14. Invalid, //释放
  15. }
  16. public bool isCombat;
  17. public delegate void OnTimerCallBack();
  18. private float m_maxDuration;
  19. public float maxDuration
  20. {
  21. get { return m_maxDuration; }
  22. set { m_maxDuration = value; }
  23. }
  24. private int m_curHitCount;
  25. private uint m_maxHitCount;
  26. private float duration;
  27. private OnTimerCallBack m_callBack;
  28. private TimerType m_type = TimerType.GameTimeUpdate;
  29. public TimerType Type
  30. {
  31. get { return m_type; }
  32. }
  33. private int m_timerGuid;
  34. public int TimerGuid
  35. {
  36. get { return m_timerGuid; }
  37. }
  38. private bool m_isEnabled = true;
  39. public bool IsEnabled
  40. {
  41. get { return m_isEnabled; }
  42. set { m_isEnabled = value; }
  43. }
  44. public string timerName { set; get; }
  45. /// <summary>
  46. /// 当前tiemr 已运行时长
  47. /// </summary>
  48. public float Duration
  49. {
  50. get { return duration; }
  51. }
  52. /// <summary>
  53. /// 当前tiemr 剩余时长
  54. /// </summary>
  55. public float LeftTime
  56. {
  57. get { return m_maxDuration - duration; }
  58. }
  59. private float m_startTime;
  60. //public bool isStop;
  61. public CombatTimer(float duration, OnTimerCallBack callBack, TimerType type = TimerType.GameTimeUpdate,
  62. uint maxHitCount = 1)
  63. {
  64. m_maxDuration = duration;
  65. m_callBack = callBack;
  66. m_type = type;
  67. m_maxHitCount = maxHitCount;
  68. }
  69. public void Begin()
  70. {
  71. m_startTime = 0;
  72. // m_startTime = Time.unscaledTime;
  73. }
  74. public void DeltaUpdate(float dateTime) //Executed by FixedUpdate
  75. {
  76. if (!IsEnabled)
  77. {
  78. return;
  79. }
  80. duration += dateTime;
  81. if (duration >= m_maxDuration)
  82. {
  83. int maxCount = 1;
  84. if (m_maxDuration != 0)
  85. {
  86. maxCount = (int) (duration / m_maxDuration);
  87. }
  88. for (int i = 0; i < maxCount; i++)
  89. {
  90. m_curHitCount++;
  91. if (m_callBack != null)
  92. {
  93. m_callBack();
  94. }
  95. if (m_curHitCount >= m_maxHitCount)
  96. {
  97. CombatTimerManager.Instance.RemoveTimer(this);
  98. break;
  99. }
  100. else
  101. {
  102. duration = duration - m_maxDuration;
  103. }
  104. //m_callBack();
  105. }
  106. }
  107. }
  108. public void Dispose()
  109. {
  110. IsEnabled = false;
  111. m_maxDuration = 0;
  112. m_callBack = null;
  113. m_type = TimerType.Invalid;
  114. m_curHitCount = 0;
  115. m_maxHitCount = 0;
  116. }
  117. } //Timer
  118. } //Timer