您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

129 行
4.1 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using SUISS.Core;
  5. using SUISS.Scheduling;
  6. using UnityEngine;
  7. namespace SUISSEngine
  8. {
  9. public abstract class InterestingEventFlagging<T> where T : struct, IConvertible
  10. {
  11. public InterestingEventFlagging(Messenger messenger, Scheduler scheduler, IRatingRequestManager ratingRequestManager)
  12. {
  13. messenger.Subscribe<InterestingRatingTriggerEvent<T>>(new Action<InterestingRatingTriggerEvent<T>>(this.HandleInterestingRatingTriggerEvent));
  14. this._scheduler = scheduler;
  15. this._timeOutInfo = new Dictionary<T, InterestingEventFlagging<T>.TimeOutInfo>();
  16. this._flags = new Dictionary<T, int>();
  17. }
  18. public InterestingEventFlagging()
  19. {
  20. }
  21. private void HandleInterestingRatingTriggerEvent(InterestingRatingTriggerEvent<T> e)
  22. {
  23. T eventType = e.EventType;
  24. this.AddFlag(eventType);
  25. this.HandleFlagAdd(eventType);
  26. if (this._timeOutInfo.ContainsKey(eventType))
  27. {
  28. this.StartTimer(eventType);
  29. }
  30. }
  31. private void AddFlag(T et)
  32. {
  33. if (this._flags.ContainsKey(et))
  34. {
  35. (this._flags)[et] = this._flags[et] + 1;
  36. }
  37. else
  38. {
  39. this._flags.Add(et, 1);
  40. }
  41. }
  42. private void StartTimer(T et)
  43. {
  44. if (!this._timeOutInfo.ContainsKey(et))
  45. {
  46. UnityEngine.Debug.LogWarning("Can not start time since there is no timeoutinfo for the " + et + "event.");
  47. return;
  48. }
  49. InterestingEventFlagging<T>.TimeOutInfo toi = this._timeOutInfo[et];
  50. this._scheduler.StartRoutine(this.TimerCoroutine(et, toi));
  51. }
  52. private IEnumerator TimerCoroutine(T et, InterestingEventFlagging<T>.TimeOutInfo toi)
  53. {
  54. yield return Timing.time + (double)toi.TimeOutDuration;
  55. if (toi.TimeOutStyle == InterestingEventFlagging<T>.TimeOutStyle.Decrement && this.FlagsRaised(et) > 0)
  56. {
  57. (this._flags)[et] = this._flags[et] - 1;
  58. }
  59. else
  60. {
  61. this._flags[et] = 0;
  62. }
  63. yield break;
  64. }
  65. protected abstract void HandleFlagAdd(T eventType);
  66. protected bool FlagRaised(T flag)
  67. {
  68. return this.FlagsRaised(flag) > 0;
  69. }
  70. protected int FlagsRaised(T flag)
  71. {
  72. return (!this._flags.ContainsKey(flag)) ? 0 : this._flags[flag];
  73. }
  74. protected void DelayedNotice(double delay)
  75. {
  76. if (this._delayedNoticeCoroutine != null)
  77. {
  78. this._scheduler.StopRoutine(this._delayedNoticeCoroutine);
  79. }
  80. this._delayedNoticeCoroutine = this.DelayedNoticeCoroutine(delay);
  81. this._scheduler.StartRoutine(this._delayedNoticeCoroutine);
  82. }
  83. private IEnumerator DelayedNoticeCoroutine(double delay)
  84. {
  85. yield return Timing.time + delay;
  86. this._delayedNoticeCoroutine = null;
  87. yield break;
  88. }
  89. private Scheduler _scheduler;
  90. protected Dictionary<T, InterestingEventFlagging<T>.TimeOutInfo> _timeOutInfo;
  91. private Dictionary<T, int> _flags;
  92. private IEnumerator _delayedNoticeCoroutine;
  93. protected enum TimeOutStyle
  94. {
  95. FullReset,
  96. Decrement
  97. }
  98. protected class TimeOutInfo
  99. {
  100. public TimeOutInfo(int timeOutDuration, InterestingEventFlagging<T>.TimeOutStyle timeOutStyle)
  101. {
  102. this.TimeOutDuration = timeOutDuration;
  103. this.TimeOutStyle = timeOutStyle;
  104. }
  105. public int TimeOutDuration { get; private set; }
  106. public InterestingEventFlagging<T>.TimeOutStyle TimeOutStyle { get; private set; }
  107. }
  108. }
  109. }