Entity.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. using System;
  2. using System.Collections.Generic;
  3. using Fort23.UTool;
  4. using UnityEngine;
  5. using UnityEngine.Pool;
  6. namespace Fort23.Core
  7. {
  8. public class Scene : Entity
  9. {
  10. public new Entity Domain
  11. {
  12. get => this.domain;
  13. set => this.domain = value;
  14. }
  15. public Scene()
  16. {
  17. this.IsRegister = true;
  18. this.Domain = this;
  19. }
  20. }
  21. public partial class Entity : CObject
  22. {
  23. private static readonly Pool<Dictionary<Type, Entity>> _dictPool = new Pool<Dictionary<Type, Entity>>();
  24. private static readonly Pool<Dictionary<long, Entity>> _childrenPool = new Pool<Dictionary<long, Entity>>();
  25. public long InstanceID { get; protected set; }
  26. public bool IsDisposed => this.InstanceID == 0;
  27. public long ID { get; set; }
  28. private bool _isFromPool { get; set; }
  29. private bool _isComponent { get; set; }
  30. private bool _isRegister;
  31. protected bool IsRegister
  32. {
  33. get => _isRegister;
  34. set
  35. {
  36. _isRegister = value;
  37. EventSystem.Instance.RegisterSystem(this, value);
  38. }
  39. }
  40. protected static Entity Create(Type type, bool isFromPool)
  41. {
  42. Entity component;
  43. if (isFromPool)
  44. {
  45. component = (Entity) CObjectPool.Instance.Fetch(type);
  46. }
  47. else
  48. {
  49. component = (Entity) Activator.CreateInstance(type);
  50. }
  51. return component;
  52. }
  53. public override void Dispose()
  54. {
  55. if (this.IsDisposed)
  56. {
  57. return;
  58. }
  59. // 清理Component
  60. if (this._components != null)
  61. {
  62. List<Type> keys = new List<Type>();
  63. foreach (var componentsKey in _components.Keys)
  64. {
  65. keys.Add(componentsKey);
  66. }
  67. for (var i = 0; i < keys.Count; i++)
  68. {
  69. _components[keys[i]].Dispose();
  70. }
  71. // foreach (KeyValuePair<Type, Entity> kv in this._components)
  72. // {
  73. // kv.Value.Dispose();
  74. // }
  75. this._components.Clear();
  76. _dictPool.Recycle(this._components);
  77. this._components = null;
  78. }
  79. // 清理Children
  80. if (this._children != null)
  81. {
  82. List<long> keys = new List<long>();
  83. foreach (var childrenKey in _children.Keys)
  84. {
  85. keys.Add(childrenKey);
  86. }
  87. for (var i = 0; i < keys.Count; i++)
  88. {
  89. _children[keys[i]].Dispose();
  90. }
  91. // foreach (Entity child in this._children.Values)
  92. // {
  93. // child.Dispose();
  94. // }
  95. if (_children != null)
  96. {
  97. this._children.Clear();
  98. _childrenPool.Recycle(this._children);
  99. this._children = null;
  100. }
  101. }
  102. // 触发Destroy事件
  103. EventSystem.Instance.Destroy(this);
  104. this.domain = null;
  105. if (this.parent != null && !this.parent.IsDisposed)
  106. {
  107. if (this._isComponent)
  108. {
  109. this.parent.RemoveComponent(this);
  110. }
  111. else
  112. {
  113. this.parent.RemoveChild(this);
  114. }
  115. }
  116. this.parent = null;
  117. if (this._isFromPool)
  118. {
  119. CObjectPool.Instance.Recycle(this);
  120. }
  121. else
  122. {
  123. base.Dispose();
  124. }
  125. this.InstanceID = 0;
  126. this.IsRegister = false;
  127. }
  128. public override void ActiveObj()
  129. {
  130. }
  131. public override void DormancyObj()
  132. {
  133. }
  134. public virtual bool IsUpdate()
  135. {
  136. return true;
  137. }
  138. private Dictionary<Type, Entity> _components;
  139. public Dictionary<Type, Entity> Components
  140. {
  141. get
  142. {
  143. if (this._components == null)
  144. {
  145. this._components = _dictPool.Fetch();
  146. }
  147. return this._components;
  148. }
  149. }
  150. protected Entity parent;
  151. // 可以改变parent,但是不能设置为null(只能通过调用dispose重置为null)
  152. public Entity Parent
  153. {
  154. get => this.parent;
  155. set
  156. {
  157. if (value == null)
  158. {
  159. // throw new Exception($"cant set parent null: {this.GetType().Name}");
  160. LogTool.Exception(new Exception($"cant set parent null: {this.GetType().Name}"));
  161. }
  162. if (value == this)
  163. {
  164. LogTool.Exception(new Exception($"cant set parent self: {this.GetType().Name}"));
  165. }
  166. if (this.parent != null) // 之前有parent
  167. {
  168. // parent相同,不设置
  169. if (this.parent == value)
  170. {
  171. LogTool.Exception(new Exception($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}"));
  172. }
  173. this.parent.RemoveChild(this);
  174. }
  175. this.parent = value;
  176. this.parent.AddChild(this);
  177. this._isComponent = false;
  178. this.Domain = this.parent.domain;
  179. }
  180. }
  181. // 该方法只能在AddComponent中调用,其他人不允许调用
  182. private Entity ComponentParent
  183. {
  184. set
  185. {
  186. if (this.parent != null)
  187. {
  188. LogTool.Exception(new Exception($"Component parent is not null: {this.GetType().Name}"));
  189. }
  190. this.parent = value;
  191. this._isComponent = true;
  192. this.Domain = this.parent.domain;
  193. }
  194. }
  195. private Dictionary<long, Entity> _children;
  196. public Dictionary<long, Entity> Children
  197. {
  198. get
  199. {
  200. if (this._children == null)
  201. {
  202. this._children = _childrenPool.Fetch();
  203. }
  204. return this._children;
  205. }
  206. }
  207. protected Entity domain;
  208. /// <summary>
  209. /// 域,数据树的root
  210. /// </summary>
  211. /// <exception cref="Exception"></exception>
  212. public Entity Domain
  213. {
  214. get { return this.domain; }
  215. private set
  216. {
  217. if (value == null)
  218. {
  219. LogTool.Exception(new Exception($"domain cant set null: {this.GetType().Name}"));
  220. }
  221. if (this.domain == value)
  222. {
  223. return;
  224. }
  225. Entity preDomain = this.domain;
  226. this.domain = value;
  227. if (preDomain == null)
  228. {
  229. this.InstanceID = IdGenerater.GenerateId();
  230. this.IsRegister = true;
  231. }
  232. // 递归设置孩子的Domain
  233. if (this._children != null)
  234. {
  235. foreach (Entity entity in this._children.Values)
  236. {
  237. entity.Domain = this.domain;
  238. }
  239. }
  240. if (this._components != null)
  241. {
  242. foreach (Entity component in this._components.Values)
  243. {
  244. component.Domain = this.domain;
  245. }
  246. }
  247. }
  248. }
  249. #region 获取组件(包含获取children的)
  250. public K GetChild<K>(long id) where K : Entity
  251. {
  252. if (this._children == null)
  253. {
  254. return null;
  255. }
  256. this._children.TryGetValue(id, out Entity child);
  257. return child as K;
  258. }
  259. public virtual K GetComponent<K>() where K : Entity
  260. {
  261. if (this._components == null)
  262. {
  263. return null;
  264. }
  265. Entity component;
  266. if (!this._components.TryGetValue(typeof(K), out component))
  267. {
  268. return default;
  269. }
  270. return (K) component;
  271. }
  272. public virtual K[] GetComponentAll<K>() where K : Entity
  273. {
  274. if (this._components == null)
  275. {
  276. return null;
  277. }
  278. Entity component;
  279. List<K> allValue = ListPool<K>.Get();
  280. foreach (var VARIABLE in _components.Values)
  281. {
  282. if (VARIABLE is K)
  283. {
  284. allValue.Add(VARIABLE as K);
  285. }
  286. }
  287. K[] array = allValue.ToArray();
  288. ListPool<K>.Release(allValue);
  289. return array;
  290. }
  291. public virtual Entity GetComponent(Type type)
  292. {
  293. if (this._components == null)
  294. {
  295. return null;
  296. }
  297. Entity component;
  298. if (!this._components.TryGetValue(type, out component))
  299. {
  300. return null;
  301. }
  302. return component;
  303. }
  304. #endregion
  305. #region 添加组件
  306. public Entity AddComponent(Entity component)
  307. {
  308. Type type = component.GetType();
  309. if (this._components != null && this._components.ContainsKey(type))
  310. {
  311. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  312. }
  313. component.ComponentParent = this;
  314. this.AddToComponent(type, component);
  315. return component;
  316. }
  317. public Entity AddComponent(Type type, bool isFromPool = false)
  318. {
  319. if (this._components != null && this._components.ContainsKey(type))
  320. {
  321. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  322. }
  323. Entity component = Create(type, isFromPool);
  324. component.ID = this.ID;
  325. component.ComponentParent = this;
  326. EventSystem.Instance.Awake(component, null);
  327. this.AddToComponent(type, component);
  328. return component;
  329. }
  330. public K AddComponent<K>(bool isFromPool = false) where K : Entity, new()
  331. {
  332. Type type = typeof(K);
  333. if (this._components != null && this._components.ContainsKey(type))
  334. {
  335. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  336. }
  337. // return default;
  338. Entity component = Create(type, isFromPool);
  339. component.ID = this.ID;
  340. component.ComponentParent = this;
  341. EventSystem.Instance.Awake(component, null);
  342. this.AddToComponent(type, component);
  343. return component as K;
  344. }
  345. public K AddComponent<K, P1>(P1 p1, bool isFromPool = false) where K : Entity, new()
  346. {
  347. Type type = typeof(K);
  348. if (this._components != null && this._components.ContainsKey(type))
  349. {
  350. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  351. }
  352. Entity component = Create(type, isFromPool);
  353. component.ID = this.ID;
  354. component.ComponentParent = this;
  355. EventSystem.Instance.Awake(component, new object[] {p1});
  356. this.AddToComponent(type, component);
  357. return component as K;
  358. }
  359. public K AddComponent<K, P1, P2>(P1 p1, P2 p2, bool isFromPool = false) where K : Entity, new()
  360. {
  361. Type type = typeof(K);
  362. if (this._components != null && this._components.ContainsKey(type))
  363. {
  364. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  365. }
  366. Entity component = Create(type, isFromPool);
  367. component.ID = this.ID;
  368. component.ComponentParent = this;
  369. EventSystem.Instance.Awake(component, new object[] {p1, p2});
  370. this.AddToComponent(type, component);
  371. return component as K;
  372. }
  373. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3, bool isFromPool = false) where K : Entity, new()
  374. {
  375. Type type = typeof(K);
  376. if (this._components != null && this._components.ContainsKey(type))
  377. {
  378. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  379. }
  380. Entity component = Create(type, isFromPool);
  381. component.ID = this.ID;
  382. component.ComponentParent = this;
  383. EventSystem.Instance.Awake(component, new object[] {p1, p2, p3});
  384. this.AddToComponent(type, component);
  385. return component as K;
  386. }
  387. #endregion
  388. #region 移除组件
  389. public void RemoveComponent<K>() where K : Entity
  390. {
  391. if (this._components == null)
  392. {
  393. return;
  394. }
  395. Type type = typeof(K);
  396. Entity c = this.GetComponent(type);
  397. if (c == null)
  398. {
  399. return;
  400. }
  401. this.RemoveFromComponent(type, c);
  402. c.Dispose();
  403. }
  404. public void RemoveComponent(Entity component)
  405. {
  406. if (this._components == null)
  407. {
  408. return;
  409. }
  410. Type type = component.GetType();
  411. Entity c = this.GetComponent(component.GetType());
  412. if (c == null)
  413. {
  414. return;
  415. }
  416. this.RemoveFromComponent(type, c);
  417. c.Dispose();
  418. }
  419. public void RemoveComponent(Type type)
  420. {
  421. Entity c = this.GetComponent(type);
  422. if (c == null)
  423. {
  424. return;
  425. }
  426. RemoveFromComponent(type, c);
  427. c.Dispose();
  428. }
  429. #endregion
  430. #region 添加组件为child
  431. public T AddChild<T>(bool isFromPool = false) where T : Entity
  432. {
  433. Type type = typeof(T);
  434. T component = (T) Entity.Create(type, isFromPool);
  435. component.ID = IdGenerater.GenerateId();
  436. component.Parent = this;
  437. EventSystem.Instance.Awake(component, null);
  438. return component;
  439. }
  440. public T AddChild<T, A>(A a, bool isFromPool = false) where T : Entity
  441. {
  442. Type type = typeof(T);
  443. T component = (T) Entity.Create(type, isFromPool);
  444. component.ID = IdGenerater.GenerateId();
  445. component.Parent = this;
  446. EventSystem.Instance.Awake(component, new object[] {a});
  447. return component;
  448. }
  449. public T AddChild<T, A, B>(A a, B b, bool isFromPool = false) where T : Entity
  450. {
  451. Type type = typeof(T);
  452. T component = (T) Entity.Create(type, isFromPool);
  453. component.ID = IdGenerater.GenerateId();
  454. component.Parent = this;
  455. EventSystem.Instance.Awake(component, new object[] {a, b});
  456. return component;
  457. }
  458. public T AddChild<T, A, B, C>(A a, B b, C c, bool isFromPool = false) where T : Entity
  459. {
  460. Type type = typeof(T);
  461. T component = (T) Entity.Create(type, isFromPool);
  462. component.ID = IdGenerater.GenerateId();
  463. component.Parent = this;
  464. EventSystem.Instance.Awake(component, new object[] {a, b, c});
  465. return component;
  466. }
  467. public T AddChild<T, A, B, C, D>(A a, B b, C c, D d, bool isFromPool = false) where T : Entity
  468. {
  469. Type type = typeof(T);
  470. T component = (T) Entity.Create(type, isFromPool);
  471. component.ID = IdGenerater.GenerateId();
  472. component.Parent = this;
  473. EventSystem.Instance.Awake(component, new object[] {a, b, c, d});
  474. return component;
  475. }
  476. 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
  477. {
  478. Type type = typeof(T);
  479. T component = (T) Entity.Create(type, isFromPool);
  480. component.ID = IdGenerater.GenerateId();
  481. component.Parent = this;
  482. EventSystem.Instance.Awake(component, new object[] {a, b, c, d, e});
  483. return component;
  484. }
  485. public T AddChildWithId<T>(long id, bool isFromPool = false) where T : Entity
  486. {
  487. Type type = typeof(T);
  488. T component = (T) Entity.Create(type, isFromPool);
  489. component.ID = id;
  490. component.Parent = this;
  491. EventSystem.Instance.Awake(component, null);
  492. return component;
  493. }
  494. public T AddChildWithId<T, A>(long id, A a, bool isFromPool = false) where T : Entity
  495. {
  496. Type type = typeof(T);
  497. T component = (T) Entity.Create(type, isFromPool);
  498. component.ID = id;
  499. component.Parent = this;
  500. EventSystem.Instance.Awake(component, new object[] {a});
  501. return component;
  502. }
  503. public T AddChildWithId<T, A, B>(long id, A a, B b, bool isFromPool = false) where T : Entity
  504. {
  505. Type type = typeof(T);
  506. T component = (T) Entity.Create(type, isFromPool);
  507. component.ID = id;
  508. component.Parent = this;
  509. EventSystem.Instance.Awake(component, new object[] {a, b});
  510. return component;
  511. }
  512. public T AddChildWithId<T, A, B, C>(long id, A a, B b, C c, bool isFromPool = false) where T : Entity
  513. {
  514. Type type = typeof(T);
  515. T component = (T) Entity.Create(type, isFromPool);
  516. component.ID = id;
  517. component.Parent = this;
  518. EventSystem.Instance.Awake(component, new object[] {a, b, c});
  519. return component;
  520. }
  521. #endregion
  522. #region 私有方法,增删组件和增删child
  523. private void AddChild(Entity entity)
  524. {
  525. this.Children.Add(entity.ID, entity);
  526. }
  527. private void RemoveChild(Entity entity)
  528. {
  529. if (this._children == null)
  530. {
  531. return;
  532. }
  533. this._children.Remove(entity.ID);
  534. if (this._children.Count == 0)
  535. {
  536. _childrenPool.Recycle(this._children);
  537. this._children = null;
  538. }
  539. }
  540. private void AddToComponent(Type type, Entity component)
  541. {
  542. if (this._components == null)
  543. {
  544. this._components = _dictPool.Fetch();
  545. }
  546. this._components.Add(type, component);
  547. }
  548. private void RemoveFromComponent(Type type, Entity component)
  549. {
  550. if (this._components == null)
  551. {
  552. return;
  553. }
  554. this._components.Remove(type);
  555. if (this._components.Count == 0 && this._isFromPool)
  556. {
  557. _dictPool.Recycle(this._components);
  558. this._components = null;
  559. }
  560. }
  561. #endregion
  562. }
  563. public class Pool<T> where T : class, new()
  564. {
  565. private readonly Queue<T> _pool = new Queue<T>();
  566. public T Fetch()
  567. {
  568. if (_pool.Count == 0)
  569. {
  570. return new T();
  571. }
  572. return _pool.Dequeue();
  573. }
  574. public void Recycle(T t)
  575. {
  576. _pool.Enqueue(t);
  577. }
  578. public void Clear()
  579. {
  580. this._pool.Clear();
  581. }
  582. }
  583. }