| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730 | 
							- using System;
 
- using System.Collections.Generic;
 
- using Fort23.UTool;
 
- using UnityEngine;
 
- using UnityEngine.Pool;
 
- namespace Fort23.Core
 
- {
 
-     public class Scene : Entity
 
-     {
 
-         public new Entity Domain
 
-         {
 
-             get => this.domain;
 
-             set => this.domain = value;
 
-         }
 
-         public Scene()
 
-         {
 
-             this.IsRegister = true;
 
-             this.Domain = this;
 
-         }
 
-     }
 
-     public partial class Entity : CObject
 
-     {
 
-         private static readonly Pool<Dictionary<Type, Entity>> _dictPool = new Pool<Dictionary<Type, Entity>>();
 
-         private static readonly Pool<Dictionary<long, Entity>> _childrenPool = new Pool<Dictionary<long, Entity>>();
 
-         public long InstanceID { get; protected set; }
 
-         public bool IsDisposed => this.InstanceID == 0;
 
-         public long ID { get; set; }
 
-         private bool _isFromPool { get; set; }
 
-         private bool _isComponent { get; set; }
 
-         private bool _isRegister;
 
-         protected bool IsRegister
 
-         {
 
-             get => _isRegister;
 
-             set
 
-             {
 
-                 _isRegister = value;
 
-                 EventSystem.Instance.RegisterSystem(this, value);
 
-             }
 
-         }
 
-         protected static Entity Create(Type type, bool isFromPool)
 
-         {
 
-             Entity component;
 
-             if (isFromPool)
 
-             {
 
-                 component = (Entity) CObjectPool.Instance.Fetch(type);
 
-             }
 
-             else
 
-             {
 
-                 component = (Entity) Activator.CreateInstance(type);
 
-             }
 
-             return component;
 
-         }
 
-         public override void Dispose()
 
-         {
 
-             if (this.IsDisposed)
 
-             {
 
-                 return;
 
-             }
 
-             // 清理Component
 
-             if (this._components != null)
 
-             {
 
-                 List<Type> keys = new List<Type>();
 
-                 foreach (var componentsKey in _components.Keys)
 
-                 {
 
-                     keys.Add(componentsKey);
 
-                 }
 
-                 for (var i = 0; i < keys.Count; i++)
 
-                 {
 
-                     _components[keys[i]].Dispose();
 
-                 }
 
-                 // foreach (KeyValuePair<Type, Entity> kv in this._components)
 
-                 // {
 
-                 //     kv.Value.Dispose();
 
-                 // }
 
-                 this._components.Clear();
 
-                 _dictPool.Recycle(this._components);
 
-                 this._components = null;
 
-             }
 
-             // 清理Children
 
-             if (this._children != null)
 
-             {
 
-                 List<long> keys = new List<long>();
 
-                 foreach (var childrenKey in _children.Keys)
 
-                 {
 
-                     keys.Add(childrenKey);
 
-                 }
 
-                 for (var i = 0; i < keys.Count; i++)
 
-                 {
 
-                     _children[keys[i]].Dispose();
 
-                 }
 
-                 // foreach (Entity child in this._children.Values)
 
-                 // {
 
-                 //     child.Dispose();
 
-                 // }
 
-                 if (_children != null)
 
-                 {
 
-                     this._children.Clear();
 
-                     _childrenPool.Recycle(this._children);
 
-                     this._children = null;
 
-                 }
 
-             }
 
-             // 触发Destroy事件
 
-             EventSystem.Instance.Destroy(this);
 
-             this.domain = null;
 
-             if (this.parent != null && !this.parent.IsDisposed)
 
-             {
 
-                 if (this._isComponent)
 
-                 {
 
-                     this.parent.RemoveComponent(this);
 
-                 }
 
-                 else
 
-                 {
 
-                     this.parent.RemoveChild(this);
 
-                 }
 
-             }
 
-             this.parent = null;
 
-             if (this._isFromPool)
 
-             {
 
-                 CObjectPool.Instance.Recycle(this);
 
-             }
 
-             else
 
-             {
 
-                 base.Dispose();
 
-             }
 
-             this.InstanceID = 0;
 
-             this.IsRegister = false;
 
-         }
 
-         public override void ActiveObj()
 
-         {
 
-             
 
-         }
 
-         public override void DormancyObj()
 
-         {
 
-            
 
-         }
 
-         public virtual bool IsUpdate()
 
-         {
 
-             return true;
 
-         }
 
-         private Dictionary<Type, Entity> _components;
 
-         public Dictionary<Type, Entity> Components
 
-         {
 
-             get
 
-             {
 
-                 if (this._components == null)
 
-                 {
 
-                     this._components = _dictPool.Fetch();
 
-                 }
 
-                 return this._components;
 
-             }
 
-         }
 
-         protected Entity parent;
 
-         // 可以改变parent,但是不能设置为null(只能通过调用dispose重置为null)
 
-         public Entity Parent
 
-         {
 
-             get => this.parent;
 
-             set
 
-             {
 
-                 if (value == null)
 
-                 {
 
-                     // throw new Exception($"cant set parent null: {this.GetType().Name}");
 
-                     LogTool.Exception(new Exception($"cant set parent null: {this.GetType().Name}"));
 
-                 }
 
-                 if (value == this)
 
-                 {
 
-                     LogTool.Exception(new Exception($"cant set parent self: {this.GetType().Name}"));
 
-                 }
 
-                 if (this.parent != null) // 之前有parent
 
-                 {
 
-                     // parent相同,不设置
 
-                     if (this.parent == value)
 
-                     {
 
-                         LogTool.Exception(new Exception($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}"));
 
-                     }
 
-                     this.parent.RemoveChild(this);
 
-                 }
 
-                 this.parent = value;
 
-                 this.parent.AddChild(this);
 
-                 this._isComponent = false;
 
-                 this.Domain = this.parent.domain;
 
-             }
 
-         }
 
-         // 该方法只能在AddComponent中调用,其他人不允许调用
 
-         private Entity ComponentParent
 
-         {
 
-             set
 
-             {
 
-                 if (this.parent != null)
 
-                 {
 
-                     LogTool.Exception(new Exception($"Component parent is not null: {this.GetType().Name}"));
 
-                 }
 
-                 this.parent = value;
 
-                 this._isComponent = true;
 
-                 this.Domain = this.parent.domain;
 
-             }
 
-         }
 
-         private Dictionary<long, Entity> _children;
 
-         public Dictionary<long, Entity> Children
 
-         {
 
-             get
 
-             {
 
-                 if (this._children == null)
 
-                 {
 
-                     this._children = _childrenPool.Fetch();
 
-                 }
 
-                 return this._children;
 
-             }
 
-         }
 
-         protected Entity domain;
 
-         /// <summary>
 
-         /// 域,数据树的root
 
-         /// </summary>
 
-         /// <exception cref="Exception"></exception>
 
-         public Entity Domain
 
-         {
 
-             get { return this.domain; }
 
-             private set
 
-             {
 
-                 if (value == null)
 
-                 {
 
-                     LogTool.Exception(new Exception($"domain cant set null: {this.GetType().Name}"));
 
-                 }
 
-                 if (this.domain == value)
 
-                 {
 
-                     return;
 
-                 }
 
-                 Entity preDomain = this.domain;
 
-                 this.domain = value;
 
-                 if (preDomain == null)
 
-                 {
 
-                     this.InstanceID = IdGenerater.GenerateId();
 
-                     this.IsRegister = true;
 
-                 }
 
-                 // 递归设置孩子的Domain
 
-                 if (this._children != null)
 
-                 {
 
-                     foreach (Entity entity in this._children.Values)
 
-                     {
 
-                         entity.Domain = this.domain;
 
-                     }
 
-                 }
 
-                 if (this._components != null)
 
-                 {
 
-                     foreach (Entity component in this._components.Values)
 
-                     {
 
-                         component.Domain = this.domain;
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         #region 获取组件(包含获取children的)
 
-         public K GetChild<K>(long id) where K : Entity
 
-         {
 
-             if (this._children == null)
 
-             {
 
-                 return null;
 
-             }
 
-             this._children.TryGetValue(id, out Entity child);
 
-             return child as K;
 
-         }
 
-         public virtual K GetComponent<K>() where K : Entity
 
-         {
 
-             if (this._components == null)
 
-             {
 
-                 return null;
 
-             }
 
-             Entity component;
 
-             if (!this._components.TryGetValue(typeof(K), out component))
 
-             {
 
-                 return default;
 
-             }
 
-             return (K) component;
 
-         }
 
-         
 
-        
 
-         public virtual K[] GetComponentAll<K>() where K : Entity
 
-         {
 
-             if (this._components == null)
 
-             {
 
-                 return null;
 
-             }
 
-             Entity component;
 
-             List<K> allValue = ListPool<K>.Get();
 
-             foreach (var VARIABLE in _components.Values)
 
-             {
 
-                 if (VARIABLE is K)
 
-                 {
 
-                     allValue.Add(VARIABLE as K);
 
-                 }
 
-             }
 
-             K[] array = allValue.ToArray();
 
-             ListPool<K>.Release(allValue);
 
-             return array;
 
-         }
 
-         public virtual Entity GetComponent(Type type)
 
-         {
 
-             if (this._components == null)
 
-             {
 
-                 return null;
 
-             }
 
-             Entity component;
 
-             if (!this._components.TryGetValue(type, out component))
 
-             {
 
-                 return null;
 
-             }
 
-             return component;
 
-         }
 
-         #endregion
 
-         #region 添加组件
 
-         public Entity AddComponent(Entity component)
 
-         {
 
-             Type type = component.GetType();
 
-             if (this._components != null && this._components.ContainsKey(type))
 
-             {
 
-                 LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
 
-             }
 
-             component.ComponentParent = this;
 
-             this.AddToComponent(type, component);
 
-             return component;
 
-         }
 
-         public Entity AddComponent(Type type, bool isFromPool = false)
 
-         {
 
-             if (this._components != null && this._components.ContainsKey(type))
 
-             {
 
-                 LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
 
-             }
 
-             Entity component = Create(type, isFromPool);
 
-             component.ID = this.ID;
 
-             component.ComponentParent = this;
 
-             EventSystem.Instance.Awake(component, null);
 
-             this.AddToComponent(type, component);
 
-             return component;
 
-         }
 
-         public K AddComponent<K>(bool isFromPool = false) where K : Entity, new()
 
-         {
 
-             Type type = typeof(K);
 
-             if (this._components != null && this._components.ContainsKey(type))
 
-             {
 
-                 LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
 
-             }
 
-             // return default;
 
-             Entity component = Create(type, isFromPool);
 
-             component.ID = this.ID;
 
-             component.ComponentParent = this;
 
-             EventSystem.Instance.Awake(component, null);
 
-             this.AddToComponent(type, component);
 
-             return component as K;
 
-         }
 
-         public K AddComponent<K, P1>(P1 p1, bool isFromPool = false) where K : Entity, new()
 
-         {
 
-             Type type = typeof(K);
 
-             if (this._components != null && this._components.ContainsKey(type))
 
-             {
 
-                 LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
 
-             }
 
-             Entity component = Create(type, isFromPool);
 
-             component.ID = this.ID;
 
-             component.ComponentParent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {p1});
 
-             this.AddToComponent(type, component);
 
-             return component as K;
 
-         }
 
-         
 
-         
 
-         public Entity AddComponent< P1>(Type type1, P1 p1, bool isFromPool = false)
 
-         {
 
-             Type type = type1;
 
-             if (this._components != null && this._components.ContainsKey(type))
 
-             {
 
-                 LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
 
-             }
 
-             Entity component = Create(type, isFromPool);
 
-             component.ID = this.ID;
 
-             component.ComponentParent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {p1});
 
-             this.AddToComponent(type, component);
 
-             return component ;
 
-         }
 
-         public K AddComponent<K, P1, P2>(P1 p1, P2 p2, bool isFromPool = false) where K : Entity, new()
 
-         {
 
-             Type type = typeof(K);
 
-             if (this._components != null && this._components.ContainsKey(type))
 
-             {
 
-                 LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
 
-             }
 
-             Entity component = Create(type, isFromPool);
 
-             component.ID = this.ID;
 
-             component.ComponentParent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {p1, p2});
 
-             this.AddToComponent(type, component);
 
-             return component as K;
 
-         }
 
-         public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3, bool isFromPool = false) where K : Entity, new()
 
-         {
 
-             Type type = typeof(K);
 
-             if (this._components != null && this._components.ContainsKey(type))
 
-             {
 
-                 LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
 
-             }
 
-             Entity component = Create(type, isFromPool);
 
-             component.ID = this.ID;
 
-             component.ComponentParent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {p1, p2, p3});
 
-             this.AddToComponent(type, component);
 
-             return component as K;
 
-         }
 
-         #endregion
 
-         #region 移除组件
 
-         public void RemoveComponent<K>() where K : Entity
 
-         {
 
-             if (this._components == null)
 
-             {
 
-                 return;
 
-             }
 
-             Type type = typeof(K);
 
-             Entity c = this.GetComponent(type);
 
-             if (c == null)
 
-             {
 
-                 return;
 
-             }
 
-             this.RemoveFromComponent(type, c);
 
-             c.Dispose();
 
-         }
 
-         public void RemoveComponent(Entity component)
 
-         {
 
-             if (this._components == null)
 
-             {
 
-                 return;
 
-             }
 
-             Type type = component.GetType();
 
-             Entity c = this.GetComponent(component.GetType());
 
-             if (c == null)
 
-             {
 
-                 return;
 
-             }
 
-             this.RemoveFromComponent(type, c);
 
-             c.Dispose();
 
-         }
 
-         public void RemoveComponent(Type type)
 
-         {
 
-             Entity c = this.GetComponent(type);
 
-             if (c == null)
 
-             {
 
-                 return;
 
-             }
 
-             RemoveFromComponent(type, c);
 
-             c.Dispose();
 
-         }
 
-         #endregion
 
-         #region 添加组件为child
 
-         public T AddChild<T>(bool isFromPool = false) where T : Entity
 
-         {
 
-             Type type = typeof(T);
 
-             T component = (T) Entity.Create(type, isFromPool);
 
-             component.ID = IdGenerater.GenerateId();
 
-             component.Parent = this;
 
-             EventSystem.Instance.Awake(component, null);
 
-             return component;
 
-         }
 
-         public T AddChild<T, A>(A a, bool isFromPool = false) where T : Entity
 
-         {
 
-             Type type = typeof(T);
 
-             T component = (T) Entity.Create(type, isFromPool);
 
-             component.ID = IdGenerater.GenerateId();
 
-             component.Parent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {a});
 
-             return component;
 
-         }
 
-         public T AddChild<T, A, B>(A a, B b, bool isFromPool = false) where T : Entity
 
-         {
 
-             Type type = typeof(T);
 
-             T component = (T) Entity.Create(type, isFromPool);
 
-             component.ID = IdGenerater.GenerateId();
 
-             component.Parent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {a, b});
 
-             return component;
 
-         }
 
