Entity.cs 4.8 KB

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