Entity.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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("C")]
  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.componentDict.Values)
  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 virtual 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 virtual 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 virtual 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 virtual 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 virtual 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 virtual 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 virtual void RemoveComponent<K>() where K : Component
  136. {
  137. if (this.IsDisposed)
  138. {
  139. return;
  140. }
  141. Type type = typeof (K);
  142. Component component;
  143. if (!this.componentDict.TryGetValue(type, out component))
  144. {
  145. return;
  146. }
  147. this.components.Remove(component);
  148. this.componentDict.Remove(type);
  149. component.Dispose();
  150. }
  151. public virtual void RemoveComponent(Type type)
  152. {
  153. if (this.IsDisposed)
  154. {
  155. return;
  156. }
  157. Component component;
  158. if (!this.componentDict.TryGetValue(type, out component))
  159. {
  160. return;
  161. }
  162. this.components?.Remove(component);
  163. this.componentDict.Remove(type);
  164. component.Dispose();
  165. }
  166. public K GetComponent<K>() where K : Component
  167. {
  168. Component component;
  169. if (!this.componentDict.TryGetValue(typeof(K), out component))
  170. {
  171. return default(K);
  172. }
  173. return (K)component;
  174. }
  175. public Component GetComponent(Type type)
  176. {
  177. Component component;
  178. if (!this.componentDict.TryGetValue(type, out component))
  179. {
  180. return null;
  181. }
  182. return component;
  183. }
  184. public Component[] GetComponents()
  185. {
  186. return this.componentDict.Values.ToArray();
  187. }
  188. public override void EndInit()
  189. {
  190. try
  191. {
  192. base.EndInit();
  193. this.componentDict.Clear();
  194. if (this.components != null)
  195. {
  196. foreach (Component component in this.components)
  197. {
  198. component.Parent = this;
  199. this.componentDict.Add(component.GetType(), component);
  200. }
  201. }
  202. }
  203. catch (Exception e)
  204. {
  205. Log.Error(e);
  206. }
  207. }
  208. }
  209. }