Entity.cs 5.5 KB

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