Entity.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MongoDB.Bson.Serialization.Attributes;
  5. namespace Model
  6. {
  7. [BsonIgnoreExtraElements]
  8. public partial class Entity : Disposer
  9. {
  10. [BsonIgnore]
  11. public Entity Parent { get; set; }
  12. [BsonElement]
  13. [BsonIgnoreIfNull]
  14. private HashSet<Component> components;
  15. [BsonIgnore]
  16. private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  17. protected Entity()
  18. {
  19. this.Id = IdGenerater.GenerateId();
  20. }
  21. protected Entity(long id)
  22. {
  23. this.Id = id;
  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 = ComponentFactory.Create<K>(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 ISerializeToEntity)
  56. {
  57. this.components.Add(component);
  58. }
  59. this.componentDict.Add(component.GetType(), component);
  60. return component;
  61. }
  62. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  63. {
  64. K component = ComponentFactory.Create<K, P1>(this, p1);
  65. if (this.componentDict.ContainsKey(component.GetType()))
  66. {
  67. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  68. }
  69. if (this.components == null)
  70. {
  71. this.components = new HashSet<Component>();
  72. }
  73. if (component is ISerializeToEntity)
  74. {
  75. this.components.Add(component);
  76. }
  77. this.componentDict.Add(component.GetType(), component);
  78. return component;
  79. }
  80. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  81. {
  82. K component = ComponentFactory.Create<K, P1, P2>(this, p1, p2);
  83. if (this.componentDict.ContainsKey(component.GetType()))
  84. {
  85. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  86. }
  87. if (this.components == null)
  88. {
  89. this.components = new HashSet<Component>();
  90. }
  91. if (component is ISerializeToEntity)
  92. {
  93. this.components.Add(component);
  94. }
  95. this.componentDict.Add(component.GetType(), component);
  96. return component;
  97. }
  98. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  99. {
  100. K component = ComponentFactory.Create<K, P1, P2, P3>(this, p1, p2, p3);
  101. if (this.componentDict.ContainsKey(component.GetType()))
  102. {
  103. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  104. }
  105. if (this.components == null)
  106. {
  107. this.components = new HashSet<Component>();
  108. }
  109. if (component is ISerializeToEntity)
  110. {
  111. this.components.Add(component);
  112. }
  113. this.componentDict.Add(component.GetType(), component);
  114. return component;
  115. }
  116. public void RemoveComponent<K>() where K : Component
  117. {
  118. Component component;
  119. if (!this.componentDict.TryGetValue(typeof(K), out component))
  120. {
  121. return;
  122. }
  123. this.components?.Remove(component);
  124. this.componentDict.Remove(typeof(K));
  125. if (this.components != null && this.components.Count == 0)
  126. {
  127. this.components = null;
  128. }
  129. component.Dispose();
  130. }
  131. public void RemoveComponent(Type type)
  132. {
  133. Component component;
  134. if (!this.componentDict.TryGetValue(type, out component))
  135. {
  136. return;
  137. }
  138. this.components?.Remove(component);
  139. this.componentDict.Remove(type);
  140. if (this.components != null && this.components.Count == 0)
  141. {
  142. this.components = null;
  143. }
  144. component.Dispose();
  145. }
  146. public K GetComponent<K>() where K : Component
  147. {
  148. Component component;
  149. if (!this.componentDict.TryGetValue(typeof(K), out component))
  150. {
  151. return default(K);
  152. }
  153. return (K)component;
  154. }
  155. public Component[] GetComponents()
  156. {
  157. return this.componentDict.Values.ToArray();
  158. }
  159. public override void BeginInit()
  160. {
  161. this.components = new HashSet<Component>();
  162. this.componentDict = new Dictionary<Type, Component>();
  163. }
  164. public override void EndInit()
  165. {
  166. try
  167. {
  168. ObjectEvents.Instance.Add(this);
  169. if (this.components != null && this.components.Count == 0)
  170. {
  171. this.components = null;
  172. }
  173. this.componentDict.Clear();
  174. if (this.components != null)
  175. {
  176. foreach (Component component in this.components)
  177. {
  178. component.Entity = this;
  179. this.componentDict.Add(component.GetType(), component);
  180. }
  181. }
  182. }
  183. catch (Exception e)
  184. {
  185. Log.Error(e.ToString());
  186. }
  187. }
  188. }
  189. }