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