Game.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ET
  4. {
  5. public static class Game
  6. {
  7. [StaticField]
  8. private static readonly Dictionary<Type, ISingleton> singletonTypes = new Dictionary<Type, ISingleton>();
  9. [StaticField]
  10. private static readonly Stack<ISingleton> singletons = new Stack<ISingleton>();
  11. [StaticField]
  12. private static readonly Queue<ISingleton> updates = new Queue<ISingleton>();
  13. [StaticField]
  14. private static readonly Queue<ISingleton> lateUpdates = new Queue<ISingleton>();
  15. [StaticField]
  16. private static readonly Queue<ETTask> frameFinishTask = new Queue<ETTask>();
  17. public static T AddSingleton<T>() where T: Singleton<T>, new()
  18. {
  19. T singleton = new T();
  20. AddSingleton(singleton);
  21. return singleton;
  22. }
  23. public static void AddSingleton(ISingleton singleton)
  24. {
  25. Type singletonType = singleton.GetType();
  26. if (singletonTypes.ContainsKey(singletonType))
  27. {
  28. throw new Exception($"already exist singleton: {singletonType.Name}");
  29. }
  30. singletonTypes.Add(singletonType, singleton);
  31. singletons.Push(singleton);
  32. singleton.Register();
  33. if (singleton is ISingletonAwake awake)
  34. {
  35. awake.Awake();
  36. }
  37. if (singleton is ISingletonUpdate)
  38. {
  39. updates.Enqueue(singleton);
  40. }
  41. if (singleton is ISingletonLateUpdate)
  42. {
  43. lateUpdates.Enqueue(singleton);
  44. }
  45. }
  46. public static async ETTask WaitFrameFinish()
  47. {
  48. ETTask task = ETTask.Create(true);
  49. frameFinishTask.Enqueue(task);
  50. await task;
  51. }
  52. public static void Update(int timeMS)
  53. {
  54. int count = updates.Count;
  55. while (count-- > 0)
  56. {
  57. ISingleton singleton = updates.Dequeue();
  58. if (singleton.IsDisposed())
  59. {
  60. continue;
  61. }
  62. if (singleton is not ISingletonUpdate update)
  63. {
  64. continue;
  65. }
  66. updates.Enqueue(singleton);
  67. try
  68. {
  69. update.Update(timeMS);
  70. }
  71. catch (Exception e)
  72. {
  73. Log.Error(e);
  74. }
  75. }
  76. }
  77. public static void LateUpdate()
  78. {
  79. int count = lateUpdates.Count;
  80. while (count-- > 0)
  81. {
  82. ISingleton singleton = lateUpdates.Dequeue();
  83. if (singleton.IsDisposed())
  84. {
  85. continue;
  86. }
  87. if (singleton is not ISingletonLateUpdate lateUpdate)
  88. {
  89. continue;
  90. }
  91. lateUpdates.Enqueue(singleton);
  92. try
  93. {
  94. lateUpdate.LateUpdate();
  95. }
  96. catch (Exception e)
  97. {
  98. Log.Error(e);
  99. }
  100. }
  101. }
  102. public static void FrameFinishUpdate()
  103. {
  104. while (frameFinishTask.Count > 0)
  105. {
  106. ETTask task = frameFinishTask.Dequeue();
  107. task.SetResult();
  108. }
  109. }
  110. public static void Close()
  111. {
  112. // 顺序反过来清理
  113. while (singletons.Count > 0)
  114. {
  115. ISingleton iSingleton = singletons.Pop();
  116. iSingleton.Destroy();
  117. }
  118. singletonTypes.Clear();
  119. }
  120. }
  121. }