Entity.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 Entity AddComponent< P1>(Type type1, P1 p1, bool isFromPool = false)
  360. {
  361. Type type = type1;
  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});
  370. this.AddToComponent(type, component);
  371. return component ;
  372. }
  373. public K AddComponent<K, P1, P2>(P1 p1, P2 p2, 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});
  384. this.AddToComponent(type, component);
  385. return component as K;
  386. }
  387. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3, bool isFromPool = false) where K : Entity, new()
  388. {
  389. Type type = typeof(K);
  390. if (this._components != null && this._components.ContainsKey(type))
  391. {
  392. LogTool.Exception(new Exception($"entity already has component: {type.FullName}"));
  393. }
  394. Entity component = Create(type, isFromPool);
  395. component.ID = this.ID;
  396. component.ComponentParent = this;
  397. EventSystem.Instance.Awake(component, new object[] {p1, p2, p3});
  398. this.AddToComponent(type, component);
  399. return component as K;
  400. }
  401. #endregion
  402. #region 移除组件
  403. public void RemoveComponent<K>() where K : Entity
  404. {
  405. if (this._components == null)
  406. {
  407. return;
  408. }
  409. Type type = typeof(K);
  410. Entity c = this.GetComponent(type);
  411. if (c == null)
  412. {
  413. return;
  414. }
  415. this.RemoveFromComponent(type, c);
  416. c.Dispose();
  417. }
  418. public void RemoveComponent(Entity component)
  419. {
  420. if (this._components == null)
  421. {
  422. return;
  423. }
  424. Type type = component.GetType();
  425. Entity c = this.GetComponent(component.GetType());
  426. if (c == null)
  427. {
  428. return;
  429. }
  430. this.RemoveFromComponent(type, c);
  431. c.Dispose();
  432. }
  433. public void RemoveComponent(Type type)
  434. {
  435. Entity c = this.GetComponent(type);
  436. if (c == null)
  437. {
  438. return;
  439. }
  440. RemoveFromComponent(type, c);
  441. c.Dispose();
  442. }
  443. #endregion
  444. #region 添加组件为child
  445. public T AddChild<T>(bool isFromPool = false) where T : Entity
  446. {
  447. Type type = typeof(T);
  448. T component = (T) Entity.Create(type, isFromPool);
  449. component.ID = IdGenerater.GenerateId();
  450. component.Parent = this;
  451. EventSystem.Instance.Awake(component, null);
  452. return component;
  453. }
  454. public T AddChild<T, A>(A a, bool isFromPool = false) where T : Entity
  455. {
  456. Type type = typeof(T);
  457. T component = (T) Entity.Create(type, isFromPool);
  458. component.ID = IdGenerater.GenerateId();
  459. component.Parent = this;
  460. EventSystem.Instance.Awake(component, new object[] {a});
  461. return component;
  462. }
  463. public T AddChild<T, A, B>(A a, B b, bool isFromPool = false) where T : Entity
  464. {
  465. Type type = typeof(T);
  466. T component = (T) Entity.Create(type, isFromPool);
  467. component.ID = IdGenerater.GenerateId();
  468. component.Parent = this;
  469. EventSystem.Instance.Awake(component, new object[] {a, b});
  470. return component;
  471. }
  472. public T AddChild<T, A, B, C>(A a, B b, C c, bool isFromPool = false) where T : Entity
  473. {
  474. Type type = typeof(T);
  475. T component = (T) Entity.Create(type, isFromPool);
  476. component.ID = IdGenerater.GenerateId();
  477. component.Parent = this;
  478. EventSystem.Instance.Awake(component, new object[] {a, b, c});
  479. return component;
  480. }
  481. public T AddChild<T, A, B, C, D>(A a, B b, C c, D d, bool isFromPool = false) where T : Entity
  482. {
  483. Type type = typeof(T);
  484. T component = (T) Entity.Create(type, isFromPool);
  485. component.ID = IdGenerater.GenerateId();
  486. component.Parent = this;
  487. EventSystem.Instance.Awake(component, new object[] {a, b, c, d});
  488. return component;
  489. }
  490. 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
  491. {
  492. Type type = typeof(T);
  493. T component = (T) Entity.Create(type, isFromPool);
  494. component.ID = IdGenerater.GenerateId();
  495. component.Parent = this;
  496. EventSystem.Instance.Awake(component, new object[] {a, b, c, d, e});
  497. return component;
  498. }
  499. public T AddChildWithId<T>(long id, bool isFromPool = false) where T : Entity
  500. {
  501. Type type = typeof(T);
  502. T component = (T) Entity.Create(type, isFromPool);
  503. component.ID = id;
  504. component.Parent = this;
  505. EventSystem.Instance.Awake(component, null);
  506. return component;
  507. }
  508. public T AddChildWithId<T, A>(long id, A a, bool isFromPool = false) where T : Entity
  509. {
  510. Type type = typeof(T);
  511. T component = (T) Entity.Create(type, isFromPool);
  512. component.ID = id;
  513. component.Parent = this;
  514. EventSystem.Instance.Awake(component, new object[] {a});
  515. return component;
  516. }
  517. public T AddChildWithId<T, A, B>(long id, A a, B b, bool isFromPool = false) where T : Entity
  518. {
  519. Type type = typeof(T);
  520. T component = (T) Entity.Create(type, isFromPool);
  521. component.ID = id;
  522. component.Parent = this;
  523. EventSystem.Instance.Awake(component, new object[] {a, b});
  524. return component;
  525. }
  526. public T AddChildWithId<T, A, B, C>(long id, A a, B b, C c, bool isFromPool = false) where T : Entity
  527. {
  528. Type type = typeof(T);
  529. T component = (T) Entity.Create(type, isFromPool);
  530. component.ID = id;
  531. component.Parent = this;
  532. EventSystem.Instance.Awake(component, new object[] {a, b, c});
  533. return component;
  534. }
  535. #endregion
  536. #region 私有方法,增删组件和增删child
  537. private void AddChild(Entity entity)
  538. {
  539. this.Children.Add(entity.ID, entity);
  540. }
  541. private void RemoveChild(Entity entity)
  542. {
  543. if (this._children == null)
  544. {
  545. return;
  546. }
  547. this._children.Remove(entity.ID);
  548. if (this._children.Count == 0)
  549. {
  550. _childrenPool.Recycle(this._children);
  551. this._children = null;
  552. }
  553. }
  554. private void AddToComponent(Type type, Entity component)
  555. {
  556. if (this._components == null)
  557. {
  558. this._components = _dictPool.Fetch();
  559. }
  560. this._components.Add(type, component);
  561. }
  562. private void RemoveFromComponent(Type type, Entity component)
  563. {
  564. if (this._components == null)
  565. {
  566. return;
  567. }
  568. this._components.Remove(type);
  569. if (this._components.Count == 0 && this._isFromPool)
  570. {
  571. _dictPool.Recycle(this._components);
  572. this._components = null;
  573. }
  574. }
  575. #endregion
  576. }
  577. public class Pool<T> where T : class, new()
  578. {
  579. private readonly Queue<T> _pool = new Queue<T>();
  580. public T Fetch()
  581. {
  582. if (_pool.Count == 0)
  583. {
  584. return new T();
  585. }
  586. return _pool.Dequeue();
  587. }
  588. public void Recycle(T t)
  589. {
  590. _pool.Enqueue(t);
  591. }
  592. public void Clear()
  593. {
  594. this._pool.Clear();
  595. }
  596. }
  597. }