-         public T AddChild<T, A, B, C>(A a, B b, C c, bool isFromPool = false) where T : Entity
 
-         {
 
-             Type type = typeof(T);
 
-             T component = (T) Entity.Create(type, isFromPool);
 
-             component.ID = IdGenerater.GenerateId();
 
-             component.Parent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {a, b, c});
 
-             return component;
 
-         }
 
-         public T AddChild<T, A, B, C, D>(A a, B b, C c, D d, bool isFromPool = false) where T : Entity
 
-         {
 
-             Type type = typeof(T);
 
-             T component = (T) Entity.Create(type, isFromPool);
 
-             component.ID = IdGenerater.GenerateId();
 
-             component.Parent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {a, b, c, d});
 
-             return component;
 
-         }
 
-         public T AddChild<T, A, B, C, D, E>(A a, B b, C c, D d, E e, bool isFromPool = false) where T : Entity
 
-         {
 
-             Type type = typeof(T);
 
-             T component = (T) Entity.Create(type, isFromPool);
 
-             component.ID = IdGenerater.GenerateId();
 
-             component.Parent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {a, b, c, d, e});
 
-             return component;
 
-         }
 
-         public T AddChildWithId<T>(long id, bool isFromPool = false) where T : Entity
 
-         {
 
-             Type type = typeof(T);
 
-             T component = (T) Entity.Create(type, isFromPool);
 
-             component.ID = id;
 
-             component.Parent = this;
 
-             EventSystem.Instance.Awake(component, null);
 
-             return component;
 
-         }
 
