TimeItem.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /****************************************************************************
  2. * Copyright (c) 2017 snowcold
  3. * Copyright (c) 2017 liangxie
  4. ****************************************************************************/
  5. namespace QFramework
  6. {
  7. using System;
  8. public class TimeItem : IBinaryHeapElement, IPoolable, IPoolType
  9. {
  10. /*
  11. * tick:当前第几次
  12. */
  13. private float mDelayTime;
  14. private bool mIsEnable = true;
  15. private int mRepeatCount;
  16. private float mSortScore;
  17. private Action<int> mCallback;
  18. private int mCallbackTick;
  19. private int mHeapIndex;
  20. private bool mIsCache;
  21. public static TimeItem Allocate(Action<int> callback, float delayTime, int repeatCount = 1)
  22. {
  23. TimeItem item = SafeObjectPool<TimeItem>.Instance.Allocate();
  24. item.Set(callback, delayTime, repeatCount);
  25. return item;
  26. }
  27. public void Set(Action<int> callback, float delayTime, int repeatCount)
  28. {
  29. mCallbackTick = 0;
  30. mCallback = callback;
  31. mDelayTime = delayTime;
  32. mRepeatCount = repeatCount;
  33. }
  34. public void OnTimeTick()
  35. {
  36. if (mCallback != null)
  37. {
  38. mCallback(++mCallbackTick);
  39. }
  40. if (mRepeatCount > 0)
  41. {
  42. --mRepeatCount;
  43. }
  44. }
  45. public Action<int> callback
  46. {
  47. get { return mCallback; }
  48. }
  49. public float SortScore
  50. {
  51. get { return mSortScore; }
  52. set { mSortScore = value; }
  53. }
  54. public int HeapIndex
  55. {
  56. get { return mHeapIndex; }
  57. set { mHeapIndex = value; }
  58. }
  59. public bool isEnable
  60. {
  61. get { return mIsEnable; }
  62. }
  63. public bool IsRecycled
  64. {
  65. get
  66. {
  67. return mIsCache;
  68. }
  69. set
  70. {
  71. mIsCache = value;
  72. }
  73. }
  74. public void Cancel()
  75. {
  76. if (mIsEnable)
  77. {
  78. mIsEnable = false;
  79. mCallback = null;
  80. }
  81. }
  82. public bool NeedRepeat()
  83. {
  84. if (mRepeatCount == 0)
  85. {
  86. return false;
  87. }
  88. return true;
  89. }
  90. public float DelayTime()
  91. {
  92. return mDelayTime;
  93. }
  94. public void RebuildHeap<T>(BinaryHeap<T> heap) where T : IBinaryHeapElement
  95. {
  96. heap.RebuildAtIndex(mHeapIndex);
  97. }
  98. public void OnRecycled()
  99. {
  100. mCallbackTick = 0;
  101. mCallback = null;
  102. mIsEnable = true;
  103. mHeapIndex = 0;
  104. }
  105. public void Recycle2Cache()
  106. {
  107. //超出缓存最大值
  108. SafeObjectPool<TimeItem>.Instance.Recycle(this);
  109. }
  110. }
  111. }