|
|
@@ -5,68 +5,7 @@ using System.Text;
|
|
|
|
|
|
namespace ET
|
|
|
{
|
|
|
- public class TypeSystems
|
|
|
- {
|
|
|
- public class OneTypeSystems
|
|
|
- {
|
|
|
- public OneTypeSystems(int count)
|
|
|
- {
|
|
|
- this.QueueFlag = new bool[count];
|
|
|
- }
|
|
|
-
|
|
|
- public readonly UnOrderMultiMap<Type, object> Map = new();
|
|
|
- // 这里不用hash,数量比较少,直接for循环速度更快
|
|
|
- public readonly bool[] QueueFlag;
|
|
|
- }
|
|
|
-
|
|
|
- private readonly int count;
|
|
|
-
|
|
|
- public TypeSystems(int count)
|
|
|
- {
|
|
|
- this.count = count;
|
|
|
- }
|
|
|
-
|
|
|
- private readonly Dictionary<Type, OneTypeSystems> typeSystemsMap = new();
|
|
|
-
|
|
|
- public OneTypeSystems GetOrCreateOneTypeSystems(Type type)
|
|
|
- {
|
|
|
- OneTypeSystems systems = null;
|
|
|
- this.typeSystemsMap.TryGetValue(type, out systems);
|
|
|
- if (systems != null)
|
|
|
- {
|
|
|
- return systems;
|
|
|
- }
|
|
|
-
|
|
|
- systems = new OneTypeSystems(this.count);
|
|
|
- this.typeSystemsMap.Add(type, systems);
|
|
|
- return systems;
|
|
|
- }
|
|
|
-
|
|
|
- public OneTypeSystems GetOneTypeSystems(Type type)
|
|
|
- {
|
|
|
- OneTypeSystems systems = null;
|
|
|
- this.typeSystemsMap.TryGetValue(type, out systems);
|
|
|
- return systems;
|
|
|
- }
|
|
|
-
|
|
|
- public List<object> GetSystems(Type type, Type systemType)
|
|
|
- {
|
|
|
- OneTypeSystems oneTypeSystems = null;
|
|
|
- if (!this.typeSystemsMap.TryGetValue(type, out oneTypeSystems))
|
|
|
- {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- if (!oneTypeSystems.Map.TryGetValue(systemType, out List<object> systems))
|
|
|
- {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- return systems;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public class EventSystem: Singleton<EventSystem>, ISingletonUpdate, ISingletonLateUpdate
|
|
|
+ public class EventSystem: Singleton<EventSystem>
|
|
|
{
|
|
|
private class EventInfo
|
|
|
{
|
|
|
@@ -88,19 +27,7 @@ namespace ET
|
|
|
private readonly Dictionary<Type, List<EventInfo>> allEvents = new();
|
|
|
|
|
|
private Dictionary<Type, Dictionary<int, object>> allInvokes = new();
|
|
|
-
|
|
|
- private TypeSystems typeSystems;
|
|
|
-
|
|
|
- private readonly Queue<EntityRef<Entity>>[] queues = new Queue<EntityRef<Entity>>[InstanceQueueIndex.Max];
|
|
|
-
|
|
|
- public EventSystem()
|
|
|
- {
|
|
|
- for (int i = 0; i < this.queues.Length; i++)
|
|
|
- {
|
|
|
- this.queues[i] = new Queue<EntityRef<Entity>>();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
public void Add(Dictionary<string, Type> addTypes)
|
|
|
{
|
|
|
this.allTypes.Clear();
|
|
|
@@ -124,24 +51,6 @@ namespace ET
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- this.typeSystems = new TypeSystems(InstanceQueueIndex.Max);
|
|
|
-
|
|
|
- foreach (Type type in this.GetTypes(typeof (ObjectSystemAttribute)))
|
|
|
- {
|
|
|
- object obj = Activator.CreateInstance(type);
|
|
|
-
|
|
|
- if (obj is ISystemType iSystemType)
|
|
|
- {
|
|
|
- TypeSystems.OneTypeSystems oneTypeSystems = this.typeSystems.GetOrCreateOneTypeSystems(iSystemType.Type());
|
|
|
- oneTypeSystems.Map.Add(iSystemType.SystemType(), obj);
|
|
|
- int index = iSystemType.GetInstanceQueueIndex();
|
|
|
- if (index > InstanceQueueIndex.None && index < InstanceQueueIndex.Max)
|
|
|
- {
|
|
|
- oneTypeSystems.QueueFlag[index] = true;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
this.allEvents.Clear();
|
|
|
foreach (Type type in types[typeof (EventAttribute)])
|
|
|
{
|
|
|
@@ -197,9 +106,10 @@ namespace ET
|
|
|
{
|
|
|
throw new Exception($"action type duplicate: {iInvoke.Type.Name} {invokeAttribute.Type}", e);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ Game.Load();
|
|
|
}
|
|
|
|
|
|
public HashSet<Type> GetTypes(Type systemAttributeType)
|
|
|
@@ -222,392 +132,6 @@ namespace ET
|
|
|
return this.allTypes[typeName];
|
|
|
}
|
|
|
|
|
|
- public virtual void RegisterSystem(Entity component)
|
|
|
- {
|
|
|
- Type type = component.GetType();
|
|
|
-
|
|
|
- TypeSystems.OneTypeSystems oneTypeSystems = this.typeSystems.GetOneTypeSystems(type);
|
|
|
- if (oneTypeSystems == null)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
- for (int i = 0; i < oneTypeSystems.QueueFlag.Length; ++i)
|
|
|
- {
|
|
|
- if (!oneTypeSystems.QueueFlag[i])
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
- this.queues[i].Enqueue(component);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Deserialize(Entity component)
|
|
|
- {
|
|
|
- if (component is not IDeserialize)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- List<object> iDeserializeSystems = this.typeSystems.GetSystems(component.GetType(), typeof (IDeserializeSystem));
|
|
|
- if (iDeserializeSystems == null)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (IDeserializeSystem deserializeSystem in iDeserializeSystems)
|
|
|
- {
|
|
|
- if (deserializeSystem == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- deserializeSystem.Run(component);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // GetComponentSystem
|
|
|
- public void GetComponent(Entity entity, Entity component)
|
|
|
- {
|
|
|
- List<object> iGetSystem = this.typeSystems.GetSystems(entity.GetType(), typeof (IGetComponentSystem));
|
|
|
- if (iGetSystem == null)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (IGetComponentSystem getSystem in iGetSystem)
|
|
|
- {
|
|
|
- if (getSystem == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- getSystem.Run(entity, component);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // AddComponentSystem
|
|
|
- public void AddComponent(Entity entity, Entity component)
|
|
|
- {
|
|
|
- List<object> iAddSystem = this.typeSystems.GetSystems(entity.GetType(), typeof (IAddComponentSystem));
|
|
|
- if (iAddSystem == null)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (IAddComponentSystem addComponentSystem in iAddSystem)
|
|
|
- {
|
|
|
- if (addComponentSystem == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- addComponentSystem.Run(entity, component);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Awake(Entity component)
|
|
|
- {
|
|
|
- List<object> iAwakeSystems = this.typeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem));
|
|
|
- if (iAwakeSystems == null)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
|
|
|
- {
|
|
|
- if (aAwakeSystem == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- aAwakeSystem.Run(component);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Awake<P1>(Entity component, P1 p1)
|
|
|
- {
|
|
|
- if (component is not IAwake<P1>)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- List<object> iAwakeSystems = this.typeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem<P1>));
|
|
|
- if (iAwakeSystems == null)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (IAwakeSystem<P1> aAwakeSystem in iAwakeSystems)
|
|
|
- {
|
|
|
- if (aAwakeSystem == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- aAwakeSystem.Run(component, p1);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Awake<P1, P2>(Entity component, P1 p1, P2 p2)
|
|
|
- {
|
|
|
- if (component is not IAwake<P1, P2>)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- List<object> iAwakeSystems = this.typeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem<P1, P2>));
|
|
|
- if (iAwakeSystems == null)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (IAwakeSystem<P1, P2> aAwakeSystem in iAwakeSystems)
|
|
|
- {
|
|
|
- if (aAwakeSystem == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- aAwakeSystem.Run(component, p1, p2);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Awake<P1, P2, P3>(Entity component, P1 p1, P2 p2, P3 p3)
|
|
|
- {
|
|
|
- if (component is not IAwake<P1, P2, P3>)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- List<object> iAwakeSystems = this.typeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem<P1, P2, P3>));
|
|
|
- if (iAwakeSystems == null)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (IAwakeSystem<P1, P2, P3> aAwakeSystem in iAwakeSystems)
|
|
|
- {
|
|
|
- if (aAwakeSystem == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- aAwakeSystem.Run(component, p1, p2, p3);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Load()
|
|
|
- {
|
|
|
- Queue<EntityRef<Entity>> queue = this.queues[InstanceQueueIndex.Load];
|
|
|
- int count = queue.Count;
|
|
|
- while (count-- > 0)
|
|
|
- {
|
|
|
- Entity component = queue.Dequeue();
|
|
|
- if (component == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (component.IsDisposed)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (component is not ILoad)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- List<object> iLoadSystems = this.typeSystems.GetSystems(component.GetType(), typeof (ILoadSystem));
|
|
|
- if (iLoadSystems == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- queue.Enqueue(component);
|
|
|
-
|
|
|
- foreach (ILoadSystem iLoadSystem in iLoadSystems)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- iLoadSystem.Run(component);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Destroy(Entity component)
|
|
|
- {
|
|
|
- if (component is not IDestroy)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- List<object> iDestroySystems = this.typeSystems.GetSystems(component.GetType(), typeof (IDestroySystem));
|
|
|
- if (iDestroySystems == null)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (IDestroySystem iDestroySystem in iDestroySystems)
|
|
|
- {
|
|
|
- if (iDestroySystem == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- iDestroySystem.Run(component);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Update()
|
|
|
- {
|
|
|
- Queue<EntityRef<Entity>> queue = this.queues[InstanceQueueIndex.Update];
|
|
|
- int count = queue.Count;
|
|
|
- while (count-- > 0)
|
|
|
- {
|
|
|
- Entity component = queue.Dequeue();
|
|
|
- if (component == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (component.IsDisposed)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (component is not IUpdate)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- List<object> iUpdateSystems = this.typeSystems.GetSystems(component.GetType(), typeof (IUpdateSystem));
|
|
|
- if (iUpdateSystems == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- queue.Enqueue(component);
|
|
|
-
|
|
|
- foreach (IUpdateSystem iUpdateSystem in iUpdateSystems)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- iUpdateSystem.Run(component);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void LateUpdate()
|
|
|
- {
|
|
|
- Queue<EntityRef<Entity>> queue = this.queues[InstanceQueueIndex.LateUpdate];
|
|
|
- int count = queue.Count;
|
|
|
- while (count-- > 0)
|
|
|
- {
|
|
|
- Entity component = queue.Dequeue();
|
|
|
- if (component == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (component.IsDisposed)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (component is not ILateUpdate)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- List<object> iLateUpdateSystems = this.typeSystems.GetSystems(component.GetType(), typeof (ILateUpdateSystem));
|
|
|
- if (iLateUpdateSystems == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- queue.Enqueue(component);
|
|
|
-
|
|
|
- foreach (ILateUpdateSystem iLateUpdateSystem in iLateUpdateSystems)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- iLateUpdateSystem.Run(component);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public async ETTask PublishAsync<S, T>(S scene, T a) where S: class, IScene where T : struct
|
|
|
{
|
|
|
List<EventInfo> iEvents;
|