Entity.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Base;
  5. using MongoDB.Bson.Serialization.Attributes;
  6. namespace Base
  7. {
  8. public abstract class Entity<T>: Object where T : Entity<T>
  9. {
  10. [BsonElement, BsonIgnoreIfNull]
  11. private HashSet<Component<T>> components = new HashSet<Component<T>>();
  12. private Dictionary<Type, Component<T>> componentDict = new Dictionary<Type, Component<T>>();
  13. [BsonIgnore]
  14. public T Parent { get; set; }
  15. public string Name { get; }
  16. [BsonElement, BsonIgnoreIfNull]
  17. private Dictionary<long, T> idChildren;
  18. [BsonElement, BsonIgnoreIfNull]
  19. private Dictionary<string, T> nameChildren;
  20. protected Entity()
  21. {
  22. this.Name = "";
  23. ObjectManager.Add(this);
  24. }
  25. protected Entity(string name)
  26. {
  27. this.Name = name;
  28. ObjectManager.Add(this);
  29. }
  30. protected Entity(long id, string name): base(id)
  31. {
  32. this.Name = name;
  33. ObjectManager.Add(this);
  34. }
  35. public T Clone()
  36. {
  37. return MongoHelper.FromBson<T>(MongoHelper.ToBson(this));
  38. }
  39. public int Count
  40. {
  41. get
  42. {
  43. return this.idChildren.Count;
  44. }
  45. }
  46. public void Add(T t)
  47. {
  48. t.Parent = (T)this;
  49. if (this.idChildren == null)
  50. {
  51. this.idChildren = new Dictionary<long, T>();
  52. this.nameChildren = new Dictionary<string, T>();
  53. }
  54. this.idChildren.Add(t.Id, t);
  55. this.nameChildren.Add(t.Name, t);
  56. }
  57. private void Remove(T t)
  58. {
  59. this.idChildren.Remove(t.Id);
  60. this.nameChildren.Remove(t.Name);
  61. if (this.idChildren.Count == 0)
  62. {
  63. this.idChildren = null;
  64. this.nameChildren = null;
  65. }
  66. t.Dispose();
  67. }
  68. public void Remove(long id)
  69. {
  70. T t;
  71. if (!this.idChildren.TryGetValue(id, out t))
  72. {
  73. return;
  74. }
  75. this.Remove(t);
  76. }
  77. public void Remove(string name)
  78. {
  79. T t;
  80. if (!this.nameChildren.TryGetValue(name, out t))
  81. {
  82. return;
  83. }
  84. this.Remove(t);
  85. }
  86. public override void Dispose()
  87. {
  88. if (this.Id == 0)
  89. {
  90. return;
  91. }
  92. base.Dispose();
  93. foreach (T t in this.idChildren.Values.ToArray())
  94. {
  95. t.Dispose();
  96. }
  97. foreach (Component<T> component in this.GetComponents())
  98. {
  99. try
  100. {
  101. component.Dispose();
  102. }
  103. catch (Exception e)
  104. {
  105. Log.Error(e.ToString());
  106. }
  107. }
  108. this.Parent?.Remove(this.Id);
  109. ObjectManager.Remove(this.Id);
  110. }
  111. public K AddComponent<K>() where K : Component<T>, new()
  112. {
  113. K component = (K) Activator.CreateInstance(typeof (K));
  114. component.Owner = (T) this;
  115. if (this.componentDict.ContainsKey(component.GetType()))
  116. {
  117. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  118. }
  119. if (this.components == null)
  120. {
  121. this.components = new HashSet<Component<T>>();
  122. }
  123. this.components.Add(component);
  124. this.componentDict.Add(component.GetType(), component);
  125. ObjectManager.Awake(component.Id);
  126. return component;
  127. }
  128. public K AddComponent<K, P1>(P1 p1) where K : Component<T>, new()
  129. {
  130. K component = (K)Activator.CreateInstance(typeof(K));
  131. component.Owner = (T)this;
  132. if (this.componentDict.ContainsKey(component.GetType()))
  133. {
  134. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  135. }
  136. if (this.components == null)
  137. {
  138. this.components = new HashSet<Component<T>>();
  139. }
  140. this.components.Add(component);
  141. this.componentDict.Add(component.GetType(), component);
  142. ObjectManager.Awake(component.Id, p1);
  143. return component;
  144. }
  145. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component<T>, new()
  146. {
  147. K component = (K)Activator.CreateInstance(typeof(K));
  148. component.Owner = (T)this;
  149. if (this.componentDict.ContainsKey(component.GetType()))
  150. {
  151. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  152. }
  153. if (this.components == null)
  154. {
  155. this.components = new HashSet<Component<T>>();
  156. }
  157. this.components.Add(component);
  158. this.componentDict.Add(component.GetType(), component);
  159. ObjectManager.Awake(component.Id, p1, p2);
  160. return component;
  161. }
  162. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component<T>, new()
  163. {
  164. K component = (K)Activator.CreateInstance(typeof(K));
  165. component.Owner = (T)this;
  166. if (this.componentDict.ContainsKey(component.GetType()))
  167. {
  168. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  169. }
  170. if (this.components == null)
  171. {
  172. this.components = new HashSet<Component<T>>();
  173. }
  174. this.components.Add(component);
  175. this.componentDict.Add(component.GetType(), component);
  176. ObjectManager.Awake(component.Id, p1, p2, p3);
  177. return component;
  178. }
  179. public void AddComponent(Component<T> component)
  180. {
  181. if (this.componentDict.ContainsKey(component.GetType()))
  182. {
  183. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {component.GetType().Name}");
  184. }
  185. if (this.components == null)
  186. {
  187. this.components = new HashSet<Component<T>>();
  188. }
  189. this.components.Add(component);
  190. this.componentDict.Add(component.GetType(), component);
  191. ObjectManager.Awake(component.Id);
  192. }
  193. public void RemoveComponent<K>() where K : Component<T>
  194. {
  195. Component<T> component;
  196. if (!this.componentDict.TryGetValue(typeof (K), out component))
  197. {
  198. return;
  199. }
  200. this.components.Remove(component);
  201. this.componentDict.Remove(typeof (K));
  202. if (this.components.Count == 0)
  203. {
  204. this.components = null;
  205. }
  206. component.Dispose();
  207. }
  208. public K GetComponent<K>() where K : Component<T>
  209. {
  210. Component<T> component;
  211. if (!this.componentDict.TryGetValue(typeof (K), out component))
  212. {
  213. return default(K);
  214. }
  215. return (K) component;
  216. }
  217. public Component<T>[] GetComponents()
  218. {
  219. return components.ToArray();
  220. }
  221. public override void BeginInit()
  222. {
  223. base.BeginInit();
  224. this.components = new HashSet<Component<T>>();
  225. this.componentDict = new Dictionary<Type, Component<T>>();
  226. }
  227. public override void EndInit()
  228. {
  229. base.EndInit();
  230. if (this.components.Count == 0)
  231. {
  232. this.components = null;
  233. return;
  234. }
  235. foreach (Component<T> component in this.components)
  236. {
  237. component.Owner = (T) this;
  238. this.componentDict.Add(component.GetType(), component);
  239. }
  240. }
  241. }
  242. }