diff --git a/Assets/TEngine/Runtime/ECS/ECSComponent.cs b/Assets/TEngine/Runtime/ECS/ECSComponent.cs deleted file mode 100644 index 4f522518..00000000 --- a/Assets/TEngine/Runtime/ECS/ECSComponent.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace TEngine -{ - /// - /// Ecs构架可以将此组件从Entity上移除这个组件并丢入对象池,给其他此刻需要此组件的Entity使用, - /// 因此可以节省大量的内存反复创建和释放, 这也是Ecs的特性可以大量重复使用组件 - /// - public class EcsComponent : EcsObject - { - public Entity Entity { get; set; } - } -} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/ECS/Interface.meta b/Assets/TEngine/Runtime/ECS/Interface.meta deleted file mode 100644 index 15a3d8ca..00000000 --- a/Assets/TEngine/Runtime/ECS/Interface.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: f361f36f4eb725c4391977964b94423e -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/TEngine/Runtime/ECS/Interface/Interface.cs b/Assets/TEngine/Runtime/ECS/Interface/Interface.cs deleted file mode 100644 index 981353a4..00000000 --- a/Assets/TEngine/Runtime/ECS/Interface/Interface.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace TEngine -{ - public interface ISystemType - { - Type Type(); - Type SystemType(); - } -} diff --git a/Assets/TEngine/Runtime/ECS/Interface/Interface.cs.meta b/Assets/TEngine/Runtime/ECS/Interface/Interface.cs.meta deleted file mode 100644 index 96c64601..00000000 --- a/Assets/TEngine/Runtime/ECS/Interface/Interface.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 4807792c3fd17824e9e7cc8ce8ba9e05 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/TEngine/Runtime/ECS/System/IAwakeSystem.cs b/Assets/TEngine/Runtime/ECS/System/IAwakeSystem.cs deleted file mode 100644 index 902ca7c1..00000000 --- a/Assets/TEngine/Runtime/ECS/System/IAwakeSystem.cs +++ /dev/null @@ -1,154 +0,0 @@ -using System; - -namespace TEngine -{ - public interface IAwake - { - } - - public interface IAwake - { - } - - public interface IAwake - { - } - - public interface IAwake - { - } - - public interface IAwake - { - } - - public interface IAwakeSystem : ISystemType - { - void Run(object o); - } - - public interface IAwakeSystem : ISystemType - { - void Run(object o, T t); - } - - public interface IAwakeSystem : ISystemType - { - void Run(object o, T t, U u); - } - - public interface IAwakeSystem : ISystemType - { - void Run(object o, T t, U u, V v); - } - - public interface IAwakeSystem : ISystemType - { - void Run(object o, T t, U u, V v, W w); - } - - [ObjectSystem] - public abstract class AwakeSystem : IAwakeSystem where T : IAwake - { - public Type Type() - { - return typeof(T); - } - - public Type SystemType() - { - return typeof(IAwakeSystem); - } - - public void Run(object o) - { - this.Awake((T)o); - } - - public abstract void Awake(T self); - } - - [ObjectSystem] - public abstract class AwakeSystem : IAwakeSystem where T : IAwake - { - public Type Type() - { - return typeof(T); - } - - public Type SystemType() - { - return typeof(IAwakeSystem); - } - - public void Run(object o, A a) - { - this.Awake((T)o, a); - } - - public abstract void Awake(T self, A a); - } - - [ObjectSystem] - public abstract class AwakeSystem : IAwakeSystem where T : IAwake - { - public Type Type() - { - return typeof(T); - } - - public Type SystemType() - { - return typeof(IAwakeSystem); - } - - public void Run(object o, A a, B b) - { - this.Awake((T)o, a, b); - } - - public abstract void Awake(T self, A a, B b); - } - - [ObjectSystem] - public abstract class AwakeSystem : IAwakeSystem where T : IAwake - { - public Type Type() - { - return typeof(T); - } - - public Type SystemType() - { - return typeof(IAwakeSystem); - } - - public void Run(object o, A a, B b, C c) - { - this.Awake((T)o, a, b, c); - } - - public abstract void Awake(T self, A a, B b, C c); - } - - [ObjectSystem] - public abstract class AwakeSystem : IAwakeSystem where T : IAwake - { - public Type Type() - { - return typeof(T); - } - - public Type SystemType() - { - return typeof(IAwakeSystem); - } - - public void Run(object o, A a, B b, C c, D d) - { - this.Awake((T)o, a, b, c, d); - } - - public abstract void Awake(T self, A a, B b, C c, D d); - } -} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/ECS/System/IAwakeSystem.cs.meta b/Assets/TEngine/Runtime/ECS/System/IAwakeSystem.cs.meta deleted file mode 100644 index 764e94d1..00000000 --- a/Assets/TEngine/Runtime/ECS/System/IAwakeSystem.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 8b44f8b7436d5f842b74580708345602 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/TEngine/Runtime/ECS/System/IFixedUpdateSystem.cs b/Assets/TEngine/Runtime/ECS/System/IFixedUpdateSystem.cs deleted file mode 100644 index 83de2c2b..00000000 --- a/Assets/TEngine/Runtime/ECS/System/IFixedUpdateSystem.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; - -namespace TEngine -{ - /// - /// Ecs组件物理更新接口(减少组件for循环开销) - /// - public interface IFixedUpdate - { - void FixedUpdate(); - } - - public interface IFixedUpdateSystem : ISystemType - { - void Run(object o); - } - - public abstract class FixedUpdateSystem : IFixedUpdateSystem where T : IFixedUpdate - { - public void Run(object o) - { - this.FixedUpdate((T)o); - } - - public Type Type() - { - return typeof(T); - } - - public Type SystemType() - { - return typeof(IUpdateSystem); - } - - public abstract void FixedUpdate(T self); - } -} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/ECS/System/IFixedUpdateSystem.cs.meta b/Assets/TEngine/Runtime/ECS/System/IFixedUpdateSystem.cs.meta deleted file mode 100644 index 4f545368..00000000 --- a/Assets/TEngine/Runtime/ECS/System/IFixedUpdateSystem.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: a5d5fe02a754ab04a994132d573121b2 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/TEngine/Runtime/ECS/System/ILateUpdateSystem.cs b/Assets/TEngine/Runtime/ECS/System/ILateUpdateSystem.cs deleted file mode 100644 index 21ec2131..00000000 --- a/Assets/TEngine/Runtime/ECS/System/ILateUpdateSystem.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; - -namespace TEngine -{ - /// - /// Ecs组件物理更新接口(减少组件for循环开销) - /// - public interface ILateUpdate - { - void LateUpdate(); - } - - public interface ILateUpdateSystem : ISystemType - { - void Run(object o); - } - - public abstract class LateUpdateSystem : ILateUpdateSystem where T : ILateUpdate - { - public void Run(object o) - { - this.LateUpdate((T)o); - } - - public Type Type() - { - return typeof(T); - } - - public Type SystemType() - { - return typeof(IUpdateSystem); - } - - public abstract void LateUpdate(T self); - } -} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/ECS/System/IUpdateSystem.cs b/Assets/TEngine/Runtime/ECS/System/IUpdateSystem.cs deleted file mode 100644 index dac0e9ae..00000000 --- a/Assets/TEngine/Runtime/ECS/System/IUpdateSystem.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; - -namespace TEngine -{ - /// - /// Ecs组件更新接口(减少组件for循环开销) - /// - public interface IUpdate - { - void Update(); - } - - public interface IUpdateSystem : ISystemType - { - void Run(object o); - } - - public abstract class UpdateSystem : IUpdateSystem where T : IUpdate - { - public void Run(object o) - { - this.Update((T)o); - } - - public Type Type() - { - return typeof(T); - } - - public Type SystemType() - { - return typeof(IUpdateSystem); - } - - public abstract void Update(T self); - } -} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/ECS/System/IUpdateSystem.cs.meta b/Assets/TEngine/Runtime/ECS/System/IUpdateSystem.cs.meta deleted file mode 100644 index 26606c80..00000000 --- a/Assets/TEngine/Runtime/ECS/System/IUpdateSystem.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 48e6061eb63189142bb682b49473156e -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/TEngine/Runtime/ECS.meta b/Assets/TEngine/Runtime/Entity.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS.meta rename to Assets/TEngine/Runtime/Entity.meta diff --git a/Assets/TEngine/Runtime/ECS/ArrayPool.cs b/Assets/TEngine/Runtime/Entity/ArrayPool.cs similarity index 98% rename from Assets/TEngine/Runtime/ECS/ArrayPool.cs rename to Assets/TEngine/Runtime/Entity/ArrayPool.cs index ec955c1b..aef46d93 100644 --- a/Assets/TEngine/Runtime/ECS/ArrayPool.cs +++ b/Assets/TEngine/Runtime/Entity/ArrayPool.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; -namespace TEngine +namespace TEngine.EntityModule { public interface IIndex { diff --git a/Assets/TEngine/Runtime/ECS/ArrayPool.cs.meta b/Assets/TEngine/Runtime/Entity/ArrayPool.cs.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/ArrayPool.cs.meta rename to Assets/TEngine/Runtime/Entity/ArrayPool.cs.meta diff --git a/Assets/TEngine/Runtime/ECS/Attribute.meta b/Assets/TEngine/Runtime/Entity/Attribute.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/Attribute.meta rename to Assets/TEngine/Runtime/Entity/Attribute.meta diff --git a/Assets/TEngine/Runtime/ECS/Attribute/BaseAttribute.cs b/Assets/TEngine/Runtime/Entity/Attribute/BaseAttribute.cs similarity index 100% rename from Assets/TEngine/Runtime/ECS/Attribute/BaseAttribute.cs rename to Assets/TEngine/Runtime/Entity/Attribute/BaseAttribute.cs diff --git a/Assets/TEngine/Runtime/ECS/Attribute/BaseAttribute.cs.meta b/Assets/TEngine/Runtime/Entity/Attribute/BaseAttribute.cs.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/Attribute/BaseAttribute.cs.meta rename to Assets/TEngine/Runtime/Entity/Attribute/BaseAttribute.cs.meta diff --git a/Assets/TEngine/Runtime/ECS/Attribute/ObjectSystemAttribute.cs b/Assets/TEngine/Runtime/Entity/Attribute/EntitySystemAttribute.cs similarity index 68% rename from Assets/TEngine/Runtime/ECS/Attribute/ObjectSystemAttribute.cs rename to Assets/TEngine/Runtime/Entity/Attribute/EntitySystemAttribute.cs index de280567..1cb9c98d 100644 --- a/Assets/TEngine/Runtime/ECS/Attribute/ObjectSystemAttribute.cs +++ b/Assets/TEngine/Runtime/Entity/Attribute/EntitySystemAttribute.cs @@ -3,7 +3,7 @@ namespace TEngine { [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] - public class ObjectSystemAttribute : BaseAttribute + public class EntitySystemAttribute : BaseAttribute { } } \ No newline at end of file diff --git a/Assets/TEngine/Runtime/ECS/ECSComponent.cs.meta b/Assets/TEngine/Runtime/Entity/Attribute/EntitySystemAttribute.cs.meta similarity index 83% rename from Assets/TEngine/Runtime/ECS/ECSComponent.cs.meta rename to Assets/TEngine/Runtime/Entity/Attribute/EntitySystemAttribute.cs.meta index c23d0901..d93d80fe 100644 --- a/Assets/TEngine/Runtime/ECS/ECSComponent.cs.meta +++ b/Assets/TEngine/Runtime/Entity/Attribute/EntitySystemAttribute.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: e93d874e10ffedf488c413fd5bbc012f +guid: 418bff9105ad99345837c9356c38d68b MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/TEngine/Runtime/ECS/Demo.meta b/Assets/TEngine/Runtime/Entity/Demo.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/Demo.meta rename to Assets/TEngine/Runtime/Entity/Demo.meta diff --git a/Assets/TEngine/Runtime/ECS/Demo/ECSDemoApp.cs b/Assets/TEngine/Runtime/Entity/Demo/ECSDemoApp.cs similarity index 64% rename from Assets/TEngine/Runtime/ECS/Demo/ECSDemoApp.cs rename to Assets/TEngine/Runtime/Entity/Demo/ECSDemoApp.cs index cf28533c..d2cde783 100644 --- a/Assets/TEngine/Runtime/ECS/Demo/ECSDemoApp.cs +++ b/Assets/TEngine/Runtime/Entity/Demo/ECSDemoApp.cs @@ -8,26 +8,26 @@ public class EcsDemoApp : MonoBehaviour void Start() { var entity = Entity.Create(); - EcsGameObject actor = entity.AddComponent(); - actor.Name = typeof(EcsGameObject).ToString(); + GameObjectCmpt actor = entity.AddComponent(); + actor.Name = typeof(GameObjectCmpt).ToString(); actor.gameObject = Instantiate(@object); actor.transform = actor.gameObject.GetComponent(); - entity.AddComponent(); + entity.AddComponent(); entity.CheckDebugInfo(actor.gameObject); Debug.Log(entity.ToString()); var entity2 = Entity.Create(); - EcsGameObject actor2 = entity2.AddComponent(); - actor2.Name = typeof(EcsGameObject).ToString(); + GameObjectCmpt actor2 = entity2.AddComponent(); + actor2.Name = typeof(GameObjectCmpt).ToString(); actor2.gameObject = Instantiate(@object); actor2.transform = actor2.gameObject.GetComponent(); - entity2.AddComponent(); + entity2.AddComponent(); entity2.CheckDebugInfo(actor2.gameObject); Debug.Log(entity2.ToString()); } void Update() { - EcsSystem.Instance.Update(); + EntitySystem.Instance.Update(); } } diff --git a/Assets/TEngine/Runtime/ECS/Demo/ECSDemoApp.cs.meta b/Assets/TEngine/Runtime/Entity/Demo/ECSDemoApp.cs.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/Demo/ECSDemoApp.cs.meta rename to Assets/TEngine/Runtime/Entity/Demo/ECSDemoApp.cs.meta diff --git a/Assets/TEngine/Runtime/ECS/Demo/EcsDemo.unity b/Assets/TEngine/Runtime/Entity/Demo/EcsDemo.unity similarity index 100% rename from Assets/TEngine/Runtime/ECS/Demo/EcsDemo.unity rename to Assets/TEngine/Runtime/Entity/Demo/EcsDemo.unity diff --git a/Assets/TEngine/Runtime/ECS/Demo/EcsDemo.unity.meta b/Assets/TEngine/Runtime/Entity/Demo/EcsDemo.unity.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/Demo/EcsDemo.unity.meta rename to Assets/TEngine/Runtime/Entity/Demo/EcsDemo.unity.meta diff --git a/Assets/TEngine/Runtime/ECS/Demo/EcsUnit.prefab b/Assets/TEngine/Runtime/Entity/Demo/EcsUnit.prefab similarity index 100% rename from Assets/TEngine/Runtime/ECS/Demo/EcsUnit.prefab rename to Assets/TEngine/Runtime/Entity/Demo/EcsUnit.prefab diff --git a/Assets/TEngine/Runtime/ECS/Demo/EcsUnit.prefab.meta b/Assets/TEngine/Runtime/Entity/Demo/EcsUnit.prefab.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/Demo/EcsUnit.prefab.meta rename to Assets/TEngine/Runtime/Entity/Demo/EcsUnit.prefab.meta diff --git a/Assets/TEngine/Runtime/ECS/ECSDebugBehaviour.cs b/Assets/TEngine/Runtime/Entity/EcsDebugBehaviour.cs similarity index 98% rename from Assets/TEngine/Runtime/ECS/ECSDebugBehaviour.cs rename to Assets/TEngine/Runtime/Entity/EcsDebugBehaviour.cs index e07285c2..54cdbbb4 100644 --- a/Assets/TEngine/Runtime/ECS/ECSDebugBehaviour.cs +++ b/Assets/TEngine/Runtime/Entity/EcsDebugBehaviour.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace TEngine +namespace TEngine.EntityModule { public enum EcsDebugType { diff --git a/Assets/TEngine/Runtime/ECS/ECSDebugBehaviour.cs.meta b/Assets/TEngine/Runtime/Entity/EcsDebugBehaviour.cs.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/ECSDebugBehaviour.cs.meta rename to Assets/TEngine/Runtime/Entity/EcsDebugBehaviour.cs.meta diff --git a/Assets/TEngine/Runtime/ECS/ECSObject.cs b/Assets/TEngine/Runtime/Entity/EcsObject.cs similarity index 81% rename from Assets/TEngine/Runtime/ECS/ECSObject.cs rename to Assets/TEngine/Runtime/Entity/EcsObject.cs index 7460a8ce..89a68ee1 100644 --- a/Assets/TEngine/Runtime/ECS/ECSObject.cs +++ b/Assets/TEngine/Runtime/Entity/EcsObject.cs @@ -2,10 +2,10 @@ using System.Collections.Generic; using System.Runtime.Serialization; -namespace TEngine +namespace TEngine.EntityModule { /// - /// Ecs架构基类Object + /// Entity架构基类Object /// public class EcsObject { @@ -21,7 +21,7 @@ namespace TEngine private set; } - internal EcsSystem System; + internal EntitySystem System; [IgnoreDataMember] @@ -30,15 +30,15 @@ namespace TEngine public EcsObject() { HashCode = GetType().GetHashCode(); - InstanceId = EcsSystem.Instance.CurInstanceId; - EcsSystem.Instance.CurInstanceId++; + InstanceId = EntitySystem.Instance.CurInstanceId; + EntitySystem.Instance.CurInstanceId++; } public virtual void Dispose() { if (InstanceId != 0) { - EcsSystem.Instance.EcsObjects.Remove(InstanceId); + EntitySystem.Instance.EcsObjects.Remove(InstanceId); } else { @@ -58,19 +58,18 @@ namespace TEngine /// 此对象是否可以复用,复用会将对象丢入System对象池中 等待再次使用,如果是Entity对象,并且不复用的话,则把Entity所使用的组件也不复用 public static void Destroy(EcsObject ecsObject, bool reuse = true) { - if (ecsObject is EcsComponent ecsComponent) + if (ecsObject is EntityComponent entityComponent) { - ecsComponent.Entity.Components.Remove(ecsComponent); - if (ecsComponent is IUpdate update) + entityComponent.Entity.Components.Remove(entityComponent); + if (entityComponent is IUpdate update) { - ecsComponent.Entity.Updates.Remove(update); + entityComponent.Entity.Updates.Remove(update); } if (reuse) { - ecsComponent.Entity.System.Push(ecsComponent); + entityComponent.Entity.System.Push(entityComponent); } ecsObject.OnDestroy(); - return; } else if (ecsObject is Entity entity) { @@ -78,12 +77,12 @@ namespace TEngine entity.OnDestroy(); while (entity.Components.Count > 0) { - EcsComponent ecsComponentTemp = entity.Components[0]; + EntityComponent entityComponentTemp = entity.Components[0]; entity.Components.RemoveAt(0); - ecsComponentTemp.OnDestroy(); + entityComponentTemp.OnDestroy(); if (reuse) { - entity.System.Push(ecsComponentTemp); + entity.System.Push(entityComponentTemp); } } entity.Updates.Clear(); @@ -93,6 +92,7 @@ namespace TEngine entity.System.Push(entity); } } + ecsObject.Dispose(); } public T FindObjectOfType() where T : EcsObject diff --git a/Assets/TEngine/Runtime/ECS/ECSObject.cs.meta b/Assets/TEngine/Runtime/Entity/EcsObject.cs.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/ECSObject.cs.meta rename to Assets/TEngine/Runtime/Entity/EcsObject.cs.meta diff --git a/Assets/TEngine/Runtime/ECS/Entity.cs b/Assets/TEngine/Runtime/Entity/Entity.cs similarity index 83% rename from Assets/TEngine/Runtime/ECS/Entity.cs rename to Assets/TEngine/Runtime/Entity/Entity.cs index eac8e801..454727c5 100644 --- a/Assets/TEngine/Runtime/ECS/Entity.cs +++ b/Assets/TEngine/Runtime/Entity/Entity.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Runtime.Serialization; using UnityEngine; -namespace TEngine +namespace TEngine.EntityModule { [Flags] public enum EntityStatus : byte @@ -39,21 +39,26 @@ namespace TEngine } } + [IgnoreDataMember] + private bool IsDispose => (this.status & EntityStatus.IsDispose) == EntityStatus.IsDispose; + + #endregion [SerializeField] - internal List Components = new List(); + internal List Components = new List(); internal List Updates = new List(); internal List FixedUpdates = new List(); + internal List LateUpdates = new List(); internal bool InActive; internal bool CanUpdate; internal bool CanFixedUpdate; + internal bool CanLateUpdate; public int Index { get; set; } = -1; - public EcsEventCmpt Event { get; set; } public Entity() { InActive = true; - System = EcsSystem.Instance; + System = EntitySystem.Instance; } ~Entity() @@ -61,7 +66,7 @@ namespace TEngine InActive = false; } - internal void Execute() + internal void Update() { for (int i = 0; i < Updates.Count; i++) { @@ -77,6 +82,14 @@ namespace TEngine } } + internal void LateUpdate() + { + for (int i = 0; i < LateUpdates.Count; i++) + { + LateUpdates[i].LateUpdate(); + } + } + public override string ToString() { string str = "["; @@ -132,7 +145,7 @@ namespace TEngine #region Static public static T Create() where T : Entity, new() { - var entity = EcsSystem.Instance.Create(); + var entity = EntitySystem.Instance.Create(); return entity; } #endregion diff --git a/Assets/TEngine/Runtime/ECS/Entity.cs.meta b/Assets/TEngine/Runtime/Entity/Entity.cs.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/Entity.cs.meta rename to Assets/TEngine/Runtime/Entity/Entity.cs.meta diff --git a/Assets/TEngine/Runtime/Entity/EntityComponent.cs b/Assets/TEngine/Runtime/Entity/EntityComponent.cs new file mode 100644 index 00000000..40ac914c --- /dev/null +++ b/Assets/TEngine/Runtime/Entity/EntityComponent.cs @@ -0,0 +1,19 @@ +namespace TEngine.EntityModule +{ + /// + /// Entity构架可以将此组件从Entity上移除这个组件并丢入对象池,给其他此刻需要此组件的Entity使用, + /// 因此可以节省大量的内存反复创建和释放, 这也是Entity的特性可以大量重复使用组件 + /// + public class EntityComponent : EcsObject + { + public Entity Entity { get; set; } + + #region Static + public static T Create() where T : EntityComponent, new() + { + var entity = EntitySystem.Instance.CreateComponent(); + return entity; + } + #endregion + } +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/ECS/Attribute/ObjectSystemAttribute.cs.meta b/Assets/TEngine/Runtime/Entity/EntityComponent.cs.meta similarity index 83% rename from Assets/TEngine/Runtime/ECS/Attribute/ObjectSystemAttribute.cs.meta rename to Assets/TEngine/Runtime/Entity/EntityComponent.cs.meta index 5b1543ec..ffe12588 100644 --- a/Assets/TEngine/Runtime/ECS/Attribute/ObjectSystemAttribute.cs.meta +++ b/Assets/TEngine/Runtime/Entity/EntityComponent.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: fc4379fdd792a8f40a239d9222598c93 +guid: 6f60a73d6fbcb0a4ab868419f1a7706e MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/TEngine/Runtime/ECS/EntityExt.cs b/Assets/TEngine/Runtime/Entity/EntityExt.cs similarity index 53% rename from Assets/TEngine/Runtime/ECS/EntityExt.cs rename to Assets/TEngine/Runtime/Entity/EntityExt.cs index 51fe5c1c..df84b45d 100644 --- a/Assets/TEngine/Runtime/ECS/EntityExt.cs +++ b/Assets/TEngine/Runtime/Entity/EntityExt.cs @@ -1,76 +1,31 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace TEngine +namespace TEngine.EntityModule { partial class Entity { - public void RmvComponent() where T : EcsComponent, new() + public void RmvComponent(T component) where T :EntityComponent, new() { - for (int i = 0; i < Components.Count; i++) + if (component is IUpdate update) { - if (Components[i] is T component) - { - if (component is IUpdate update) - { - Updates.Remove(update); - } - else if (component is IFixedUpdate fixedUpdate) - { - FixedUpdates.Remove(fixedUpdate); - } - System.Push(component); - - CanUpdate = Updates.Count > 0; - - CanFixedUpdate = FixedUpdates.Count > 0; - } + Updates.Remove(update); + } + else if (component is IFixedUpdate fixedUpdate) + { + FixedUpdates.Remove(fixedUpdate); } -#if UNITY_EDITOR - CheckDebugInfo(); -#endif + CanUpdate = Updates.Count > 0; + + CanFixedUpdate = FixedUpdates.Count > 0; + + Destroy(component); } - public void RmvComponent(Type componentType) + public T AddComponent() where T :EntityComponent, new() { - for (int i = 0; i < Components.Count; i++) - { - if (Components[i].GetType() == componentType) - { - - if (componentType is IUpdate update) - { - Updates.Remove(update); - - CanUpdate = Updates.Count > 0; - } - else if (componentType is IFixedUpdate fixedUpdate) - { - FixedUpdates.Remove(fixedUpdate); - - CanFixedUpdate = FixedUpdates.Count > 0; - } - //if (componentType is EcsComponent component) - //{ - // System.Push(component); - //} - } - } -#if UNITY_EDITOR - CheckDebugInfo(); -#endif - } - - public T AddComponent() where T : EcsComponent, new() - { -#if UNITY_EDITOR - CheckDebugInfo(); -#endif - T component = System.Get(); + T component = EntityComponent.Create(); component.Entity = this; component.System = System; Components.Add(component); @@ -88,11 +43,8 @@ namespace TEngine return component; } - public EcsComponent AddComponent(EcsComponent component) + public EntityComponent AddComponent(EntityComponent component) { -#if UNITY_EDITOR - CheckDebugInfo(); -#endif component.Entity = this; component.System = System; Components.Add(component); @@ -110,7 +62,7 @@ namespace TEngine return component; } - public T GetComponent() where T : EcsComponent + public T GetComponent() where T :EntityComponent { for (int i = 0; i < Components.Count; i++) { @@ -123,7 +75,7 @@ namespace TEngine return null; } - public EcsComponent GetComponent(Type componentType) + public EntityComponent GetComponent(Type componentType) { for (int i = 0; i < Components.Count; i++) { @@ -136,7 +88,7 @@ namespace TEngine return null; } - public T[] GetComponents() where T : EcsComponent + public T[] GetComponents() where T :EntityComponent { List elements = new List(); for (int i = 0; i < Components.Count; i++) @@ -149,7 +101,7 @@ namespace TEngine return elements.ToArray(); } - public List GetComponentsList() where T : EcsComponent + public List GetComponentsList() where T :EntityComponent { List elements = new List(); for (int i = 0; i < Components.Count; i++) @@ -162,9 +114,9 @@ namespace TEngine return elements; } - public EcsComponent[] GetComponents(Type comType) + public EntityComponent[] GetComponents(Type comType) { - List elements = new List(); + List elements = new List(); for (int i = 0; i < Components.Count; i++) { { @@ -176,6 +128,5 @@ namespace TEngine } return elements.ToArray(); } - } -} +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/ECS/EntityExt.cs.meta b/Assets/TEngine/Runtime/Entity/EntityExt.cs.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/EntityExt.cs.meta rename to Assets/TEngine/Runtime/Entity/EntityExt.cs.meta diff --git a/Assets/TEngine/Runtime/ECS/ECSSystem.cs b/Assets/TEngine/Runtime/Entity/EntitySystem.cs similarity index 84% rename from Assets/TEngine/Runtime/ECS/ECSSystem.cs rename to Assets/TEngine/Runtime/Entity/EntitySystem.cs index c9940d8d..789496b2 100644 --- a/Assets/TEngine/Runtime/ECS/ECSSystem.cs +++ b/Assets/TEngine/Runtime/Entity/EntitySystem.cs @@ -1,31 +1,30 @@ using System; using System.Collections.Generic; -using System.Reflection; using System.Threading.Tasks; -namespace TEngine +namespace TEngine.EntityModule { /// - /// Ecs系统 + /// Entity系统 /// [Serializable] - public class EcsSystem : IDisposable + public class EntitySystem : IDisposable { - private static EcsSystem _instance; + private static EntitySystem _instance; - public static EcsSystem Instance + public static EntitySystem Instance { get { if (_instance == null) { - _instance = new EcsSystem(); + _instance = new EntitySystem(); } return _instance; } } - private EcsSystem() + private EntitySystem() { } /// @@ -34,22 +33,22 @@ namespace TEngine internal readonly Dictionary> ObjectPool = new Dictionary>(); internal readonly ArrayPool Entities = new ArrayPool(); internal readonly Dictionary EcsObjects = new Dictionary(); - internal readonly Dictionary UpdateSystems = new Dictionary(); internal int CurInstanceId = 1000; - public void AddEntity(Entity entity) + #region internal + internal void AddEntity(Entity entity) { entity.System = this; entity.Awake(); Entities.Add(entity); } - public void RemoveEntity(Entity entity) + internal void RemoveEntity(Entity entity) { Entities.Remove(entity); } - public T Get() where T : EcsObject, new() + private T Get() where T : EcsObject, new() { int type = typeof(T).GetHashCode(); if (ObjectPool.TryGetValue(type, out Stack stack)) @@ -62,15 +61,14 @@ namespace TEngine } stack = new Stack(); ObjectPool.Add(type, stack); - Instantiate: T ecsObject = new T(); - EcsObjects.Add(ecsObject.InstanceId,ecsObject); + Instantiate: T ecsObject = new T(); + EcsObjects.Add(ecsObject.InstanceId, ecsObject); return ecsObject; } - public void Push(EcsObject ecsObject) + internal void Push(EcsObject ecsObject) { int type = ecsObject.HashCode; - ecsObject.Dispose(); if (ObjectPool.TryGetValue(type, out Stack stack)) { stack.Push(ecsObject); @@ -80,22 +78,22 @@ namespace TEngine ObjectPool.Add(type, stack); stack.Push(ecsObject); } - - public T Create() where T : Entity, new() + internal T Create() where T : Entity, new() { T entity = Get(); AddEntity(entity); return entity; } - - public T Create(T entity) where T : Entity, new() + internal T CreateComponent() where T : EntityComponent, new() { - AddEntity(entity); - return entity; + T component = Get(); + return component; } + #endregion + #region Update /// - /// 更新Ecs系统 + /// 更新Entity系统 /// /// 线程池是否并行 public void Update(bool worker = false) @@ -114,7 +112,7 @@ namespace TEngine { continue; } - Entities[i].Execute(); + Entities[i].Update(); } } else @@ -130,13 +128,13 @@ namespace TEngine { return; } - Entities[i].Execute(); + Entities[i].Update(); }); } } /// - /// 更新Ecs物理系统 + /// 更新Entity物理系统 /// /// 线程池是否并行 public void FixedUpdate(bool worker = false) @@ -175,6 +173,7 @@ namespace TEngine }); } } + #endregion #region Dispose public void Dispose() diff --git a/Assets/TEngine/Runtime/ECS/ECSSystem.cs.meta b/Assets/TEngine/Runtime/Entity/EntitySystem.cs.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/ECSSystem.cs.meta rename to Assets/TEngine/Runtime/Entity/EntitySystem.cs.meta diff --git a/Assets/TEngine/Runtime/ECS/ECSEventCmpt.cs b/Assets/TEngine/Runtime/Entity/EventCmpt.cs similarity index 96% rename from Assets/TEngine/Runtime/ECS/ECSEventCmpt.cs rename to Assets/TEngine/Runtime/Entity/EventCmpt.cs index 4cc96678..e5002e93 100644 --- a/Assets/TEngine/Runtime/ECS/ECSEventCmpt.cs +++ b/Assets/TEngine/Runtime/Entity/EventCmpt.cs @@ -1,8 +1,8 @@ using System; -namespace TEngine +namespace TEngine.EntityModule { - public class EcsEventCmpt : EcsComponent + public class EventCmpt :EntityComponent { private GameEvent _gameEvent; @@ -88,7 +88,6 @@ namespace TEngine public override void Awake() { _gameEvent = GameMemPool.Alloc(); - Entity.Event = this; } #endregion } diff --git a/Assets/TEngine/Runtime/ECS/ECSEventCmpt.cs.meta b/Assets/TEngine/Runtime/Entity/EventCmpt.cs.meta similarity index 83% rename from Assets/TEngine/Runtime/ECS/ECSEventCmpt.cs.meta rename to Assets/TEngine/Runtime/Entity/EventCmpt.cs.meta index ad55fd08..ed947803 100644 --- a/Assets/TEngine/Runtime/ECS/ECSEventCmpt.cs.meta +++ b/Assets/TEngine/Runtime/Entity/EventCmpt.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: af343da6b3090a24b92fc12aaa638411 +guid: 7e37fd19b27821c49b3bab2d92b2fe37 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/TEngine/Runtime/ECS/EcsGameObject.cs b/Assets/TEngine/Runtime/Entity/GameObjectCmpt.cs similarity index 63% rename from Assets/TEngine/Runtime/ECS/EcsGameObject.cs rename to Assets/TEngine/Runtime/Entity/GameObjectCmpt.cs index c76efd0b..648d6e90 100644 --- a/Assets/TEngine/Runtime/ECS/EcsGameObject.cs +++ b/Assets/TEngine/Runtime/Entity/GameObjectCmpt.cs @@ -1,9 +1,9 @@ -namespace TEngine +namespace TEngine.EntityModule { /// - /// Ecs Actor + /// Entity Actor /// - public class EcsGameObject : EcsComponent + public class GameObjectCmpt :EntityComponent { public string Name; public UnityEngine.GameObject gameObject; diff --git a/Assets/TEngine/Runtime/ECS/EcsGameObject.cs.meta b/Assets/TEngine/Runtime/Entity/GameObjectCmpt.cs.meta similarity index 83% rename from Assets/TEngine/Runtime/ECS/EcsGameObject.cs.meta rename to Assets/TEngine/Runtime/Entity/GameObjectCmpt.cs.meta index 5189b3eb..583093ac 100644 --- a/Assets/TEngine/Runtime/ECS/EcsGameObject.cs.meta +++ b/Assets/TEngine/Runtime/Entity/GameObjectCmpt.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 2e71316dca3cb664d964935745d3b851 +guid: 559f282b0e220ab4e8ccb2342e13aaf2 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/TEngine/Runtime/ECS/System.meta b/Assets/TEngine/Runtime/Entity/Interface.meta similarity index 100% rename from Assets/TEngine/Runtime/ECS/System.meta rename to Assets/TEngine/Runtime/Entity/Interface.meta diff --git a/Assets/TEngine/Runtime/Entity/Interface/IFixedUpdate.cs b/Assets/TEngine/Runtime/Entity/Interface/IFixedUpdate.cs new file mode 100644 index 00000000..5d3706d8 --- /dev/null +++ b/Assets/TEngine/Runtime/Entity/Interface/IFixedUpdate.cs @@ -0,0 +1,10 @@ +namespace TEngine.EntityModule +{ + /// + /// Entity组件物理更新接口(减少组件for循环开销) + /// + public interface IFixedUpdate + { + void FixedUpdate(); + } +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/Entity/Interface/IFixedUpdate.cs.meta b/Assets/TEngine/Runtime/Entity/Interface/IFixedUpdate.cs.meta new file mode 100644 index 00000000..6420c581 --- /dev/null +++ b/Assets/TEngine/Runtime/Entity/Interface/IFixedUpdate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8fd15ec3752def44099dd23f93d18d24 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Entity/Interface/ILateUpdate.cs b/Assets/TEngine/Runtime/Entity/Interface/ILateUpdate.cs new file mode 100644 index 00000000..bd4245eb --- /dev/null +++ b/Assets/TEngine/Runtime/Entity/Interface/ILateUpdate.cs @@ -0,0 +1,10 @@ +namespace TEngine.EntityModule +{ + /// + /// Entity组件物理更新接口(减少组件for循环开销) + /// + public interface ILateUpdate + { + void LateUpdate(); + } +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/Entity/Interface/ILateUpdate.cs.meta b/Assets/TEngine/Runtime/Entity/Interface/ILateUpdate.cs.meta new file mode 100644 index 00000000..08131ac5 --- /dev/null +++ b/Assets/TEngine/Runtime/Entity/Interface/ILateUpdate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5a7758a3d6376b64e96df22d14c947aa +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Entity/Interface/IUpdate.cs b/Assets/TEngine/Runtime/Entity/Interface/IUpdate.cs new file mode 100644 index 00000000..4fa24c06 --- /dev/null +++ b/Assets/TEngine/Runtime/Entity/Interface/IUpdate.cs @@ -0,0 +1,10 @@ +namespace TEngine.EntityModule +{ + /// + /// Entity组件更新接口(减少组件for循环开销) + /// + public interface IUpdate + { + void Update(); + } +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/Entity/Interface/IUpdate.cs.meta b/Assets/TEngine/Runtime/Entity/Interface/IUpdate.cs.meta new file mode 100644 index 00000000..1c08fb72 --- /dev/null +++ b/Assets/TEngine/Runtime/Entity/Interface/IUpdate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c9a60cb450a1e3a48945932e5224ea81 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: