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