Entity.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. }
  50. public K AddComponent<K>() where K : Component, new()
  51. {
  52. K component = (K) Activator.CreateInstance(typeof (K));
  53. component.Owner = this;
  54. if (this.componentDict.ContainsKey(component.GetType()))
  55. {
  56. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  57. }
  58. if (this.components == null)
  59. {
  60. this.components = new HashSet<Component>();
  61. }
  62. if (component is ComponentDB)
  63. {
  64. this.components.Add(component);
  65. }
  66. this.componentDict.Add(component.GetType(), component);
  67. ObjectEvents.Instance.Awake(component);
  68. return component;
  69. }
  70. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  71. {
  72. K component = (K) Activator.CreateInstance(typeof (K));
  73. component.Owner = 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>();
  81. }
  82. if (component is ComponentDB)
  83. {
  84. this.components.Add(component);
  85. }
  86. this.componentDict.Add(component.GetType(), component);
  87. ObjectEvents.Instance.Awake(component, p1);
  88. return component;
  89. }
  90. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  91. {
  92. K component = (K) Activator.CreateInstance(typeof (K));
  93. component.Owner = this;
  94. if (this.componentDict.ContainsKey(component.GetType()))
  95. {
  96. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  97. }
  98. if (this.components == null)
  99. {
  100. this.components = new HashSet<Component>();
  101. }
  102. if (component is ComponentDB)
  103. {
  104. this.components.Add(component);
  105. }
  106. this.componentDict.Add(component.GetType(), component);
  107. ObjectEvents.Instance.Awake(component, p1, p2);
  108. return component;
  109. }
  110. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  111. {
  112. K component = (K) Activator.CreateInstance(typeof (K));
  113. component.Owner = this;
  114. if (this.componentDict.ContainsKey(component.GetType()))
  115. {
  116. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  117. }
  118. if (this.components == null)
  119. {
  120. this.components = new HashSet<Component>();
  121. }
  122. if (component is ComponentDB)
  123. {
  124. this.components.Add(component);
  125. }
  126. this.componentDict.Add(component.GetType(), component);
  127. ObjectEvents.Instance.Awake(component, p1, p2, p3);
  128. return component;
  129. }
  130. public void AddComponent(Component component)
  131. {
  132. if (this.componentDict.ContainsKey(component.GetType()))
  133. {
  134. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {component.GetType().Name}");
  135. }
  136. if (this.components == null)
  137. {
  138. this.components = new HashSet<Component>();
  139. }
  140. if (component is ComponentDB)
  141. {
  142. this.components.Add(component);
  143. }
  144. this.componentDict.Add(component.GetType(), component);
  145. ObjectEvents.Instance.Awake(component);
  146. }
  147. public void RemoveComponent<K>() where K : Component
  148. {
  149. if (!this.componentDict.TryGetValue(typeof(K), out Component component))
  150. {
  151. return;
  152. }
  153. this.components.Remove(component);
  154. this.componentDict.Remove(typeof (K));
  155. if (this.components.Count == 0)
  156. {
  157. this.components = null;
  158. }
  159. component.Dispose();
  160. }
  161. public void RemoveComponent(Type type)
  162. {
  163. if (!this.componentDict.TryGetValue(type, out Component component))
  164. {
  165. return;
  166. }
  167. this.components.Remove(component);
  168. this.componentDict.Remove(type);
  169. if (this.components.Count == 0)
  170. {
  171. this.components = null;
  172. }
  173. component.Dispose();
  174. }
  175. public K GetComponent<K>() where K : Component
  176. {
  177. if (!this.componentDict.TryGetValue(typeof(K), out Component component))
  178. {
  179. return default(K);
  180. }
  181. return (K) component;
  182. }
  183. public Component[] GetComponents()
  184. {
  185. return components.ToArray();
  186. }
  187. public override void BeginInit()
  188. {
  189. base.BeginInit();
  190. this.components = new HashSet<Component>();
  191. this.componentDict = new Dictionary<Type, Component>();
  192. }
  193. public override void EndInit()
  194. {
  195. base.EndInit();
  196. ObjectEvents.Instance.Add(this);
  197. if (this.components.Count == 0)
  198. {
  199. this.components = null;
  200. return;
  201. }
  202. foreach (Component component in this.components)
  203. {
  204. component.Owner = this;
  205. this.componentDict.Add(component.GetType(), component);
  206. }
  207. }
  208. }
  209. }