Entity.cs 4.7 KB

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