Entity.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using MongoDB.Bson.Serialization.Attributes;
  2. namespace KYFramework
  3. {
  4. [BsonIgnoreExtraElements]
  5. public class Entity : ComponentWithId
  6. {
  7. [BsonElement("C")]
  8. [BsonIgnoreIfNull]
  9. private HashSet<Component> components = new HashSet<Component>();
  10. [BsonIgnore]
  11. private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  12. public Entity()
  13. {
  14. }
  15. protected Entity(long id) : base(id)
  16. {
  17. }
  18. public override void Dispose()
  19. {
  20. if (this.IsDisposed)
  21. {
  22. return;
  23. }
  24. base.Dispose();
  25. foreach (Component component in this.componentDict.Values)
  26. {
  27. try
  28. {
  29. component.Dispose();
  30. }
  31. catch (Exception e)
  32. {
  33. Log.Error(e);
  34. }
  35. }
  36. this.components.Clear();
  37. this.componentDict.Clear();
  38. }
  39. public virtual Component AddComponent(Component component)
  40. {
  41. Type type = component.GetType();
  42. if (this.componentDict.ContainsKey(type))
  43. {
  44. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
  45. }
  46. component.Parent = this;
  47. this.componentDict.Add(type, component);
  48. if (component is ISerializeToEntity)
  49. {
  50. this.components.Add(component);
  51. }
  52. return component;
  53. }
  54. public virtual Component AddComponent(Type type)
  55. {
  56. if (this.componentDict.ContainsKey(type))
  57. {
  58. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
  59. }
  60. Component component = ComponentFactory.CreateWithParent(type, this, this.IsFromPool);
  61. this.componentDict.Add(type, component);
  62. if (component is ISerializeToEntity)
  63. {
  64. this.components.Add(component);
  65. }
  66. return component;
  67. }
  68. public virtual K AddComponent<K>() where K : Component, new()
  69. {
  70. Type type = typeof(K);
  71. if (this.componentDict.ContainsKey(type))
  72. {
  73. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  74. }
  75. K component = ComponentFactory.CreateWithParent<K>(this, this.IsFromPool);
  76. this.componentDict.Add(type, component);
  77. if (component is ISerializeToEntity)
  78. {
  79. this.components.Add(component);
  80. }
  81. return component;
  82. }
  83. public virtual K AddComponent<K, P1>(P1 p1) where K : Component, new()
  84. {
  85. Type type = typeof(K);
  86. if (this.componentDict.ContainsKey(type))
  87. {
  88. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  89. }
  90. K component = ComponentFactory.CreateWithParent<K, P1>(this, p1, this.IsFromPool);
  91. this.componentDict.Add(type, component);
  92. if (component is ISerializeToEntity)
  93. {
  94. this.components.Add(component);
  95. }
  96. return component;
  97. }
  98. public virtual K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  99. {
  100. Type type = typeof(K);
  101. if (this.componentDict.ContainsKey(type))
  102. {
  103. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  104. }
  105. K component = ComponentFactory.CreateWithParent<K, P1, P2>(this, p1, p2, this.IsFromPool);
  106. this.componentDict.Add(type, component);
  107. if (component is ISerializeToEntity)
  108. {
  109. this.components.Add(component);
  110. }
  111. return component;
  112. }
  113. public virtual K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  114. {
  115. Type type = typeof(K);
  116. if (this.componentDict.ContainsKey(type))
  117. {
  118. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  119. }
  120. K component = ComponentFactory.CreateWithParent<K, P1, P2, P3>(this, p1, p2, p3, this.IsFromPool);
  121. this.componentDict.Add(type, component);
  122. if (component is ISerializeToEntity)
  123. {
  124. this.components.Add(component);
  125. }
  126. return component;
  127. }
  128. public virtual void RemoveComponent<K>() where K : Component
  129. {
  130. if (this.IsDisposed)
  131. {
  132. return;
  133. }
  134. Type type = typeof(K);
  135. Component component;
  136. if (!this.componentDict.TryGetValue(type, out component))
  137. {
  138. return;
  139. }
  140. this.componentDict.Remove(type);
  141. this.components.Remove(component);
  142. component.Dispose();
  143. }
  144. public virtual void RemoveComponent(Type type)
  145. {
  146. if (this.IsDisposed)
  147. {
  148. return;
  149. }
  150. Component component;
  151. if (!this.componentDict.TryGetValue(type, out component))
  152. {
  153. return;
  154. }
  155. this.componentDict.Remove(type);
  156. this.components.Remove(component);
  157. component.Dispose();
  158. }
  159. public K GetComponent<K>() where K : Component
  160. {
  161. Component component;
  162. if (!this.componentDict.TryGetValue(typeof(K), out component))
  163. {
  164. return default(K);
  165. }
  166. return (K)component;
  167. }
  168. public Component GetComponent(Type type)
  169. {
  170. Component component;
  171. if (!this.componentDict.TryGetValue(type, out component))
  172. {
  173. return null;
  174. }
  175. return component;
  176. }
  177. public Component[] GetComponents()
  178. {
  179. return this.componentDict.Values.ToArray();
  180. }
  181. public override void EndInit()
  182. {
  183. try
  184. {
  185. base.EndInit();
  186. this.componentDict.Clear();
  187. if (this.components != null)
  188. {
  189. foreach (Component component in this.components)
  190. {
  191. component.Parent = this;
  192. this.componentDict.Add(component.GetType(), component);
  193. }
  194. }
  195. }
  196. catch (Exception e)
  197. {
  198. Log.Error(e);
  199. }
  200. }
  201. }
  202. }