Entity.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Base;
  5. using MongoDB.Bson.Serialization.Attributes;
  6. namespace Model
  7. {
  8. public class Entity: Disposer
  9. {
  10. public EntityType Type { get; set; }
  11. [BsonElement]
  12. [BsonIgnoreIfNull]
  13. private HashSet<Component> components = new HashSet<Component>();
  14. [BsonIgnore]
  15. private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  16. protected Entity()
  17. {
  18. ObjectEvents.Instance.Add(this);
  19. }
  20. protected Entity(long id): base(id)
  21. {
  22. ObjectEvents.Instance.Add(this);
  23. }
  24. public override void Dispose()
  25. {
  26. if (this.Id == 0)
  27. {
  28. return;
  29. }
  30. base.Dispose();
  31. foreach (Component component in this.GetComponents())
  32. {
  33. try
  34. {
  35. component.Dispose();
  36. }
  37. catch (Exception e)
  38. {
  39. Log.Error(e.ToString());
  40. }
  41. }
  42. }
  43. public K AddComponent<K>() where K : Component, new()
  44. {
  45. K component = (K) Activator.CreateInstance(typeof (K));
  46. component.Owner = this;
  47. if (this.componentDict.ContainsKey(component.GetType()))
  48. {
  49. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  50. }
  51. if (this.components == null)
  52. {
  53. this.components = new HashSet<Component>();
  54. }
  55. if (component is ComponentDB)
  56. {
  57. this.components.Add(component);
  58. }
  59. this.componentDict.Add(component.GetType(), component);
  60. ObjectEvents.Instance.Awake(component);
  61. return component;
  62. }
  63. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  64. {
  65. K component = (K) Activator.CreateInstance(typeof (K));
  66. component.Owner = this;
  67. if (this.componentDict.ContainsKey(component.GetType()))
  68. {
  69. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  70. }
  71. if (this.components == null)
  72. {
  73. this.components = new HashSet<Component>();
  74. }
  75. if (component is ComponentDB)
  76. {
  77. this.components.Add(component);
  78. }
  79. this.componentDict.Add(component.GetType(), component);
  80. ObjectEvents.Instance.Awake(component, p1);
  81. return component;
  82. }
  83. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  84. {
  85. K component = (K) Activator.CreateInstance(typeof (K));
  86. component.Owner = this;
  87. if (this.componentDict.ContainsKey(component.GetType()))
  88. {
  89. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  90. }
  91. if (this.components == null)
  92. {
  93. this.components = new HashSet<Component>();
  94. }
  95. if (component is ComponentDB)
  96. {
  97. this.components.Add(component);
  98. }
  99. this.componentDict.Add(component.GetType(), component);
  100. ObjectEvents.Instance.Awake(component, p1, p2);
  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 = (K) Activator.CreateInstance(typeof (K));
  106. component.Owner = this;
  107. if (this.componentDict.ContainsKey(component.GetType()))
  108. {
  109. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  110. }
  111. if (this.components == null)
  112. {
  113. this.components = new HashSet<Component>();
  114. }
  115. if (component is ComponentDB)
  116. {
  117. this.components.Add(component);
  118. }
  119. this.componentDict.Add(component.GetType(), component);
  120. ObjectEvents.Instance.Awake(component, p1, p2, p3);
  121. return component;
  122. }
  123. public void AddComponent(Component component)
  124. {
  125. if (this.componentDict.ContainsKey(component.GetType()))
  126. {
  127. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {component.GetType().Name}");
  128. }
  129. if (this.components == null)
  130. {
  131. this.components = new HashSet<Component>();
  132. }
  133. if (component is ComponentDB)
  134. {
  135. this.components.Add(component);
  136. }
  137. this.componentDict.Add(component.GetType(), component);
  138. ObjectEvents.Instance.Awake(component);
  139. }
  140. public void RemoveComponent<K>() where K : Component
  141. {
  142. if (!this.componentDict.TryGetValue(typeof(K), out Component component))
  143. {
  144. return;
  145. }
  146. this.components.Remove(component);
  147. this.componentDict.Remove(typeof (K));
  148. if (this.components.Count == 0)
  149. {
  150. this.components = null;
  151. }
  152. component.Dispose();
  153. }
  154. public void RemoveComponent(Type type)
  155. {
  156. if (!this.componentDict.TryGetValue(type, out Component component))
  157. {
  158. return;
  159. }
  160. this.components.Remove(component);
  161. this.componentDict.Remove(type);
  162. if (this.components.Count == 0)
  163. {
  164. this.components = null;
  165. }
  166. component.Dispose();
  167. }
  168. public K GetComponent<K>() where K : Component
  169. {
  170. if (!this.componentDict.TryGetValue(typeof(K), out Component component))
  171. {
  172. return default(K);
  173. }
  174. return (K) component;
  175. }
  176. public Component[] GetComponents()
  177. {
  178. return components.ToArray();
  179. }
  180. public override void BeginInit()
  181. {
  182. base.BeginInit();
  183. this.components = new HashSet<Component>();
  184. this.componentDict = new Dictionary<Type, Component>();
  185. }
  186. public override void EndInit()
  187. {
  188. base.EndInit();
  189. ObjectEvents.Instance.Add(this);
  190. if (this.components.Count == 0)
  191. {
  192. this.components = null;
  193. return;
  194. }
  195. foreach (Component component in this.components)
  196. {
  197. component.Owner = this;
  198. this.componentDict.Add(component.GetType(), component);
  199. }
  200. }
  201. }
  202. }