Entity.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. using System;
  2. using System.Collections.Generic;
  3. using MongoDB.Bson.Serialization.Attributes;
  4. namespace ET
  5. {
  6. [Flags]
  7. public enum EntityStatus: byte
  8. {
  9. None = 0,
  10. IsFromPool = 1,
  11. IsRegister = 1 << 1,
  12. IsComponent = 1 << 2,
  13. IsCreated = 1 << 3,
  14. IsNew = 1 << 4,
  15. }
  16. public partial class Entity: DisposeObject
  17. {
  18. #if ENABLE_VIEW && UNITY_EDITOR
  19. private UnityEngine.GameObject viewGO;
  20. #endif
  21. [BsonIgnore]
  22. public long InstanceId { get; protected set; }
  23. protected Entity()
  24. {
  25. }
  26. [BsonIgnore]
  27. private EntityStatus status = EntityStatus.None;
  28. [BsonIgnore]
  29. private bool IsFromPool
  30. {
  31. get => (this.status & EntityStatus.IsFromPool) == EntityStatus.IsFromPool;
  32. set
  33. {
  34. if (value)
  35. {
  36. this.status |= EntityStatus.IsFromPool;
  37. }
  38. else
  39. {
  40. this.status &= ~EntityStatus.IsFromPool;
  41. }
  42. }
  43. }
  44. [BsonIgnore]
  45. protected bool IsRegister
  46. {
  47. get => (this.status & EntityStatus.IsRegister) == EntityStatus.IsRegister;
  48. set
  49. {
  50. if (this.IsRegister == value)
  51. {
  52. return;
  53. }
  54. if (value)
  55. {
  56. this.status |= EntityStatus.IsRegister;
  57. }
  58. else
  59. {
  60. this.status &= ~EntityStatus.IsRegister;
  61. }
  62. if (!value)
  63. {
  64. Root.Instance.Remove(this.InstanceId);
  65. }
  66. else
  67. {
  68. Root.Instance.Add(this);
  69. EventSystem.Instance.RegisterSystem(this);
  70. }
  71. #if ENABLE_VIEW && UNITY_EDITOR
  72. if (value)
  73. {
  74. this.viewGO = new UnityEngine.GameObject(this.ViewName);
  75. this.viewGO.AddComponent<ComponentView>().Component = this;
  76. this.viewGO.transform.SetParent(this.Parent == null?
  77. UnityEngine.GameObject.Find("Global").transform : this.Parent.viewGO.transform);
  78. }
  79. else
  80. {
  81. UnityEngine.Object.Destroy(this.viewGO);
  82. }
  83. #endif
  84. }
  85. }
  86. protected virtual string ViewName
  87. {
  88. get
  89. {
  90. return this.GetType().Name;
  91. }
  92. }
  93. [BsonIgnore]
  94. private bool IsComponent
  95. {
  96. get => (this.status & EntityStatus.IsComponent) == EntityStatus.IsComponent;
  97. set
  98. {
  99. if (value)
  100. {
  101. this.status |= EntityStatus.IsComponent;
  102. }
  103. else
  104. {
  105. this.status &= ~EntityStatus.IsComponent;
  106. }
  107. }
  108. }
  109. [BsonIgnore]
  110. protected bool IsCreated
  111. {
  112. get => (this.status & EntityStatus.IsCreated) == EntityStatus.IsCreated;
  113. set
  114. {
  115. if (value)
  116. {
  117. this.status |= EntityStatus.IsCreated;
  118. }
  119. else
  120. {
  121. this.status &= ~EntityStatus.IsCreated;
  122. }
  123. }
  124. }
  125. [BsonIgnore]
  126. protected bool IsNew
  127. {
  128. get => (this.status & EntityStatus.IsNew) == EntityStatus.IsNew;
  129. set
  130. {
  131. if (value)
  132. {
  133. this.status |= EntityStatus.IsNew;
  134. }
  135. else
  136. {
  137. this.status &= ~EntityStatus.IsNew;
  138. }
  139. }
  140. }
  141. [BsonIgnore]
  142. public bool IsDisposed => this.InstanceId == 0;
  143. [BsonIgnore]
  144. protected Entity parent;
  145. // 可以改变parent,但是不能设置为null
  146. [BsonIgnore]
  147. public Entity Parent
  148. {
  149. get => this.parent;
  150. private set
  151. {
  152. if (value == null)
  153. {
  154. throw new Exception($"cant set parent null: {this.GetType().Name}");
  155. }
  156. if (value == this)
  157. {
  158. throw new Exception($"cant set parent self: {this.GetType().Name}");
  159. }
  160. // 严格限制parent必须要有domain,也就是说parent必须在数据树上面
  161. if (value.Domain == null)
  162. {
  163. throw new Exception($"cant set parent because parent domain is null: {this.GetType().Name} {value.GetType().Name}");
  164. }
  165. if (this.parent != null) // 之前有parent
  166. {
  167. // parent相同,不设置
  168. if (this.parent == value)
  169. {
  170. Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
  171. return;
  172. }
  173. this.parent.RemoveFromChildren(this);
  174. }
  175. this.parent = value;
  176. this.IsComponent = false;
  177. this.parent.AddToChildren(this);
  178. this.Domain = this.parent.domain;
  179. #if ENABLE_VIEW && UNITY_EDITOR
  180. this.viewGO.GetComponent<ComponentView>().Component = this;
  181. this.viewGO.transform.SetParent(this.Parent == null ?
  182. UnityEngine.GameObject.Find("Global").transform : this.Parent.viewGO.transform);
  183. foreach (var child in this.Children.Values)
  184. {
  185. child.viewGO.transform.SetParent(this.viewGO.transform);
  186. }
  187. foreach (var comp in this.Components.Values)
  188. {
  189. comp.viewGO.transform.SetParent(this.viewGO.transform);
  190. }
  191. #endif
  192. }
  193. }
  194. // 该方法只能在AddComponent中调用,其他人不允许调用
  195. [BsonIgnore]
  196. private Entity ComponentParent
  197. {
  198. set
  199. {
  200. if (value == null)
  201. {
  202. throw new Exception($"cant set parent null: {this.GetType().Name}");
  203. }
  204. if (value == this)
  205. {
  206. throw new Exception($"cant set parent self: {this.GetType().Name}");
  207. }
  208. // 严格限制parent必须要有domain,也就是说parent必须在数据树上面
  209. if (value.Domain == null)
  210. {
  211. throw new Exception($"cant set parent because parent domain is null: {this.GetType().Name} {value.GetType().Name}");
  212. }
  213. if (this.parent != null) // 之前有parent
  214. {
  215. // parent相同,不设置
  216. if (this.parent == value)
  217. {
  218. Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
  219. return;
  220. }
  221. this.parent.RemoveFromComponents(this);
  222. }
  223. this.parent = value;
  224. this.IsComponent = true;
  225. this.parent.AddToComponents(this);
  226. this.Domain = this.parent.domain;
  227. }
  228. }
  229. public T GetParent<T>() where T : Entity
  230. {
  231. return this.Parent as T;
  232. }
  233. [BsonIgnoreIfDefault]
  234. [BsonDefaultValue(0L)]
  235. [BsonElement]
  236. [BsonId]
  237. public long Id { get; set; }
  238. [BsonIgnore]
  239. protected Entity domain;
  240. [BsonIgnore]
  241. public Entity Domain
  242. {
  243. get
  244. {
  245. return this.domain;
  246. }
  247. private set
  248. {
  249. if (value == null)
  250. {
  251. throw new Exception($"domain cant set null: {this.GetType().Name}");
  252. }
  253. if (this.domain == value)
  254. {
  255. return;
  256. }
  257. Entity preDomain = this.domain;
  258. this.domain = value;
  259. if (preDomain == null)
  260. {
  261. this.InstanceId = IdGenerater.Instance.GenerateInstanceId();
  262. this.IsRegister = true;
  263. // 反序列化出来的需要设置父子关系
  264. if (this.componentsDB != null)
  265. {
  266. foreach (Entity component in this.componentsDB)
  267. {
  268. component.IsComponent = true;
  269. this.Components.Add(component.GetType().Name, component);
  270. component.parent = this;
  271. }
  272. }
  273. if (this.childrenDB != null)
  274. {
  275. foreach (Entity child in this.childrenDB)
  276. {
  277. child.IsComponent = false;
  278. this.Children.Add(child.Id, child);
  279. child.parent = this;
  280. }
  281. }
  282. }
  283. // 递归设置孩子的Domain
  284. if (this.children != null)
  285. {
  286. foreach (Entity entity in this.children.Values)
  287. {
  288. entity.Domain = this.domain;
  289. }
  290. }
  291. if (this.components != null)
  292. {
  293. foreach (Entity component in this.components.Values)
  294. {
  295. component.Domain = this.domain;
  296. }
  297. }
  298. if (!this.IsCreated)
  299. {
  300. this.IsCreated = true;
  301. EventSystem.Instance.Deserialize(this);
  302. }
  303. }
  304. }
  305. [BsonElement("Children")]
  306. [BsonIgnoreIfNull]
  307. private List<Entity> childrenDB;
  308. [BsonIgnore]
  309. private SortedDictionary<long, Entity> children;
  310. [BsonIgnore]
  311. public SortedDictionary<long, Entity> Children
  312. {
  313. get
  314. {
  315. return this.children ??= ObjectPool.Instance.Fetch<SortedDictionary<long, Entity>>();
  316. }
  317. }
  318. private void AddToChildren(Entity entity)
  319. {
  320. this.Children.Add(entity.Id, entity);
  321. }
  322. private void RemoveFromChildren(Entity entity)
  323. {
  324. if (this.children == null)
  325. {
  326. return;
  327. }
  328. this.children.Remove(entity.Id);
  329. if (this.children.Count == 0)
  330. {
  331. ObjectPool.Instance.Recycle(this.children);
  332. this.children = null;
  333. }
  334. }
  335. [BsonElement("C")]
  336. [BsonIgnoreIfNull]
  337. private List<Entity> componentsDB;
  338. [BsonIgnore]
  339. private SortedDictionary<string, Entity> components;
  340. [BsonIgnore]
  341. public SortedDictionary<string, Entity> Components
  342. {
  343. get
  344. {
  345. return this.components ??= ObjectPool.Instance.Fetch<SortedDictionary<string, Entity>>();
  346. }
  347. }
  348. public override void Dispose()
  349. {
  350. if (this.IsDisposed)
  351. {
  352. return;
  353. }
  354. this.IsRegister = false;
  355. this.InstanceId = 0;
  356. // 清理Children
  357. if (this.children != null)
  358. {
  359. foreach (Entity child in this.children.Values)
  360. {
  361. child.Dispose();
  362. }
  363. this.children.Clear();
  364. ObjectPool.Instance.Recycle(this.children);
  365. this.children = null;
  366. if (this.childrenDB != null)
  367. {
  368. this.childrenDB.Clear();
  369. // 创建的才需要回到池中,从db中不需要回收
  370. if (this.IsNew)
  371. {
  372. ObjectPool.Instance.Recycle(this.childrenDB);
  373. this.childrenDB = null;
  374. }
  375. }
  376. }
  377. // 清理Component
  378. if (this.components != null)
  379. {
  380. foreach (KeyValuePair<string, Entity> kv in this.components)
  381. {
  382. kv.Value.Dispose();
  383. }
  384. this.components.Clear();
  385. ObjectPool.Instance.Recycle(this.components);
  386. this.components = null;
  387. // 创建的才需要回到池中,从db中不需要回收
  388. if (this.componentsDB != null)
  389. {
  390. this.componentsDB.Clear();
  391. if (this.IsNew)
  392. {
  393. ObjectPool.Instance.Recycle(this.componentsDB);
  394. this.componentsDB = null;
  395. }
  396. }
  397. }
  398. // 触发Destroy事件
  399. if (this is IDestroy)
  400. {
  401. EventSystem.Instance.Destroy(this);
  402. }
  403. this.domain = null;
  404. if (this.parent != null && !this.parent.IsDisposed)
  405. {
  406. if (this.IsComponent)
  407. {
  408. this.parent.RemoveComponent(this);
  409. }
  410. else
  411. {
  412. this.parent.RemoveFromChildren(this);
  413. }
  414. }
  415. this.parent = null;
  416. base.Dispose();
  417. if (this.IsFromPool)
  418. {
  419. ObjectPool.Instance.Recycle(this);
  420. }
  421. status = EntityStatus.None;
  422. }
  423. private void AddToComponents(Entity component)
  424. {
  425. this.Components.Add(component.GetType().Name, component);
  426. }
  427. private void RemoveFromComponents(Entity component)
  428. {
  429. if (this.components == null)
  430. {
  431. return;
  432. }
  433. this.components.Remove(component.GetType().Name);
  434. if (this.components.Count == 0)
  435. {
  436. ObjectPool.Instance.Recycle(this.components);
  437. this.components = null;
  438. }
  439. }
  440. public K GetChild<K>(long id) where K : Entity
  441. {
  442. if (this.children == null)
  443. {
  444. return null;
  445. }
  446. this.children.TryGetValue(id, out Entity child);
  447. return child as K;
  448. }
  449. public void RemoveChild(long id)
  450. {
  451. if (this.children == null)
  452. {
  453. return;
  454. }
  455. if (!this.children.TryGetValue(id, out Entity child))
  456. {
  457. return;
  458. }
  459. this.children.Remove(id);
  460. child.Dispose();
  461. }
  462. public void RemoveComponent<K>() where K : Entity
  463. {
  464. if (this.IsDisposed)
  465. {
  466. return;
  467. }
  468. if (this.components == null)
  469. {
  470. return;
  471. }
  472. Type type = typeof (K);
  473. Entity c = this.GetComponent(type);
  474. if (c == null)
  475. {
  476. return;
  477. }
  478. this.RemoveFromComponents(c);
  479. c.Dispose();
  480. }
  481. public void RemoveComponent(Entity component)
  482. {
  483. if (this.IsDisposed)
  484. {
  485. return;
  486. }
  487. if (this.components == null)
  488. {
  489. return;
  490. }
  491. Entity c = this.GetComponent(component.GetType());
  492. if (c == null)
  493. {
  494. return;
  495. }
  496. if (c.InstanceId != component.InstanceId)
  497. {
  498. return;
  499. }
  500. this.RemoveFromComponents(c);
  501. c.Dispose();
  502. }
  503. public void RemoveComponent(Type type)
  504. {
  505. if (this.IsDisposed)
  506. {
  507. return;
  508. }
  509. Entity c = this.GetComponent(type);
  510. if (c == null)
  511. {
  512. return;
  513. }
  514. RemoveFromComponents(c);
  515. c.Dispose();
  516. }
  517. public K GetComponent<K>() where K : Entity
  518. {
  519. if (this.components == null)
  520. {
  521. return null;
  522. }
  523. Entity component;
  524. if (!this.components.TryGetValue(typeof (K).Name, out component))
  525. {
  526. return default;
  527. }
  528. // 如果有IGetComponent接口,则触发GetComponentSystem
  529. if (this is IGetComponent)
  530. {
  531. EventSystem.Instance.GetComponent(this, component);
  532. }
  533. return (K) component;
  534. }
  535. public Entity GetComponent(Type type)
  536. {
  537. if (this.components == null)
  538. {
  539. return null;
  540. }
  541. Entity component;
  542. if (!this.components.TryGetValue(type.Name, out component))
  543. {
  544. return null;
  545. }
  546. // 如果有IGetComponent接口,则触发GetComponentSystem
  547. if (this is IGetComponent)
  548. {
  549. EventSystem.Instance.GetComponent(this, component);
  550. }
  551. return component;
  552. }
  553. private static Entity Create(Type type, bool isFromPool)
  554. {
  555. Entity component;
  556. if (isFromPool)
  557. {
  558. component = (Entity) ObjectPool.Instance.Fetch(type);
  559. }
  560. else
  561. {
  562. component = Activator.CreateInstance(type) as Entity;
  563. }
  564. component.IsFromPool = isFromPool;
  565. component.IsCreated = true;
  566. component.IsNew = true;
  567. component.Id = 0;
  568. return component;
  569. }
  570. public Entity AddComponent(Entity component)
  571. {
  572. Type type = component.GetType();
  573. if (this.components != null && this.components.ContainsKey(type.Name))
  574. {
  575. throw new Exception($"entity already has component: {type.FullName}");
  576. }
  577. component.ComponentParent = this;
  578. if (this is IAddComponent)
  579. {
  580. EventSystem.Instance.AddComponent(this, component);
  581. }
  582. return component;
  583. }
  584. public Entity AddComponent(Type type, bool isFromPool = false)
  585. {
  586. if (this.components != null && this.components.ContainsKey(type.Name))
  587. {
  588. throw new Exception($"entity already has component: {type.FullName}");
  589. }
  590. Entity component = Create(type, isFromPool);
  591. component.Id = this.Id;
  592. component.ComponentParent = this;
  593. EventSystem.Instance.Awake(component);
  594. if (this is IAddComponent)
  595. {
  596. EventSystem.Instance.AddComponent(this, component);
  597. }
  598. return component;
  599. }
  600. public K AddComponentWithId<K>(long id, bool isFromPool = false) where K : Entity, IAwake, new()
  601. {
  602. Type type = typeof (K);
  603. if (this.components != null && this.components.ContainsKey(type.Name))
  604. {
  605. throw new Exception($"entity already has component: {type.FullName}");
  606. }
  607. Entity component = Create(type, isFromPool);
  608. component.Id = id;
  609. component.ComponentParent = this;
  610. EventSystem.Instance.Awake(component);
  611. if (this is IAddComponent)
  612. {
  613. EventSystem.Instance.AddComponent(this, component);
  614. }
  615. return component as K;
  616. }
  617. public K AddComponentWithId<K, P1>(long id, P1 p1, bool isFromPool = false) where K : Entity, IAwake<P1>, new()
  618. {
  619. Type type = typeof (K);
  620. if (this.components != null && this.components.ContainsKey(type.Name))
  621. {
  622. throw new Exception($"entity already has component: {type.FullName}");
  623. }
  624. Entity component = Create(type, isFromPool);
  625. component.Id = id;
  626. component.ComponentParent = this;
  627. EventSystem.Instance.Awake(component, p1);
  628. if (this is IAddComponent)
  629. {
  630. EventSystem.Instance.AddComponent(this, component);
  631. }
  632. return component as K;
  633. }
  634. public K AddComponentWithId<K, P1, P2>(long id, P1 p1, P2 p2, bool isFromPool = false) where K : Entity, IAwake<P1, P2>, new()
  635. {
  636. Type type = typeof (K);
  637. if (this.components != null && this.components.ContainsKey(type.Name))
  638. {
  639. throw new Exception($"entity already has component: {type.FullName}");
  640. }
  641. Entity component = Create(type, isFromPool);
  642. component.Id = id;
  643. component.ComponentParent = this;
  644. EventSystem.Instance.Awake(component, p1, p2);
  645. if (this is IAddComponent)
  646. {
  647. EventSystem.Instance.AddComponent(this, component);
  648. }
  649. return component as K;
  650. }
  651. public K AddComponentWithId<K, P1, P2, P3>(long id, P1 p1, P2 p2, P3 p3, bool isFromPool = false) where K : Entity, IAwake<P1, P2, P3>, new()
  652. {
  653. Type type = typeof (K);
  654. if (this.components != null && this.components.ContainsKey(type.Name))
  655. {
  656. throw new Exception($"entity already has component: {type.FullName}");
  657. }
  658. Entity component = Create(type, isFromPool);
  659. component.Id = id;
  660. component.ComponentParent = this;
  661. EventSystem.Instance.Awake(component, p1, p2, p3);
  662. if (this is IAddComponent)
  663. {
  664. EventSystem.Instance.AddComponent(this, component);
  665. }
  666. return component as K;
  667. }
  668. public K AddComponent<K>(bool isFromPool = false) where K : Entity, IAwake, new()
  669. {
  670. return this.AddComponentWithId<K>(this.Id, isFromPool);
  671. }
  672. public K AddComponent<K, P1>(P1 p1, bool isFromPool = false) where K : Entity, IAwake<P1>, new()
  673. {
  674. return this.AddComponentWithId<K, P1>(this.Id, p1, isFromPool);
  675. }
  676. public K AddComponent<K, P1, P2>(P1 p1, P2 p2, bool isFromPool = false) where K : Entity, IAwake<P1, P2>, new()
  677. {
  678. return this.AddComponentWithId<K, P1, P2>(this.Id, p1, p2, isFromPool);
  679. }
  680. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3, bool isFromPool = false) where K : Entity, IAwake<P1, P2, P3>, new()
  681. {
  682. return this.AddComponentWithId<K, P1, P2, P3>(this.Id, p1, p2, p3, isFromPool);
  683. }
  684. public Entity AddChild(Entity entity)
  685. {
  686. entity.Parent = this;
  687. return entity;
  688. }
  689. public T AddChild<T>(bool isFromPool = false) where T : Entity, IAwake
  690. {
  691. Type type = typeof (T);
  692. T component = (T) Entity.Create(type, isFromPool);
  693. component.Id = IdGenerater.Instance.GenerateId();
  694. component.Parent = this;
  695. EventSystem.Instance.Awake(component);
  696. return component;
  697. }
  698. public T AddChild<T, A>(A a, bool isFromPool = false) where T : Entity, IAwake<A>
  699. {
  700. Type type = typeof (T);
  701. T component = (T) Entity.Create(type, isFromPool);
  702. component.Id = IdGenerater.Instance.GenerateId();
  703. component.Parent = this;
  704. EventSystem.Instance.Awake(component, a);
  705. return component;
  706. }
  707. public T AddChild<T, A, B>(A a, B b, bool isFromPool = false) where T : Entity, IAwake<A, B>
  708. {
  709. Type type = typeof (T);
  710. T component = (T) Entity.Create(type, isFromPool);
  711. component.Id = IdGenerater.Instance.GenerateId();
  712. component.Parent = this;
  713. EventSystem.Instance.Awake(component, a, b);
  714. return component;
  715. }
  716. public T AddChild<T, A, B, C>(A a, B b, C c, bool isFromPool = false) where T : Entity, IAwake<A, B, C>
  717. {
  718. Type type = typeof (T);
  719. T component = (T) Entity.Create(type, isFromPool);
  720. component.Id = IdGenerater.Instance.GenerateId();
  721. component.Parent = this;
  722. EventSystem.Instance.Awake(component, a, b, c);
  723. return component;
  724. }
  725. public T AddChild<T, A, B, C, D>(A a, B b, C c, D d, bool isFromPool = false) where T : Entity, IAwake<A, B, C, D>
  726. {
  727. Type type = typeof (T);
  728. T component = (T) Entity.Create(type, isFromPool);
  729. component.Id = IdGenerater.Instance.GenerateId();
  730. component.Parent = this;
  731. EventSystem.Instance.Awake(component, a, b, c, d);
  732. return component;
  733. }
  734. public T AddChildWithId<T>(long id, bool isFromPool = false) where T : Entity, IAwake, new()
  735. {
  736. Type type = typeof (T);
  737. T component = Entity.Create(type, isFromPool) as T;
  738. component.Id = id;
  739. component.Parent = this;
  740. EventSystem.Instance.Awake(component);
  741. return component;
  742. }
  743. public T AddChildWithId<T, A>(long id, A a, bool isFromPool = false) where T : Entity, IAwake<A>
  744. {
  745. Type type = typeof (T);
  746. T component = (T) Entity.Create(type, isFromPool);
  747. component.Id = id;
  748. component.Parent = this;
  749. EventSystem.Instance.Awake(component, a);
  750. return component;
  751. }
  752. public T AddChildWithId<T, A, B>(long id, A a, B b, bool isFromPool = false) where T : Entity, IAwake<A, B>
  753. {
  754. Type type = typeof (T);
  755. T component = (T) Entity.Create(type, isFromPool);
  756. component.Id = id;
  757. component.Parent = this;
  758. EventSystem.Instance.Awake(component, a, b);
  759. return component;
  760. }
  761. public T AddChildWithId<T, A, B, C>(long id, A a, B b, C c, bool isFromPool = false) where T : Entity, IAwake<A, B, C>
  762. {
  763. Type type = typeof (T);
  764. T component = (T) Entity.Create(type, isFromPool);
  765. component.Id = id;
  766. component.Parent = this;
  767. EventSystem.Instance.Awake(component, a, b, c);
  768. return component;
  769. }
  770. public override void BeginInit()
  771. {
  772. this.componentsDB?.Clear();
  773. if (this.components != null && this.components.Count != 0)
  774. {
  775. foreach (Entity entity in this.components.Values)
  776. {
  777. if (entity is not ISerializeToEntity)
  778. {
  779. continue;
  780. }
  781. this.componentsDB ??= ObjectPool.Instance.Fetch<List<Entity>>();
  782. this.componentsDB.Add(entity);
  783. entity.BeginInit();
  784. }
  785. }
  786. this.childrenDB?.Clear();
  787. if (this.children != null && this.children.Count != 0)
  788. {
  789. foreach (Entity entity in this.children.Values)
  790. {
  791. if (entity is not ISerializeToEntity)
  792. {
  793. continue;
  794. }
  795. this.childrenDB ??= ObjectPool.Instance.Fetch<List<Entity>>();
  796. this.childrenDB.Add(entity);
  797. entity.BeginInit();
  798. }
  799. }
  800. }
  801. }
  802. }