Entity.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. using System;
  2. using System.Collections.Generic;
  3. using MongoDB.Bson;
  4. using MongoDB.Bson.Serialization.Attributes;
  5. #if !SERVER
  6. using UnityEngine;
  7. #endif
  8. namespace ET
  9. {
  10. [Flags]
  11. public enum EntityStatus: byte
  12. {
  13. None = 0,
  14. IsFromPool = 1,
  15. IsRegister = 1 << 1,
  16. IsComponent = 1 << 2,
  17. IsCreate = 1 << 3,
  18. }
  19. public partial class Entity : Object
  20. {
  21. private static readonly Pool<HashSet<Entity>> hashSetPool = new Pool<HashSet<Entity>>();
  22. private static readonly Pool<Dictionary<Type, Entity>> dictPool = new Pool<Dictionary<Type, Entity>>();
  23. private static readonly Pool<Dictionary<long, Entity>> childrenPool = new Pool<Dictionary<long, Entity>>();
  24. [BsonIgnore]
  25. public long InstanceId { get; set; }
  26. protected Entity()
  27. {
  28. }
  29. [BsonIgnore]
  30. private EntityStatus status = EntityStatus.None;
  31. [BsonIgnore]
  32. public bool IsFromPool
  33. {
  34. get
  35. {
  36. return (this.status & EntityStatus.IsFromPool) == EntityStatus.IsFromPool;
  37. }
  38. set
  39. {
  40. if (value)
  41. {
  42. this.status |= EntityStatus.IsFromPool;
  43. }
  44. else
  45. {
  46. this.status &= ~EntityStatus.IsFromPool;
  47. }
  48. }
  49. }
  50. [BsonIgnore]
  51. public bool IsRegister
  52. {
  53. get
  54. {
  55. return (this.status & EntityStatus.IsRegister) == EntityStatus.IsRegister;
  56. }
  57. set
  58. {
  59. if (this.IsRegister == value)
  60. {
  61. return;
  62. }
  63. if (value)
  64. {
  65. this.status |= EntityStatus.IsRegister;
  66. }
  67. else
  68. {
  69. this.status &= ~EntityStatus.IsRegister;
  70. }
  71. EventSystem.Instance.RegisterSystem(this, value);
  72. }
  73. }
  74. [BsonIgnore]
  75. private bool IsComponent
  76. {
  77. get
  78. {
  79. return (this.status & EntityStatus.IsComponent) == EntityStatus.IsComponent;
  80. }
  81. set
  82. {
  83. if (value)
  84. {
  85. this.status |= EntityStatus.IsComponent;
  86. }
  87. else
  88. {
  89. this.status &= ~EntityStatus.IsComponent;
  90. }
  91. }
  92. }
  93. [BsonIgnore]
  94. public bool IsCreate
  95. {
  96. get
  97. {
  98. return (this.status & EntityStatus.IsCreate) == EntityStatus.IsCreate;
  99. }
  100. set
  101. {
  102. if (value)
  103. {
  104. this.status |= EntityStatus.IsCreate;
  105. }
  106. else
  107. {
  108. this.status &= ~EntityStatus.IsCreate;
  109. }
  110. }
  111. }
  112. [BsonIgnore]
  113. public bool IsDisposed
  114. {
  115. get
  116. {
  117. return this.InstanceId == 0;
  118. }
  119. }
  120. [BsonIgnore]
  121. protected Entity parent;
  122. [BsonIgnore]
  123. public Entity Parent
  124. {
  125. get
  126. {
  127. return this.parent;
  128. }
  129. set
  130. {
  131. if (value == null)
  132. {
  133. throw new Exception($"cant set parent null: {this.GetType().Name}");
  134. }
  135. if (this.parent != null) // 之前有parent
  136. {
  137. // parent相同,不设置
  138. if (this.parent.InstanceId == value.InstanceId)
  139. {
  140. Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
  141. return;
  142. }
  143. this.parent.RemoveChild(this);
  144. this.parent = value;
  145. this.parent.AddChild(this);
  146. this.Domain = this.parent.domain;
  147. }
  148. else
  149. {
  150. this.parent = value;
  151. this.parent.AddChild(this);
  152. this.IsComponent = false;
  153. AfterSetParent();
  154. }
  155. }
  156. }
  157. // 该方法只能在AddComponent中调用,其他人不允许调用
  158. [BsonIgnore]
  159. private Entity ComponentParent
  160. {
  161. set
  162. {
  163. if (this.parent != null)
  164. {
  165. throw new Exception($"Component parent is not null: {this.GetType().Name}");
  166. }
  167. this.parent = value;
  168. this.IsComponent = true;
  169. AfterSetParent();
  170. }
  171. }
  172. private void AfterSetParent()
  173. {
  174. this.Domain = this.parent.domain;
  175. #if UNITY_EDITOR
  176. if (this.ViewGO != null && this.parent.ViewGO != null)
  177. {
  178. this.ViewGO.transform.SetParent(this.parent.ViewGO.transform, false);
  179. }
  180. #endif
  181. }
  182. public T GetParent<T>() where T : Entity
  183. {
  184. return this.Parent as T;
  185. }
  186. public override string ToString()
  187. {
  188. return this.ToJson();
  189. }
  190. [BsonIgnoreIfDefault]
  191. [BsonDefaultValue(0L)]
  192. [BsonElement]
  193. [BsonId]
  194. public long Id { get; set; }
  195. [BsonIgnore]
  196. protected Entity domain;
  197. [BsonIgnore]
  198. public Entity Domain
  199. {
  200. get
  201. {
  202. return this.domain;
  203. }
  204. set
  205. {
  206. if (value == null)
  207. {
  208. return;
  209. }
  210. Entity preDomain = this.domain;
  211. this.domain = value;
  212. //if (!(this.domain is Scene))
  213. //{
  214. // throw new Exception($"domain is not scene: {this.GetType().Name}");
  215. //}
  216. if (preDomain == null)
  217. {
  218. this.InstanceId = IdGenerater.GenerateInstanceId();
  219. // 反序列化出来的需要设置父子关系
  220. if (!this.IsCreate)
  221. {
  222. if (this.componentsDB != null)
  223. {
  224. foreach (Entity component in this.componentsDB)
  225. {
  226. component.IsComponent = true;
  227. this.Components.Add(component.GetType(), component);
  228. component.parent = this;
  229. }
  230. }
  231. if (this.childrenDB != null)
  232. {
  233. foreach (Entity child in this.childrenDB)
  234. {
  235. child.IsComponent = false;
  236. this.Children.Add(child.Id, child);
  237. child.parent = this;
  238. }
  239. }
  240. }
  241. }
  242. // 是否注册跟parent一致
  243. if (this.parent != null)
  244. {
  245. this.IsRegister = this.Parent.IsRegister;
  246. }
  247. // 递归设置孩子的Domain
  248. if (this.children != null)
  249. {
  250. foreach (Entity entity in this.children.Values)
  251. {
  252. entity.Domain = this.domain;
  253. }
  254. }
  255. if (this.components != null)
  256. {
  257. foreach (Entity component in this.components.Values)
  258. {
  259. component.Domain = this.domain;
  260. }
  261. }
  262. if (preDomain == null && !this.IsCreate)
  263. {
  264. EventSystem.Instance.Deserialize(this);
  265. }
  266. }
  267. }
  268. [BsonElement("Children")]
  269. [BsonIgnoreIfNull]
  270. protected HashSet<Entity> childrenDB;
  271. [BsonIgnore]
  272. protected Dictionary<long, Entity> children;
  273. [BsonIgnore]
  274. public Dictionary<long, Entity> Children
  275. {
  276. get
  277. {
  278. return this.children ?? (this.children = childrenPool.Fetch());
  279. }
  280. }
  281. public void AddChild(Entity entity)
  282. {
  283. this.Children.Add(entity.Id, entity);
  284. this.AddChildDB(entity);
  285. }
  286. public void RemoveChild(Entity entity)
  287. {
  288. if (this.children == null)
  289. {
  290. return;
  291. }
  292. this.children.Remove(entity.Id);
  293. if (this.children.Count == 0)
  294. {
  295. childrenPool.Recycle(this.children);
  296. this.children = null;
  297. }
  298. this.RemoveChildDB(entity);
  299. }
  300. private void AddChildDB(Entity entity)
  301. {
  302. if (!(entity is ISerializeToEntity))
  303. {
  304. return;
  305. }
  306. if (this.childrenDB == null)
  307. {
  308. this.childrenDB = hashSetPool.Fetch();
  309. }
  310. this.childrenDB.Add(entity);
  311. }
  312. public void RemoveChildDB(Entity entity)
  313. {
  314. if (!(entity is ISerializeToEntity))
  315. {
  316. return;
  317. }
  318. if (this.childrenDB == null)
  319. {
  320. return;
  321. }
  322. this.childrenDB.Remove(entity);
  323. if (this.childrenDB.Count == 0)
  324. {
  325. if (this.IsFromPool)
  326. {
  327. hashSetPool.Recycle(this.childrenDB);
  328. this.childrenDB = null;
  329. }
  330. }
  331. }
  332. public void RemoveAllChild()
  333. {
  334. this.children.Clear();
  335. this.childrenDB.Clear();
  336. }
  337. [BsonElement("C")]
  338. [BsonIgnoreIfNull]
  339. private HashSet<Entity> componentsDB;
  340. [BsonIgnore]
  341. private Dictionary<Type, Entity> components;
  342. [BsonIgnore]
  343. public Dictionary<Type, Entity> Components
  344. {
  345. get
  346. {
  347. return this.components ?? (this.components = dictPool.Fetch());
  348. }
  349. }
  350. public override void Dispose()
  351. {
  352. if (this.IsDisposed)
  353. {
  354. return;
  355. }
  356. EventSystem.Instance.Remove(this.InstanceId);
  357. this.InstanceId = 0;
  358. // 清理Component
  359. if (this.components != null)
  360. {
  361. foreach (var kv in this.components)
  362. {
  363. kv.Value.Dispose();
  364. }
  365. this.components.Clear();
  366. dictPool.Recycle(this.components);
  367. this.components = null;
  368. // 从池中创建的才需要回到池中,从db中不需要回收
  369. if (this.componentsDB != null)
  370. {
  371. this.componentsDB.Clear();
  372. if (this.IsFromPool)
  373. {
  374. hashSetPool.Recycle(this.componentsDB);
  375. this.componentsDB = null;
  376. }
  377. }
  378. }
  379. // 清理Children
  380. if (this.children != null)
  381. {
  382. foreach (Entity child in this.children.Values)
  383. {
  384. child.Dispose();
  385. }
  386. this.children.Clear();
  387. childrenPool.Recycle(this.children);
  388. this.children = null;
  389. if (this.childrenDB != null)
  390. {
  391. this.childrenDB.Clear();
  392. // 从池中创建的才需要回到池中,从db中不需要回收
  393. if (this.IsFromPool)
  394. {
  395. hashSetPool.Recycle(this.childrenDB);
  396. this.childrenDB = null;
  397. }
  398. }
  399. }
  400. // 触发Destroy事件
  401. EventSystem.Instance.Destroy(this);
  402. this.domain = null;
  403. if (this.parent != null && !this.parent.IsDisposed)
  404. {
  405. if (this.IsComponent)
  406. {
  407. this.parent.RemoveComponent(this);
  408. }
  409. else
  410. {
  411. this.parent.RemoveChild(this);
  412. }
  413. }
  414. this.parent = null;
  415. if (this.IsFromPool)
  416. {
  417. ObjectPool.Instance.Recycle(this);
  418. }
  419. else
  420. {
  421. base.Dispose();
  422. }
  423. status = EntityStatus.None;
  424. }
  425. private void AddToComponentsDB(Entity component)
  426. {
  427. if (this.componentsDB == null)
  428. {
  429. this.componentsDB = hashSetPool.Fetch();
  430. }
  431. this.componentsDB.Add(component);
  432. }
  433. private void RemoveFromComponentsDB(Entity component)
  434. {
  435. if (this.componentsDB == null)
  436. {
  437. return;
  438. }
  439. this.componentsDB.Remove(component);
  440. if (this.componentsDB.Count == 0 && this.IsFromPool)
  441. {
  442. hashSetPool.Recycle(this.componentsDB);
  443. this.componentsDB = null;
  444. }
  445. }
  446. private void AddToComponent(Type type, Entity component)
  447. {
  448. if (this.components == null)
  449. {
  450. this.components = dictPool.Fetch();
  451. }
  452. this.components.Add(type, component);
  453. if (component is ISerializeToEntity)
  454. {
  455. this.AddToComponentsDB(component);
  456. }
  457. }
  458. private void RemoveFromComponent(Type type, Entity component)
  459. {
  460. if (this.components == null)
  461. {
  462. return;
  463. }
  464. this.components.Remove(type);
  465. if (this.components.Count == 0 && this.IsFromPool)
  466. {
  467. dictPool.Recycle(this.components);
  468. this.components = null;
  469. }
  470. this.RemoveFromComponentsDB(component);
  471. }
  472. public Entity AddComponent(Entity component)
  473. {
  474. component.ComponentParent = this;
  475. Type type = component.GetType();
  476. this.AddToComponent(type, component);
  477. return component;
  478. }
  479. public Entity AddComponent(Type type)
  480. {
  481. Entity component = CreateWithComponentParent(type);
  482. this.AddToComponent(type, component);
  483. return component;
  484. }
  485. public K AddComponent<K>() where K : Entity, new()
  486. {
  487. Type type = typeof (K);
  488. K component = CreateWithComponentParent<K>();
  489. this.AddToComponent(type, component);
  490. return component;
  491. }
  492. public K AddComponent<K, P1>(P1 p1) where K : Entity, new()
  493. {
  494. Type type = typeof (K);
  495. K component = CreateWithComponentParent<K, P1>(p1);
  496. this.AddToComponent(type, component);
  497. return component;
  498. }
  499. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Entity, new()
  500. {
  501. Type type = typeof (K);
  502. K component = CreateWithComponentParent<K, P1, P2>(p1, p2);
  503. this.AddToComponent(type, component);
  504. return component;
  505. }
  506. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Entity, new()
  507. {
  508. Type type = typeof (K);
  509. K component = CreateWithComponentParent<K, P1, P2, P3>(p1, p2, p3);
  510. this.AddToComponent(type, component);
  511. return component;
  512. }
  513. public K AddComponentNoPool<K>() where K : Entity, new()
  514. {
  515. Type type = typeof (K);
  516. K component = CreateWithComponentParent<K>(false);
  517. this.AddToComponent(type, component);
  518. return component;
  519. }
  520. public K AddComponentNoPool<K, P1>(P1 p1) where K : Entity, new()
  521. {
  522. Type type = typeof (K);
  523. K component = CreateWithComponentParent<K, P1>(p1, false);
  524. this.AddToComponent(type, component);
  525. return component;
  526. }
  527. public K AddComponentNoPool<K, P1, P2>(P1 p1, P2 p2) where K : Entity, new()
  528. {
  529. Type type = typeof (K);
  530. K component = CreateWithComponentParent<K, P1, P2>(p1, p2, false);
  531. this.AddToComponent(type, component);
  532. return component;
  533. }
  534. public K AddComponentNoPool<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Entity, new()
  535. {
  536. Type type = typeof (K);
  537. K component = CreateWithComponentParent<K, P1, P2, P3>(p1, p2, p3, false);
  538. this.AddToComponent(type, component);
  539. return component;
  540. }
  541. public void RemoveComponent<K>() where K : Entity
  542. {
  543. if (this.IsDisposed)
  544. {
  545. return;
  546. }
  547. if (this.components == null)
  548. {
  549. return;
  550. }
  551. Type type = typeof (K);
  552. Entity c = this.GetComponent(type);
  553. if (c == null)
  554. {
  555. return;
  556. }
  557. this.RemoveFromComponent(type, c);
  558. c.Dispose();
  559. }
  560. public void RemoveComponent(Entity component)
  561. {
  562. if (this.IsDisposed)
  563. {
  564. return;
  565. }
  566. if (this.components == null)
  567. {
  568. return;
  569. }
  570. Type type = component.GetType();
  571. Entity c = this.GetComponent(component.GetType());
  572. if (c == null)
  573. {
  574. return;
  575. }
  576. if (c.InstanceId != component.InstanceId)
  577. {
  578. return;
  579. }
  580. this.RemoveFromComponent(type, c);
  581. c.Dispose();
  582. }
  583. public void RemoveComponent(Type type)
  584. {
  585. if (this.IsDisposed)
  586. {
  587. return;
  588. }
  589. Entity c = this.GetComponent(type);
  590. if (c == null)
  591. {
  592. return;
  593. }
  594. RemoveFromComponent(type, c);
  595. c.Dispose();
  596. }
  597. public K GetComponent<K>() where K : Entity
  598. {
  599. if (this.components == null)
  600. {
  601. return null;
  602. }
  603. Entity component;
  604. if (!this.components.TryGetValue(typeof(K), out component))
  605. {
  606. return default(K);
  607. }
  608. return (K)component;
  609. }
  610. public Entity GetComponent(Type type)
  611. {
  612. if (this.components == null)
  613. {
  614. return null;
  615. }
  616. Entity component;
  617. if (!this.components.TryGetValue(type, out component))
  618. {
  619. return null;
  620. }
  621. return component;
  622. }
  623. }
  624. }