Entity.cs 5.3 KB

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