Entity.cs 4.9 KB

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