TimerComponent.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ET
  4. {
  5. public enum TimerClass
  6. {
  7. None,
  8. OnceTimer,
  9. OnceWaitTimer,
  10. RepeatedTimer,
  11. }
  12. [ObjectSystem]
  13. public class TimerActionAwakeSystem: AwakeSystem<TimerAction, TimerClass, long, int, object>
  14. {
  15. public override void Awake(TimerAction self, TimerClass timerClass, long time, int type, object obj)
  16. {
  17. self.TimerClass = timerClass;
  18. self.Object = obj;
  19. self.Time = time;
  20. self.Type = type;
  21. }
  22. }
  23. [ObjectSystem]
  24. public class TimerActionDestroySystem: DestroySystem<TimerAction>
  25. {
  26. public override void Destroy(TimerAction self)
  27. {
  28. self.Object = null;
  29. self.Time = 0;
  30. self.TimerClass = TimerClass.None;
  31. self.Type = 0;
  32. }
  33. }
  34. public class TimerAction: Entity, IAwake, IAwake<TimerClass, long, int, object>, IDestroy
  35. {
  36. public TimerClass TimerClass;
  37. public object Object;
  38. public long Time;
  39. public int Type;
  40. }
  41. [FriendOf(typeof(TimerAction))]
  42. [FriendOf(typeof(TimerComponent))]
  43. public static class TimerComponentSystem
  44. {
  45. [ObjectSystem]
  46. public class TimerComponentAwakeSystem: AwakeSystem<TimerComponent>
  47. {
  48. public override void Awake(TimerComponent self)
  49. {
  50. TimerComponent.Instance = self;
  51. }
  52. }
  53. [ObjectSystem]
  54. public class TimerComponentUpdateSystem: UpdateSystem<TimerComponent>
  55. {
  56. public override void Update(TimerComponent self)
  57. {
  58. if (self.TimeId.Count == 0)
  59. {
  60. return;
  61. }
  62. long timeNow = TimeHelper.ServerNow();
  63. if (timeNow < self.minTime)
  64. {
  65. return;
  66. }
  67. foreach (KeyValuePair<long, List<long>> kv in self.TimeId)
  68. {
  69. long k = kv.Key;
  70. if (k > timeNow)
  71. {
  72. self.minTime = k;
  73. break;
  74. }
  75. self.timeOutTime.Enqueue(k);
  76. }
  77. while (self.timeOutTime.Count > 0)
  78. {
  79. long time = self.timeOutTime.Dequeue();
  80. var list = self.TimeId[time];
  81. for (int i = 0; i < list.Count; ++i)
  82. {
  83. long timerId = list[i];
  84. self.timeOutTimerIds.Enqueue(timerId);
  85. }
  86. self.TimeId.Remove(time);
  87. }
  88. while (self.timeOutTimerIds.Count > 0)
  89. {
  90. long timerId = self.timeOutTimerIds.Dequeue();
  91. TimerAction timerAction = self.GetChild<TimerAction>(timerId);
  92. if (timerAction == null)
  93. {
  94. continue;
  95. }
  96. self.Run(timerAction);
  97. }
  98. }
  99. }
  100. [ObjectSystem]
  101. public class TimerComponentDestroySystem: DestroySystem<TimerComponent>
  102. {
  103. public override void Destroy(TimerComponent self)
  104. {
  105. TimerComponent.Instance = null;
  106. }
  107. }
  108. private static void Run(this TimerComponent self, TimerAction timerAction)
  109. {
  110. switch (timerAction.TimerClass)
  111. {
  112. case TimerClass.OnceTimer:
  113. {
  114. int type = timerAction.Type;
  115. Game.EventSystem.Callback(type, timerAction.Object);
  116. break;
  117. }
  118. case TimerClass.OnceWaitTimer:
  119. {
  120. ETTask<bool> tcs = timerAction.Object as ETTask<bool>;
  121. self.Remove(timerAction.Id);
  122. tcs.SetResult(true);
  123. break;
  124. }
  125. case TimerClass.RepeatedTimer:
  126. {
  127. int type = timerAction.Type;
  128. long tillTime = TimeHelper.ServerNow() + timerAction.Time;
  129. self.AddTimer(tillTime, timerAction);
  130. Game.EventSystem.Callback(type, timerAction.Object);
  131. break;
  132. }
  133. }
  134. }
  135. private static void AddTimer(this TimerComponent self, long tillTime, TimerAction timer)
  136. {
  137. self.TimeId.Add(tillTime, timer.Id);
  138. if (tillTime < self.minTime)
  139. {
  140. self.minTime = tillTime;
  141. }
  142. }
  143. public static bool Remove(this TimerComponent self, ref long id)
  144. {
  145. long i = id;
  146. id = 0;
  147. return self.Remove(i);
  148. }
  149. private static bool Remove(this TimerComponent self, long id)
  150. {
  151. if (id == 0)
  152. {
  153. return false;
  154. }
  155. TimerAction timerAction = self.GetChild<TimerAction>(id);
  156. if (timerAction == null)
  157. {
  158. return false;
  159. }
  160. timerAction.Dispose();
  161. return true;
  162. }
  163. public static async ETTask<bool> WaitTillAsync(this TimerComponent self, long tillTime, ETCancellationToken cancellationToken = null)
  164. {
  165. long timeNow = TimeHelper.ServerNow();
  166. if (timeNow >= tillTime)
  167. {
  168. return true;
  169. }
  170. ETTask<bool> tcs = ETTask<bool>.Create(true);
  171. TimerAction timer = self.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.OnceWaitTimer, tillTime - timeNow, 0, tcs, true);
  172. self.AddTimer(tillTime, timer);
  173. long timerId = timer.Id;
  174. void CancelAction()
  175. {
  176. if (self.Remove(timerId))
  177. {
  178. tcs.SetResult(false);
  179. }
  180. }
  181. bool ret;
  182. try
  183. {
  184. cancellationToken?.Add(CancelAction);
  185. ret = await tcs;
  186. }
  187. finally
  188. {
  189. cancellationToken?.Remove(CancelAction);
  190. }
  191. return ret;
  192. }
  193. public static async ETTask<bool> WaitFrameAsync(this TimerComponent self, ETCancellationToken cancellationToken = null)
  194. {
  195. bool ret = await self.WaitAsync(1, cancellationToken);
  196. return ret;
  197. }
  198. public static async ETTask<bool> WaitAsync(this TimerComponent self, long time, ETCancellationToken cancellationToken = null)
  199. {
  200. if (time == 0)
  201. {
  202. return true;
  203. }
  204. long tillTime = TimeHelper.ServerNow() + time;
  205. ETTask<bool> tcs = ETTask<bool>.Create(true);
  206. TimerAction timer = self.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.OnceWaitTimer, time, 0, tcs, true);
  207. self.AddTimer(tillTime, timer);
  208. long timerId = timer.Id;
  209. void CancelAction()
  210. {
  211. if (self.Remove(timerId))
  212. {
  213. tcs.SetResult(false);
  214. }
  215. }
  216. bool ret;
  217. try
  218. {
  219. cancellationToken?.Add(CancelAction);
  220. ret = await tcs;
  221. }
  222. finally
  223. {
  224. cancellationToken?.Remove(CancelAction);
  225. }
  226. return ret;
  227. }
  228. // 用这个优点是可以热更,缺点是回调式的写法,逻辑不连贯。WaitTillAsync不能热更,优点是逻辑连贯。
  229. // wait时间短并且逻辑需要连贯的建议WaitTillAsync
  230. // wait时间长不需要逻辑连贯的建议用NewOnceTimer
  231. public static long NewOnceTimer(this TimerComponent self, long tillTime, int type, object args)
  232. {
  233. if (tillTime < TimeHelper.ServerNow())
  234. {
  235. Log.Warning($"new once time too small: {tillTime}");
  236. }
  237. TimerAction timer = self.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.OnceTimer, tillTime, type, args, true);
  238. self.AddTimer(tillTime, timer);
  239. return timer.Id;
  240. }
  241. public static long NewFrameTimer(this TimerComponent self, int type, object args)
  242. {
  243. #if APPS
  244. return self.NewRepeatedTimerInner(100, type, args);
  245. #else
  246. return self.NewRepeatedTimerInner(0, type, args);
  247. #endif
  248. }
  249. /// <summary>
  250. /// 创建一个RepeatedTimer
  251. /// </summary>
  252. private static long NewRepeatedTimerInner(this TimerComponent self, long time, int type, object args)
  253. {
  254. #if APPS
  255. if (time < 100)
  256. {
  257. throw new Exception($"repeated timer < 100, timerType: time: {time}");
  258. }
  259. #endif
  260. long tillTime = TimeHelper.ServerNow() + time;
  261. TimerAction timer = self.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.RepeatedTimer, time, type, args, true);
  262. // 每帧执行的不用加到timerId中,防止遍历
  263. self.AddTimer(tillTime, timer);
  264. return timer.Id;
  265. }
  266. public static long NewRepeatedTimer(this TimerComponent self, long time, int type, object args)
  267. {
  268. if (time < 100)
  269. {
  270. Log.Error($"time too small: {time}");
  271. return 0;
  272. }
  273. return self.NewRepeatedTimerInner(time, type, args);
  274. }
  275. }
  276. [ComponentOf(typeof(Scene))]
  277. [ChildType(typeof(TimerAction))]
  278. public class TimerComponent: Entity, IAwake, IUpdate, ILoad, IDestroy
  279. {
  280. public static TimerComponent Instance
  281. {
  282. get;
  283. set;
  284. }
  285. /// <summary>
  286. /// key: time, value: timer id
  287. /// </summary>
  288. public readonly MultiMap<long, long> TimeId = new MultiMap<long, long>();
  289. public readonly Queue<long> timeOutTime = new Queue<long>();
  290. public readonly Queue<long> timeOutTimerIds = new Queue<long>();
  291. public readonly Queue<long> everyFrameTimer = new Queue<long>();
  292. // 记录最小时间,不用每次都去MultiMap取第一个值
  293. public long minTime;
  294. }
  295. }