Entity.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 = ObjectFactory.Create<K>();
  45. component.Owner = this;
  46. if (this.componentDict.ContainsKey(component.GetType()))
  47. {
  48. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  49. }
  50. if (this.components == null)
  51. {
  52. this.components = new HashSet<Component>();
  53. }
  54. if (component is ComponentDB)
  55. {
  56. this.components.Add(component);
  57. }
  58. this.componentDict.Add(component.GetType(), component);
  59. return component;
  60. }
  61. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  62. {
  63. K component = ObjectFactory.Create<K, P1>(p1);
  64. component.Owner = this;
  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 ComponentDB)
  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 = ObjectFactory.Create<K, P1, P2>(p1, p2);
  83. component.Owner = this;
  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 = ObjectFactory.Create<K, P1, P2, P3>(p1, p2, p3);
  102. component.Owner = this;
  103. if (this.componentDict.ContainsKey(component.GetType()))
  104. {
  105. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  106. }
  107. if (this.components == null)
  108. {
  109. this.components = new HashSet<Component>();
  110. }
  111. if (component is ComponentDB)
  112. {
  113. this.components.Add(component);
  114. }
  115. this.componentDict.Add(component.GetType(), component);
  116. return component;
  117. }
  118. public void RemoveComponent<K>() where K : Component
  119. {
  120. if (!this.componentDict.TryGetValue(typeof(K), out Component component))
  121. {
  122. return;
  123. }
  124. this.components.Remove(component);
  125. this.componentDict.Remove(typeof (K));
  126. if (this.components.Count == 0)
  127. {
  128. this.components = null;
  129. }
  130. component.Dispose();
  131. }
  132. public void RemoveComponent(Type type)
  133. {
  134. if (!this.componentDict.TryGetValue(type, out Component component))
  135. {
  136. return;
  137. }
  138. this.components.Remove(component);
  139. this.componentDict.Remove(type);
  140. if (this.components.Count == 0)
  141. {
  142. this.components = null;
  143. }
  144. component.Dispose();
  145. }
  146. public K GetComponent<K>() where K : Component
  147. {
  148. if (!this.componentDict.TryGetValue(typeof(K), out Component component))
  149. {
  150. return default(K);
  151. }
  152. return (K) component;
  153. }
  154. public Component[] GetComponents()
  155. {
  156. return this.componentDict.Values.ToArray();
  157. }
  158. public override void BeginInit()
  159. {
  160. base.BeginInit();
  161. this.components = new HashSet<Component>();
  162. this.componentDict = new Dictionary<Type, Component>();
  163. }
  164. public override void EndInit()
  165. {
  166. base.EndInit();
  167. ObjectEvents.Instance.Add(this);
  168. if (this.components.Count == 0)
  169. {
  170. this.components = null;
  171. return;
  172. }
  173. foreach (Component component in this.components)
  174. {
  175. component.Owner = this;
  176. this.componentDict.Add(component.GetType(), component);
  177. }
  178. }
  179. }
  180. }