Entity.cs 4.6 KB

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