Entity.cs 5.5 KB

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