Entity.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. [BsonIgnore]
  12. public Entity Parent { get; set; }
  13. [BsonElement]
  14. [BsonIgnoreIfNull]
  15. private HashSet<Component> components;
  16. [BsonIgnore]
  17. private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  18. protected Entity()
  19. {
  20. this.Id = IdGenerater.GenerateId();
  21. }
  22. protected Entity(long id)
  23. {
  24. this.Id = id;
  25. }
  26. public override void Dispose()
  27. {
  28. if (this.Id == 0)
  29. {
  30. return;
  31. }
  32. base.Dispose();
  33. foreach (Component component in this.GetComponents())
  34. {
  35. try
  36. {
  37. component.Dispose();
  38. }
  39. catch (Exception e)
  40. {
  41. Log.Error(e.ToString());
  42. }
  43. }
  44. }
  45. public K AddComponent<K>() where K : Component, new()
  46. {
  47. K component = ComponentFactory.Create<K>(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. return component;
  62. }
  63. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  64. {
  65. K component = ComponentFactory.Create<K, P1>(this, p1);
  66. if (this.componentDict.ContainsKey(component.GetType()))
  67. {
  68. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  69. }
  70. if (this.components == null)
  71. {
  72. this.components = new HashSet<Component>();
  73. }
  74. if (component is ComponentDB)
  75. {
  76. this.components.Add(component);
  77. }
  78. this.componentDict.Add(component.GetType(), component);
  79. return component;
  80. }
  81. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  82. {
  83. K component = ComponentFactory.Create<K, P1, P2>(this, p1, p2);
  84. if (this.componentDict.ContainsKey(component.GetType()))
  85. {
  86. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  87. }
  88. if (this.components == null)
  89. {
  90. this.components = new HashSet<Component>();
  91. }
  92. if (component is ComponentDB)
  93. {
  94. this.components.Add(component);
  95. }
  96. this.componentDict.Add(component.GetType(), component);
  97. return component;
  98. }
  99. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  100. {
  101. K component = ComponentFactory.Create<K, P1, P2, P3>(this, p1, p2, p3);
  102. if (this.componentDict.ContainsKey(component.GetType()))
  103. {
  104. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  105. }
  106. if (this.components == null)
  107. {
  108. this.components = new HashSet<Component>();
  109. }
  110. if (component is ComponentDB)
  111. {
  112. this.components.Add(component);
  113. }
  114. this.componentDict.Add(component.GetType(), component);
  115. return component;
  116. }
  117. public void RemoveComponent<K>() where K : Component
  118. {
  119. Component component;
  120. if (!this.componentDict.TryGetValue(typeof(K), out component))
  121. {
  122. return;
  123. }
  124. this.components?.Remove(component);
  125. this.componentDict.Remove(typeof(K));
  126. if (this.components != null && this.components.Count == 0)
  127. {
  128. this.components = null;
  129. }
  130. component.Dispose();
  131. }
  132. public void RemoveComponent(Type type)
  133. {
  134. Component component;
  135. if (!this.componentDict.TryGetValue(type, out component))
  136. {
  137. return;
  138. }
  139. this.components?.Remove(component);
  140. this.componentDict.Remove(type);
  141. if (this.components != null && this.components.Count == 0)
  142. {
  143. this.components = null;
  144. }
  145. component.Dispose();
  146. }
  147. public K GetComponent<K>() where K : Component
  148. {
  149. Component component;
  150. if (!this.componentDict.TryGetValue(typeof(K), out component))
  151. {
  152. return default(K);
  153. }
  154. return (K)component;
  155. }
  156. public Component[] GetComponents()
  157. {
  158. return this.componentDict.Values.ToArray();
  159. }
  160. public virtual void BeginInit()
  161. {
  162. this.components = new HashSet<Component>();
  163. this.componentDict = new Dictionary<Type, Component>();
  164. }
  165. public virtual void EndInit()
  166. {
  167. ObjectEvents.Instance.Add(this);
  168. if (this.components != null && this.components.Count == 0)
  169. {
  170. this.components = null;
  171. }
  172. if (this.components != null)
  173. {
  174. foreach (Component component in this.components)
  175. {
  176. component.Entity = this;
  177. this.componentDict.Add(component.GetType(), component);
  178. }
  179. }
  180. }
  181. #if AOT
  182. private void AvoidAot()
  183. {
  184. EnumSerializer<EntityType> e = new EnumSerializer<EntityType>();
  185. }
  186. #endif
  187. }
  188. }