Entity.cs 5.1 KB

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