-         public T AddChildWithId<T, A>(long id, A a, bool isFromPool = false) where T : Entity
 
-         {
 
-             Type type = typeof(T);
 
-             T component = (T) Entity.Create(type, isFromPool);
 
-             component.ID = id;
 
-             component.Parent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {a});
 
-             return component;
 
-         }
 
-         public T AddChildWithId<T, A, B>(long id, A a, B b, bool isFromPool = false) where T : Entity
 
-         {
 
-             Type type = typeof(T);
 
-             T component = (T) Entity.Create(type, isFromPool);
 
-             component.ID = id;
 
-             component.Parent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {a, b});
 
-             return component;
 
-         }
 
-         public T AddChildWithId<T, A, B, C>(long id, A a, B b, C c, bool isFromPool = false) where T : Entity
 
-         {
 
-             Type type = typeof(T);
 
-             T component = (T) Entity.Create(type, isFromPool);
 
-             component.ID = id;
 
-             component.Parent = this;
 
-             EventSystem.Instance.Awake(component, new object[] {a, b, c});
 
-             return component;
 
-         }
 
-         #endregion
 
-         #region 私有方法,增删组件和增删child
 
-         private void AddChild(Entity entity)
 
-         {
 
-             this.Children.Add(entity.ID, entity);
 
-         }
 
