EntityManager.EntityInfo.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System.Collections.Generic;
  8. namespace GameFramework.Entity
  9. {
  10. internal sealed partial class EntityManager : GameFrameworkModule, IEntityManager
  11. {
  12. /// <summary>
  13. /// 实体信息。
  14. /// </summary>
  15. private sealed class EntityInfo : IReference
  16. {
  17. private IEntity m_Entity;
  18. private EntityStatus m_Status;
  19. private IEntity m_ParentEntity;
  20. private List<IEntity> m_ChildEntities;
  21. public EntityInfo()
  22. {
  23. m_Entity = null;
  24. m_Status = EntityStatus.Unknown;
  25. m_ParentEntity = null;
  26. m_ChildEntities = new List<IEntity>();
  27. }
  28. public IEntity Entity
  29. {
  30. get
  31. {
  32. return m_Entity;
  33. }
  34. }
  35. public EntityStatus Status
  36. {
  37. get
  38. {
  39. return m_Status;
  40. }
  41. set
  42. {
  43. m_Status = value;
  44. }
  45. }
  46. public IEntity ParentEntity
  47. {
  48. get
  49. {
  50. return m_ParentEntity;
  51. }
  52. set
  53. {
  54. m_ParentEntity = value;
  55. }
  56. }
  57. public int ChildEntityCount
  58. {
  59. get
  60. {
  61. return m_ChildEntities.Count;
  62. }
  63. }
  64. public static EntityInfo Create(IEntity entity)
  65. {
  66. if (entity == null)
  67. {
  68. throw new GameFrameworkException("Entity is invalid.");
  69. }
  70. EntityInfo entityInfo = ReferencePool.Acquire<EntityInfo>();
  71. entityInfo.m_Entity = entity;
  72. entityInfo.m_Status = EntityStatus.WillInit;
  73. return entityInfo;
  74. }
  75. public void Clear()
  76. {
  77. m_Entity = null;
  78. m_Status = EntityStatus.Unknown;
  79. m_ParentEntity = null;
  80. m_ChildEntities.Clear();
  81. }
  82. public IEntity GetChildEntity()
  83. {
  84. return m_ChildEntities.Count > 0 ? m_ChildEntities[0] : null;
  85. }
  86. public IEntity[] GetChildEntities()
  87. {
  88. return m_ChildEntities.ToArray();
  89. }
  90. public void GetChildEntities(List<IEntity> results)
  91. {
  92. if (results == null)
  93. {
  94. throw new GameFrameworkException("Results is invalid.");
  95. }
  96. results.Clear();
  97. foreach (IEntity childEntity in m_ChildEntities)
  98. {
  99. results.Add(childEntity);
  100. }
  101. }
  102. public void AddChildEntity(IEntity childEntity)
  103. {
  104. if (m_ChildEntities.Contains(childEntity))
  105. {
  106. throw new GameFrameworkException("Can not add child entity which is already exist.");
  107. }
  108. m_ChildEntities.Add(childEntity);
  109. }
  110. public void RemoveChildEntity(IEntity childEntity)
  111. {
  112. if (!m_ChildEntities.Remove(childEntity))
  113. {
  114. throw new GameFrameworkException("Can not remove child entity which is not exist.");
  115. }
  116. }
  117. }
  118. }
  119. }