Entity.cs 5.3 KB

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