CombatTimerManager.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using Fort23.UTool;
  4. using Utility;
  5. namespace Common.Utility.CombatTimer
  6. {
  7. public class CombatTimerManager : Singleton<CombatTimerManager>
  8. {
  9. private List<CombatTimer> m_LifeList = new List<CombatTimer>();
  10. //private List<Timer> m_suspendableList = new List<Timer>();
  11. private bool m_isPause = false;
  12. public bool IsPause
  13. {
  14. get { return m_isPause; }
  15. set { m_isPause = value; }
  16. }
  17. public void InitComatTime()
  18. {
  19. m_LifeList.Clear();
  20. }
  21. public void Release()
  22. {
  23. m_LifeList.Clear();
  24. }
  25. private void PreSceneChange(params object[] obj)
  26. {
  27. m_LifeList.Clear();
  28. //m_suspendableList.Clear();
  29. }
  30. public override void Dispose()
  31. {
  32. for (int i = 0; i < m_LifeList.Count; i++)
  33. {
  34. m_LifeList[i].Dispose();
  35. }
  36. m_LifeList.Clear();
  37. base.Dispose();
  38. }
  39. public void AddTimer(CombatTimer combatTimer)
  40. {
  41. if (combatTimer != null)
  42. {
  43. //timer.d
  44. m_LifeList.Add(combatTimer);
  45. combatTimer.Begin();
  46. }
  47. }
  48. public CombatTimer AddTimer(float duration, CombatTimer.OnTimerCallBack callBack,
  49. CombatTimer.TimerType type = CombatTimer.TimerType.GameTimeUpdate, uint maxHitCount = 1)
  50. {
  51. if (duration <= 0)
  52. {
  53. callBack();
  54. return null;
  55. }
  56. CombatTimer combatTimer = new CombatTimer(duration, callBack, type, maxHitCount);
  57. combatTimer.isCombat = true;
  58. AddTimer(combatTimer);
  59. return combatTimer;
  60. }
  61. public void RemoveTimer(CombatTimer combatTimer)
  62. {
  63. if (combatTimer != null)
  64. {
  65. if (m_LifeList.Contains(combatTimer))
  66. {
  67. m_LifeList.Remove(combatTimer);
  68. }
  69. combatTimer.Dispose();
  70. combatTimer = null;
  71. }
  72. }
  73. private void Adavance(float deltaTime)
  74. {
  75. if (!IsPause)
  76. {
  77. if (m_LifeList.Count <= 0)
  78. {
  79. return;
  80. }
  81. AdvanceLifeTime(deltaTime);
  82. }
  83. }
  84. private void AdvanceLifeTime(float deltaTime)
  85. {
  86. try
  87. {
  88. float curTime = deltaTime;
  89. CombatTimer[] timers = m_LifeList.ToArray();
  90. for (int i = 0; i < timers.Length; i++)
  91. {
  92. timers[i].DeltaUpdate(curTime);
  93. }
  94. }
  95. catch (Exception e)
  96. {
  97. LogTool.Exception(e);
  98. throw;
  99. }
  100. }
  101. public void ComabtUpdate(float deltaTime)
  102. {
  103. Adavance(deltaTime);
  104. }
  105. } //TimerManager
  106. }