Entity.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MongoDB.Bson.Serialization.Attributes;
  5. #if !SERVER
  6. using UnityEngine;
  7. #endif
  8. namespace ETModel
  9. {
  10. [BsonIgnoreExtraElements]
  11. public class Entity : ComponentWithId
  12. {
  13. [BsonElement("C")]
  14. [BsonIgnoreIfNull]
  15. private HashSet<Component> components = new HashSet<Component>();
  16. [BsonIgnore]
  17. private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  18. public Entity()
  19. {
  20. }
  21. protected Entity(long id): base(id)
  22. {
  23. }
  24. public override void Dispose()
  25. {
  26. if (this.IsDisposed)
  27. {
  28. return;
  29. }
  30. base.Dispose();
  31. foreach (Component component in this.componentDict.Values)
  32. {
  33. try
  34. {
  35. component.Dispose();
  36. }
  37. catch (Exception e)
  38. {
  39. Log.Error(e);
  40. }
  41. }
  42. this.components.Clear();
  43. this.componentDict.Clear();
  44. }
  45. public virtual Component AddComponent(Component component)
  46. {
  47. Type type = component.GetType();
  48. if (this.componentDict.ContainsKey(type))
  49. {
  50. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
  51. }
  52. component.Parent = this;
  53. this.componentDict.Add(type, component);
  54. return component;
  55. }
  56. public virtual Component AddComponent(Type type)
  57. {
  58. if (this.componentDict.ContainsKey(type))
  59. {
  60. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
  61. }
  62. Component component = ComponentFactory.CreateWithParent(type, this);
  63. this.componentDict.Add(type, component);
  64. return component;
  65. }
  66. public virtual K AddComponent<K>() where K : Component, new()
  67. {
  68. Type type = typeof (K);
  69. if (this.componentDict.ContainsKey(type))
  70. {
  71. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  72. }
  73. K component = ComponentFactory.CreateWithParent<K>(this);
  74. this.componentDict.Add(type, component);
  75. return component;
  76. }
  77. public virtual K AddComponent<K, P1>(P1 p1) where K : Component, new()
  78. {
  79. Type type = typeof (K);
  80. if (this.componentDict.ContainsKey(type))
  81. {
  82. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  83. }
  84. K component = ComponentFactory.CreateWithParent<K, P1>(this, p1);
  85. this.componentDict.Add(type, component);
  86. return component;
  87. }
  88. public virtual K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  89. {
  90. Type type = typeof (K);
  91. if (this.componentDict.ContainsKey(type))
  92. {
  93. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  94. }
  95. K component = ComponentFactory.CreateWithParent<K, P1, P2>(this, p1, p2);
  96. this.componentDict.Add(type, component);
  97. return component;
  98. }
  99. public virtual K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  100. {
  101. Type type = typeof (K);
  102. if (this.componentDict.ContainsKey(type))
  103. {
  104. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  105. }
  106. K component = ComponentFactory.CreateWithParent<K, P1, P2, P3>(this, p1, p2, p3);
  107. this.componentDict.Add(type, component);
  108. return component;
  109. }
  110. public virtual void RemoveComponent<K>() where K : Component
  111. {
  112. if (this.IsDisposed)
  113. {
  114. return;
  115. }
  116. Type type = typeof (K);
  117. Component component;
  118. if (!this.componentDict.TryGetValue(type, out component))
  119. {
  120. return;
  121. }
  122. this.componentDict.Remove(type);
  123. component.Dispose();
  124. }
  125. public virtual void RemoveComponent(Type type)
  126. {
  127. if (this.IsDisposed)
  128. {
  129. return;
  130. }
  131. Component component;
  132. if (!this.componentDict.TryGetValue(type, out component))
  133. {
  134. return;
  135. }
  136. this.componentDict.Remove(type);
  137. component.Dispose();
  138. }
  139. public K GetComponent<K>() where K : Component
  140. {
  141. Component component;
  142. if (!this.componentDict.TryGetValue(typeof(K), out component))
  143. {
  144. return default(K);
  145. }
  146. return (K)component;
  147. }
  148. public Component GetComponent(Type type)
  149. {
  150. Component component;
  151. if (!this.componentDict.TryGetValue(type, out component))
  152. {
  153. return null;
  154. }
  155. return component;
  156. }
  157. public Component[] GetComponents()
  158. {
  159. return this.componentDict.Values.ToArray();
  160. }
  161. public override void BeginInit()
  162. {
  163. base.BeginInit();
  164. this.components.Clear();
  165. foreach (var kv in this.componentDict)
  166. {
  167. if (kv.Value is ISerializeToEntity)
  168. {
  169. this.components.Add(kv.Value);
  170. }
  171. }
  172. }
  173. public override void EndInit()
  174. {
  175. try
  176. {
  177. base.EndInit();
  178. this.componentDict.Clear();
  179. if (this.components != null)
  180. {
  181. foreach (Component component in this.components)
  182. {
  183. component.Parent = this;
  184. this.componentDict.Add(component.GetType(), component);
  185. }
  186. }
  187. }
  188. catch (Exception e)
  189. {
  190. Log.Error(e);
  191. }
  192. }
  193. }
  194. }