Entity.cs 4.9 KB

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