Entity.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MongoDB.Bson.Serialization.Attributes;
  5. namespace Model
  6. {
  7. [BsonIgnoreExtraElements]
  8. public partial class Entity : Component
  9. {
  10. [BsonElement]
  11. [BsonIgnoreIfNull]
  12. private HashSet<Component> components;
  13. [BsonIgnore]
  14. private Dictionary<Type, Component> componentDict;
  15. protected Entity()
  16. {
  17. this.Id = IdGenerater.GenerateId();
  18. this.components = new HashSet<Component>();
  19. this.componentDict = new Dictionary<Type, Component>();
  20. }
  21. protected Entity(long id)
  22. {
  23. this.Id = id;
  24. this.components = new HashSet<Component>();
  25. this.componentDict = new Dictionary<Type, Component>();
  26. }
  27. public override void Dispose()
  28. {
  29. if (this.IsDisposed)
  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. this.components.Clear();
  46. this.componentDict.Clear();
  47. }
  48. public K AddComponent<K>() where K : Component, new()
  49. {
  50. K component = ComponentFactory.CreateWithParent<K>(this);
  51. if (this.componentDict.ContainsKey(component.GetType()))
  52. {
  53. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  54. }
  55. if (component is ISerializeToEntity)
  56. {
  57. this.components.Add(component);
  58. }
  59. this.componentDict.Add(component.GetType(), component);
  60. return component;
  61. }
  62. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  63. {
  64. K component = ComponentFactory.CreateWithParent<K, P1>(this, p1);
  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 (component is ISerializeToEntity)
  70. {
  71. this.components.Add(component);
  72. }
  73. this.componentDict.Add(component.GetType(), component);
  74. return component;
  75. }
  76. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  77. {
  78. K component = ComponentFactory.CreateWithParent<K, P1, P2>(this, p1, p2);
  79. if (this.componentDict.ContainsKey(component.GetType()))
  80. {
  81. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  82. }
  83. if (component is ISerializeToEntity)
  84. {
  85. this.components.Add(component);
  86. }
  87. this.componentDict.Add(component.GetType(), component);
  88. return component;
  89. }
  90. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  91. {
  92. K component = ComponentFactory.CreateWithParent<K, P1, P2, P3>(this, p1, p2, p3);
  93. if (this.componentDict.ContainsKey(component.GetType()))
  94. {
  95. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  96. }
  97. if (component is ISerializeToEntity)
  98. {
  99. this.components.Add(component);
  100. }
  101. this.componentDict.Add(component.GetType(), component);
  102. return component;
  103. }
  104. public void RemoveComponent<K>() where K : Component
  105. {
  106. Component component;
  107. if (!this.componentDict.TryGetValue(typeof(K), out component))
  108. {
  109. return;
  110. }
  111. this.components.Remove(component);
  112. this.componentDict.Remove(typeof(K));
  113. component.Dispose();
  114. }
  115. public void RemoveComponent(Type type)
  116. {
  117. Component component;
  118. if (!this.componentDict.TryGetValue(type, out component))
  119. {
  120. return;
  121. }
  122. this.components?.Remove(component);
  123. this.componentDict.Remove(type);
  124. component.Dispose();
  125. }
  126. public K GetComponent<K>() where K : Component
  127. {
  128. Component component;
  129. if (!this.componentDict.TryGetValue(typeof(K), out component))
  130. {
  131. return default(K);
  132. }
  133. return (K)component;
  134. }
  135. public Component[] GetComponents()
  136. {
  137. return this.componentDict.Values.ToArray();
  138. }
  139. public override void BeginInit()
  140. {
  141. this.components = new HashSet<Component>();
  142. this.componentDict = new Dictionary<Type, Component>();
  143. }
  144. public override void EndInit()
  145. {
  146. try
  147. {
  148. this.componentDict.Clear();
  149. if (this.components != null)
  150. {
  151. foreach (Component component in this.components)
  152. {
  153. component.Parent = this;
  154. this.componentDict.Add(component.GetType(), component);
  155. }
  156. }
  157. }
  158. catch (Exception e)
  159. {
  160. Log.Error(e.ToString());
  161. }
  162. }
  163. }
  164. }