|
|
@@ -5,57 +5,69 @@ using System.Text;
|
|
|
|
|
|
namespace ET
|
|
|
{
|
|
|
- public class EventSystem: Singleton<EventSystem>, ISingletonUpdate, ISingletonLateUpdate
|
|
|
+ public class TypeSystems
|
|
|
{
|
|
|
- private class OneTypeSystems
|
|
|
+ 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 = new bool[(int)InstanceQueueIndex.Max];
|
|
|
+ public readonly bool[] QueueFlag;
|
|
|
}
|
|
|
-
|
|
|
- private class TypeSystems
|
|
|
+
|
|
|
+ private readonly int count;
|
|
|
+
|
|
|
+ public TypeSystems(int count)
|
|
|
{
|
|
|
- private readonly Dictionary<Type, OneTypeSystems> typeSystemsMap = new();
|
|
|
+ this.count = count;
|
|
|
+ }
|
|
|
+
|
|
|
+ private readonly Dictionary<Type, OneTypeSystems> typeSystemsMap = new();
|
|
|
|
|
|
- public OneTypeSystems GetOrCreateOneTypeSystems(Type type)
|
|
|
+ public OneTypeSystems GetOrCreateOneTypeSystems(Type type)
|
|
|
+ {
|
|
|
+ OneTypeSystems systems = null;
|
|
|
+ this.typeSystemsMap.TryGetValue(type, out systems);
|
|
|
+ if (systems != null)
|
|
|
{
|
|
|
- OneTypeSystems systems = null;
|
|
|
- this.typeSystemsMap.TryGetValue(type, out systems);
|
|
|
- if (systems != null)
|
|
|
- {
|
|
|
- return systems;
|
|
|
- }
|
|
|
-
|
|
|
- systems = new OneTypeSystems();
|
|
|
- this.typeSystemsMap.Add(type, systems);
|
|
|
return systems;
|
|
|
}
|
|
|
|
|
|
- public OneTypeSystems GetOneTypeSystems(Type type)
|
|
|
+ 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))
|
|
|
{
|
|
|
- OneTypeSystems systems = null;
|
|
|
- this.typeSystemsMap.TryGetValue(type, out systems);
|
|
|
- return systems;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- public List<object> GetSystems(Type type, Type systemType)
|
|
|
+ if (!oneTypeSystems.Map.TryGetValue(systemType, out List<object> systems))
|
|
|
{
|
|
|
- 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;
|
|
|
+ return null;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
+ return systems;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class EventSystem: Singleton<EventSystem>, ISingletonUpdate, ISingletonLateUpdate
|
|
|
+ {
|
|
|
private class EventInfo
|
|
|
{
|
|
|
public IEvent IEvent { get; }
|
|
|
@@ -79,7 +91,7 @@ namespace ET
|
|
|
|
|
|
private TypeSystems typeSystems;
|
|
|
|
|
|
- private readonly Queue<long>[] queues = new Queue<long>[(int)InstanceQueueIndex.Max];
|
|
|
+ private readonly Queue<long>[] queues = new Queue<long>[InstanceQueueIndex.Max];
|
|
|
|
|
|
public EventSystem()
|
|
|
{
|
|
|
@@ -112,7 +124,7 @@ namespace ET
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- this.typeSystems = new TypeSystems();
|
|
|
+ this.typeSystems = new TypeSystems(InstanceQueueIndex.Max);
|
|
|
|
|
|
foreach (Type type in this.GetTypes(typeof (ObjectSystemAttribute)))
|
|
|
{
|
|
|
@@ -120,12 +132,12 @@ namespace ET
|
|
|
|
|
|
if (obj is ISystemType iSystemType)
|
|
|
{
|
|
|
- OneTypeSystems oneTypeSystems = this.typeSystems.GetOrCreateOneTypeSystems(iSystemType.Type());
|
|
|
+ TypeSystems.OneTypeSystems oneTypeSystems = this.typeSystems.GetOrCreateOneTypeSystems(iSystemType.Type());
|
|
|
oneTypeSystems.Map.Add(iSystemType.SystemType(), obj);
|
|
|
- InstanceQueueIndex index = iSystemType.GetInstanceQueueIndex();
|
|
|
+ int index = iSystemType.GetInstanceQueueIndex();
|
|
|
if (index > InstanceQueueIndex.None && index < InstanceQueueIndex.Max)
|
|
|
{
|
|
|
- oneTypeSystems.QueueFlag[(int)index] = true;
|
|
|
+ oneTypeSystems.QueueFlag[index] = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -210,11 +222,11 @@ namespace ET
|
|
|
return this.allTypes[typeName];
|
|
|
}
|
|
|
|
|
|
- public void RegisterSystem(Entity component)
|
|
|
+ public virtual void RegisterSystem(Entity component)
|
|
|
{
|
|
|
Type type = component.GetType();
|
|
|
|
|
|
- OneTypeSystems oneTypeSystems = this.typeSystems.GetOneTypeSystems(type);
|
|
|
+ TypeSystems.OneTypeSystems oneTypeSystems = this.typeSystems.GetOneTypeSystems(type);
|
|
|
if (oneTypeSystems == null)
|
|
|
{
|
|
|
return;
|
|
|
@@ -441,7 +453,7 @@ namespace ET
|
|
|
|
|
|
public void Load()
|
|
|
{
|
|
|
- Queue<long> queue = this.queues[(int)InstanceQueueIndex.Load];
|
|
|
+ Queue<long> queue = this.queues[InstanceQueueIndex.Load];
|
|
|
int count = queue.Count;
|
|
|
while (count-- > 0)
|
|
|
{
|
|
|
@@ -507,7 +519,7 @@ namespace ET
|
|
|
|
|
|
public void Update()
|
|
|
{
|
|
|
- Queue<long> queue = this.queues[(int)InstanceQueueIndex.Update];
|
|
|
+ Queue<long> queue = this.queues[InstanceQueueIndex.Update];
|
|
|
int count = queue.Count;
|
|
|
while (count-- > 0)
|
|
|
{
|
|
|
@@ -547,7 +559,7 @@ namespace ET
|
|
|
|
|
|
public void LateUpdate()
|
|
|
{
|
|
|
- Queue<long> queue = this.queues[(int)InstanceQueueIndex.LateUpdate];
|
|
|
+ Queue<long> queue = this.queues[InstanceQueueIndex.LateUpdate];
|
|
|
int count = queue.Count;
|
|
|
while (count-- > 0)
|
|
|
{
|
|
|
@@ -585,7 +597,7 @@ namespace ET
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public async ETTask PublishAsync<T>(Scene scene, T a) where T : struct
|
|
|
+ public async ETTask PublishAsync<S, T>(S scene, T a) where S: class, IScene where T : struct
|
|
|
{
|
|
|
List<EventInfo> iEvents;
|
|
|
if (!this.allEvents.TryGetValue(typeof(T), out iEvents))
|
|
|
@@ -602,7 +614,7 @@ namespace ET
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- if (!(eventInfo.IEvent is AEvent<T> aEvent))
|
|
|
+ if (!(eventInfo.IEvent is AEvent<S, T> aEvent))
|
|
|
{
|
|
|
Log.Error($"event error: {eventInfo.IEvent.GetType().Name}");
|
|
|
continue;
|
|
|
@@ -621,7 +633,7 @@ namespace ET
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void Publish<T>(Scene scene, T a) where T : struct
|
|
|
+ public void Publish<S, T>(S scene, T a) where S: class, IScene where T : struct
|
|
|
{
|
|
|
List<EventInfo> iEvents;
|
|
|
if (!this.allEvents.TryGetValue(typeof (T), out iEvents))
|
|
|
@@ -638,7 +650,7 @@ namespace ET
|
|
|
}
|
|
|
|
|
|
|
|
|
- if (!(eventInfo.IEvent is AEvent<T> aEvent))
|
|
|
+ if (!(eventInfo.IEvent is AEvent<S, T> aEvent))
|
|
|
{
|
|
|
Log.Error($"event error: {eventInfo.IEvent.GetType().Name}");
|
|
|
continue;
|