Entity.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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 virtual bool IsUpdate()
  129. {
  130. return true;
  131. }
  132. private Dictionary<Type, Entity> _components;
  133. public Dictionary<Type, Entity> Components
  134. {
  135. get
  136. {
  137. if (this._components == null)
  138. {
  139. this._components = _dictPool.Fetch();
  140. }
  141. return this._components;
  142. }
  143. }
  144. protected Entity parent;
  145. // 可以改变parent,但是不能设置为null(只能通过调用dispose重置为null)
  146. public Entity Parent
  147. {
  148. get => this.parent;
  149. set
  150. {
  151. if (value == null)
  152. {
  153. // throw new Exception($"cant set parent null: {this.GetType().Name}");
  154. LogTool.Exception(new Exception($"cant set parent null: {this.GetType().Name}"));
  155. }
  156. if (value == this)
  157. {
  158. LogTool.Exception(new Exception($"cant set parent self: {this.GetType().Name}"));
  159. }
  160. if (this.parent != null) // 之前有parent
  161. {
  162. // parent相同,不设置
  163. if (this.parent == value)
  164. {
  165. LogTool.Exception(new Exception($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}"));
  166. }
  167. this.parent.RemoveChild(this);
  168. }
  169. this.parent = value;
  170. this.parent.AddChild(this);
  171. this._isComponent = false;
  172. this.Domain = this.parent.domain;
  173. }
  174. }
  175. // 该方法只能在AddComponent中调用,其他人不允许调用
  176. private Entity ComponentParent
  177. {
  178. set
  179. {
  180. if (this.parent != null)
  181. {
  182. LogTool.Exception(new Exception($"Component parent is not null: {this.GetType().Name}"));
  183. }
  184. this.parent = value;
  185. this._isComponent = true;
  186. this.Domain = this.parent.domain;
  187. }
  188. }
  189. private Dictionary<long, Entity> _children;
  190. public Dictionary<long, Entity> Children
  191. {
  192. get
  193. {
  194. if (this._children == null)
  195. {
  196. this._children = _childrenPool.Fetch();
  197. }
  198. return this._children;
  199. }
  200. }
  201. protected Entity domain;
  202. /// <summary>
  203. /// 域,数据树的root
  204. /// </summary>
  205. /// <exception cref="Exception"></exception>
  206. public Entity Domain
  207. {
  208. get { return this.domain; }
  209. private set
  210. {
  211. if (value == null)
  212. {
  213. LogTool.Exception(new Exception($"domain cant set null: {this.GetType().Name}"));
  214. }
  215. if (this.domain == value)
  216. {
  217. return;
  218. }
  219. Entity preDomain = this.domain;
  220. this.domain = value;
  221. if (preDomain == null)
  222. {
  223. this.InstanceID = IdGenerater.GenerateId();
  224. this.IsRegister = true;
  225. }
  226. // 递归设置孩子的Domain
  227. if (this._children != null)
  228. {
  229. foreach (Entity entity in this._children.Values)
  230. {
  231. entity.Domain = this.domain;
  232. }
  233. }
  234. if (this._components != null)
  235. {
  236. foreach (Entity component in this._components.Values)
  237. {
  238. component.Domain = this.domain;
  239. }
  240. }
  241. }
  242. }
  243. #region 获取组件(包含获取children的)
  244. public K GetChild<K>(long id) where K : Entity
  245. {
  246. if (this._children == null)
  247. {
  248. return null;
  249. }
  250. this._children.TryGetValue(id, out Entity child);
  251. return child as K;
  252. }
  253. public virtual K GetComponent<K>() where K : Entity
  254. {
  255. if (this._components == null)
  256. {
  257. return null;
  258. }
  259. Entity component;
  260. if (!this._components.TryGetValue(typeof(K), out component))
  261. {
  262. return default;
  263. }
  264. return (K) component;
  265. }
  266. public virtual K[] GetComponentAll<K>() where K : Entity
  267. {
  268. if (this._components == null)
  269. {
  270. return null;
  271. }
  272. Entity component;
  273. List<K> allValue = ListPool<K>.Get();
  274. foreach (var VARIABLE in _components.Values)
  275. {
  276. if (VARIABLE is K)
  277. {
  278. allValue.Add(VARIABLE as K);
  279. }
  280. }
  281. K[] array = allValue.ToArray();
  282. ListPool<K>.Release(allValue);
  283. return array;
  284. }
  285. public virtual Entity GetComponent(Type type)
  286. {
  287. if (this._components == null)
  288. {
  289. return null;
  290. }
  291. Entity component;
  292. if (!this._components.TryGetValue(type, out component))
  293. {
  294. return null;
  295. }
  296. return component;
  297. }
  298. #endregion
  299. #region 添加组件
  300. public Entity AddComponent(Entity component)
  301. {
  302. Type type = component.GetType();
  303. if (this._components != null && this._components.ContainsKey(type))
  304. {
  305. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  306. }
  307. component.ComponentParent = this;
  308. this.AddToComponent(type, component);
  309. return component;
  310. }
  311. public Entity AddComponent(Type type, bool isFromPool = false)
  312. {
  313. if (this._components != null && this._components.ContainsKey(type))
  314. {
  315. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  316. }
  317. Entity component = Create(type, isFromPool);
  318. component.ID = this.ID;
  319. component.ComponentParent = this;
  320. EventSystem.Instance.Awake(component, null);
  321. this.AddToComponent(type, component);
  322. return component;
  323. }
  324. public K AddComponent<K>(bool isFromPool = false) where K : Entity, new()
  325. {
  326. Type type = typeof(K);
  327. if (this._components != null && this._components.ContainsKey(type))
  328. {
  329. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  330. }
  331. // return default;
  332. Entity component = Create(type, isFromPool);
  333. component.ID = this.ID;
  334. component.ComponentParent = this;
  335. EventSystem.Instance.Awake(component, null);
  336. this.AddToComponent(type, component);
  337. return component as K;
  338. }
  339. public K AddComponent<K, P1>(P1 p1, bool isFromPool = false) where K : Entity, new()
  340. {
  341. Type type = typeof(K);
  342. if (this._components != null && this._components.ContainsKey(type))
  343. {
  344. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  345. }
  346. Entity component = Create(type, isFromPool);
  347. component.ID = this.ID;
  348. component.ComponentParent = this;
  349. EventSystem.Instance.Awake(component, new object[] {p1});
  350. this.AddToComponent(type, component);
  351. return component as K;
  352. }
  353. public K AddComponent<K, P1, P2>(P1 p1, P2 p2, bool isFromPool = false) where K : Entity, new()
  354. {
  355. Type type = typeof(K);
  356. if (this._components != null && this._components.ContainsKey(type))
  357. {
  358. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  359. }
  360. Entity component = Create(type, isFromPool);
  361. component.ID = this.ID;
  362. component.ComponentParent = this;
  363. EventSystem.Instance.Awake(component, new object[] {p1, p2});
  364. this.AddToComponent(type, component);
  365. return component as K;
  366. }
  367. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3, bool isFromPool = false) where K : Entity, new()
  368. {
  369. Type type = typeof(K);
  370. if (this._components != null && this._components.ContainsKey(type))
  371. {
  372. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  373. }
  374. Entity component = Create(type, isFromPool);
  375. component.ID = this.ID;
  376. component.ComponentParent = this;
  377. EventSystem.Instance.Awake(component, new object[] {p1, p2, p3});
  378. this.AddToComponent(type, component);
  379. return component as K;
  380. }
  381. #endregion
  382. #region 移除组件
  383. public void RemoveComponent<K>() where K : Entity
  384. {
  385. if (this._components == null)
  386. {
  387. return;
  388. }
  389. Type type = typeof(K);
  390. Entity c = this.GetComponent(type);
  391. if (c == null)
  392. {
  393. return;
  394. }
  395. this.RemoveFromComponent(type, c);
  396. c.Dispose();
  397. }
  398. public void RemoveComponent(Entity component)
  399. {
  400. if (this._components == null)
  401. {
  402. return;
  403. }
  404. Type type = component.GetType();
  405. Entity c = this.GetComponent(component.GetType());
  406. if (c == null)
  407. {
  408. return;
  409. }
  410. this.RemoveFromComponent(type, c);
  411. c.Dispose();
  412. }
  413. public void RemoveComponent(Type type)
  414. {
  415. Entity c = this.GetComponent(type);
  416. if (c == null)
  417. {
  418. return;
  419. }
  420. RemoveFromComponent(type, c);
  421. c.Dispose();
  422. }
  423. #endregion
  424. #region 添加组件为child
  425. public T AddChild<T>(bool isFromPool = false) where T : Entity
  426. {
  427. Type type = typeof(T);
  428. T component = (T) Entity.Create(type, isFromPool);
  429. component.ID = IdGenerater.GenerateId();
  430. component.Parent = this;
  431. EventSystem.Instance.Awake(component, null);
  432. return component;
  433. }
  434. public T AddChild<T, A>(A a, bool isFromPool = false) where T : Entity
  435. {
  436. Type type = typeof(T);
  437. T component = (T) Entity.Create(type, isFromPool);
  438. component.ID = IdGenerater.GenerateId();
  439. component.Parent = this;
  440. EventSystem.Instance.Awake(component, new object[] {a});
  441. return component;
  442. }
  443. public T AddChild<T, A, B>(A a, B b, bool isFromPool = false) where T : Entity
  444. {
  445. Type type = typeof(T);
  446. T component = (T) Entity.Create(type, isFromPool);
  447. component.ID = IdGenerater.GenerateId();
  448. component.Parent = this;
  449. EventSystem.Instance.Awake(component, new object[] {a, b});
  450. return component;
  451. }
  452. public T AddChild<T, A, B, C>(A a, B b, C c, bool isFromPool = false) where T : Entity
  453. {
  454. Type type = typeof(T);
  455. T component = (T) Entity.Create(type, isFromPool);
  456. component.ID = IdGenerater.GenerateId();
  457. component.Parent = this;
  458. EventSystem.Instance.Awake(component, new object[] {a, b, c});
  459. return component;
  460. }
  461. public T AddChild<T, A, B, C, D>(A a, B b, C c, D d, bool isFromPool = false) where T : Entity
  462. {
  463. Type type = typeof(T);
  464. T component = (T) Entity.Create(type, isFromPool);
  465. component.ID = IdGenerater.GenerateId();
  466. component.Parent = this;
  467. EventSystem.Instance.Awake(component, new object[] {a, b, c, d});
  468. return component;
  469. }
  470. 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
  471. {
  472. Type type = typeof(T);
  473. T component = (T) Entity.Create(type, isFromPool);
  474. component.ID = IdGenerater.GenerateId();
  475. component.Parent = this;
  476. EventSystem.Instance.Awake(component, new object[] {a, b, c, d, e});
  477. return component;
  478. }
  479. public T AddChildWithId<T>(long id, bool isFromPool = false) where T : Entity
  480. {
  481. Type type = typeof(T);
  482. T component = (T) Entity.Create(type, isFromPool);
  483. component.ID = id;
  484. component.Parent = this;
  485. EventSystem.Instance.Awake(component, null);
  486. return component;
  487. }
  488. public T AddChildWithId<T, A>(long id, A a, bool isFromPool = false) where T : Entity
  489. {
  490. Type type = typeof(T);
  491. T component = (T) Entity.Create(type, isFromPool);
  492. component.ID = id;
  493. component.Parent = this;
  494. EventSystem.Instance.Awake(component, new object[] {a});
  495. return component;
  496. }
  497. public T AddChildWithId<T, A, B>(long id, A a, B b, bool isFromPool = false) where T : Entity
  498. {
  499. Type type = typeof(T);
  500. T component = (T) Entity.Create(type, isFromPool);
  501. component.ID = id;
  502. component.Parent = this;
  503. EventSystem.Instance.Awake(component, new object[] {a, b});
  504. return component;
  505. }
  506. public T AddChildWithId<T, A, B, C>(long id, A a, B b, C c, bool isFromPool = false) where T : Entity
  507. {
  508. Type type = typeof(T);
  509. T component = (T) Entity.Create(type, isFromPool);
  510. component.ID = id;
  511. component.Parent = this;
  512. EventSystem.Instance.Awake(component, new object[] {a, b, c});
  513. return component;
  514. }
  515. #endregion
  516. #region 私有方法,增删组件和增删child
  517. private void AddChild(Entity entity)
  518. {
  519. this.Children.Add(entity.ID, entity);
  520. }
  521. private void RemoveChild(Entity entity)
  522. {
  523. if (this._children == null)
  524. {
  525. return;
  526. }
  527. this._children.Remove(entity.ID);
  528. if (this._children.Count == 0)
  529. {
  530. _childrenPool.Recycle(this._children);
  531. this._children = null;
  532. }
  533. }
  534. private void AddToComponent(Type type, Entity component)
  535. {
  536. if (this._components == null)
  537. {
  538. this._components = _dictPool.Fetch();
  539. }
  540. this._components.Add(type, component);
  541. }
  542. private void RemoveFromComponent(Type type, Entity component)
  543. {
  544. if (this._components == null)
  545. {
  546. return;
  547. }
  548. this._components.Remove(type);
  549. if (this._components.Count == 0 && this._isFromPool)
  550. {
  551. _dictPool.Recycle(this._components);
  552. this._components = null;
  553. }
  554. }
  555. #endregion
  556. }
  557. public class Pool<T> where T : class, new()
  558. {
  559. private readonly Queue<T> _pool = new Queue<T>();
  560. public T Fetch()
  561. {
  562. if (_pool.Count == 0)
  563. {
  564. return new T();
  565. }
  566. return _pool.Dequeue();
  567. }
  568. public void Recycle(T t)
  569. {
  570. _pool.Enqueue(t);
  571. }
  572. public void Clear()
  573. {
  574. this._pool.Clear();
  575. }
  576. }
  577. }