-         private void RemoveChild(Entity entity)
 
-         {
 
-             if (this._children == null)
 
-             {
 
-                 return;
 
-             }
 
-             this._children.Remove(entity.ID);
 
-             if (this._children.Count == 0)
 
-             {
 
-                 _childrenPool.Recycle(this._children);
 
-                 this._children = null;
 
-             }
 
-         }
 
-         private void AddToComponent(Type type, Entity component)
 
-         {
 
-             if (this._components == null)
 
-             {
 
-                 this._components = _dictPool.Fetch();
 
-             }
 
-             this._components.Add(type, component);
 
-         }
 
-         private void RemoveFromComponent(Type type, Entity component)
 
-         {
 
-             if (this._components == null)
 
-             {
 
-                 return;
 
-             }
 
-             this._components.Remove(type);
 
-             if (this._components.Count == 0 && this._isFromPool)
 
-             {
 
-                 _dictPool.Recycle(this._components);
 
-                 this._components = null;
 
-             }
 
-         }
 
-         #endregion
 
-     }
 
-     public class Pool<T> where T : class, new()
 
-     {
 
-         private readonly Queue<T> _pool = new Queue<T>();
 
-         public T Fetch()
 
-         {
 
-             if (_pool.Count == 0)
 
-             {
 
-                 return new T();
 
-             }
 
-             return _pool.Dequeue();
 
-         }
 
-         public void Recycle(T t)
 
-         {
 
-             _pool.Enqueue(t);
 
-         }
 
-         public void Clear()
 
-         {
 
-             this._pool.Clear();
 
-         }
 
-     }
 
- }
 
 
  |