Entity.cs 4.6 KB

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