Entity.cs 5.5 KB

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