123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System;
- using System.Collections.Generic;
- namespace ET
- {
- public static class Game
- {
- [StaticField]
- private static readonly Dictionary<Type, ISingleton> singletonTypes = new Dictionary<Type, ISingleton>();
- [StaticField]
- private static readonly Stack<ISingleton> singletons = new Stack<ISingleton>();
- [StaticField]
- private static readonly Queue<ISingleton> updates = new Queue<ISingleton>();
- [StaticField]
- private static readonly Queue<ISingleton> lateUpdates = new Queue<ISingleton>();
- [StaticField]
- private static readonly Queue<ETTask> frameFinishTask = new Queue<ETTask>();
- public static T AddSingleton<T>() where T: Singleton<T>, new()
- {
- T singleton = new T();
- AddSingleton(singleton);
- return singleton;
- }
-
- public static void AddSingleton(ISingleton singleton)
- {
- Type singletonType = singleton.GetType();
- if (singletonTypes.ContainsKey(singletonType))
- {
- throw new Exception($"already exist singleton: {singletonType.Name}");
- }
- singletonTypes.Add(singletonType, singleton);
- singletons.Push(singleton);
-
- singleton.Register();
- if (singleton is ISingletonAwake awake)
- {
- awake.Awake();
- }
-
- if (singleton is ISingletonUpdate)
- {
- updates.Enqueue(singleton);
- }
-
- if (singleton is ISingletonLateUpdate)
- {
- lateUpdates.Enqueue(singleton);
- }
- }
- public static async ETTask WaitFrameFinish()
- {
- ETTask task = ETTask.Create(true);
- frameFinishTask.Enqueue(task);
- await task;
- }
- public static void Update(int timeMS)
- {
- int count = updates.Count;
- while (count-- > 0)
- {
- ISingleton singleton = updates.Dequeue();
- if (singleton.IsDisposed())
- {
- continue;
- }
- if (singleton is not ISingletonUpdate update)
- {
- continue;
- }
-
- updates.Enqueue(singleton);
- try
- {
- update.Update(timeMS);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
-
- public static void LateUpdate()
- {
- int count = lateUpdates.Count;
- while (count-- > 0)
- {
- ISingleton singleton = lateUpdates.Dequeue();
-
- if (singleton.IsDisposed())
- {
- continue;
- }
- if (singleton is not ISingletonLateUpdate lateUpdate)
- {
- continue;
- }
-
- lateUpdates.Enqueue(singleton);
- try
- {
- lateUpdate.LateUpdate();
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public static void FrameFinishUpdate()
- {
- while (frameFinishTask.Count > 0)
- {
- ETTask task = frameFinishTask.Dequeue();
- task.SetResult();
- }
- }
- public static void Close()
- {
- // 顺序反过来清理
- while (singletons.Count > 0)
- {
- ISingleton iSingleton = singletons.Pop();
- iSingleton.Destroy();
- }
- singletonTypes.Clear();
- }
- }
- }
|