Entity.cs 4.9 KB

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