Entity.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 : Disposer
  9. {
  10. [BsonIgnore]
  11. public Entity Parent { 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. this.Id = IdGenerater.GenerateId();
  20. }
  21. protected Entity(long id)
  22. {
  23. this.Id = id;
  24. }
  25. public override void Dispose()
  26. {
  27. if (this.Id == 0)
  28. {
  29. return;
  30. }
  31. base.Dispose();
  32. foreach (Component component in this.GetComponents())
  33. {
  34. try
  35. {
  36. component.Dispose();
  37. }
  38. catch (Exception e)
  39. {
  40. Log.Error(e.ToString());
  41. }
  42. }
  43. this.components.Clear();
  44. this.componentDict.Clear();
  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 (component is ISerializeToEntity)
  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 (component is ISerializeToEntity)
  68. {
  69. this.components.Add(component);
  70. }
  71. this.componentDict.Add(component.GetType(), component);
  72. return component;
  73. }
  74. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  75. {
  76. K component = ComponentFactory.Create<K, P1, P2>(this, p1, p2);
  77. if (this.componentDict.ContainsKey(component.GetType()))
  78. {
  79. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  80. }
  81. if (component is ISerializeToEntity)
  82. {
  83. this.components.Add(component);
  84. }
  85. this.componentDict.Add(component.GetType(), component);
  86. return component;
  87. }
  88. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  89. {
  90. K component = ComponentFactory.Create<K, P1, P2, P3>(this, p1, p2, p3);
  91. if (this.componentDict.ContainsKey(component.GetType()))
  92. {
  93. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  94. }
  95. if (component is ISerializeToEntity)
  96. {
  97. this.components.Add(component);
  98. }
  99. this.componentDict.Add(component.GetType(), component);
  100. return component;
  101. }
  102. public void RemoveComponent<K>() where K : Component
  103. {
  104. Component component;
  105. if (!this.componentDict.TryGetValue(typeof(K), out component))
  106. {
  107. return;
  108. }
  109. this.components.Remove(component);
  110. this.componentDict.Remove(typeof(K));
  111. component.Dispose();
  112. }
  113. public void RemoveComponent(Type type)
  114. {
  115. Component component;
  116. if (!this.componentDict.TryGetValue(type, out component))
  117. {
  118. return;
  119. }
  120. this.components?.Remove(component);
  121. this.componentDict.Remove(type);
  122. component.Dispose();
  123. }
  124. public K GetComponent<K>() where K : Component
  125. {
  126. Component component;
  127. if (!this.componentDict.TryGetValue(typeof(K), out component))
  128. {
  129. return default(K);
  130. }
  131. return (K)component;
  132. }
  133. public Component[] GetComponents()
  134. {
  135. return this.componentDict.Values.ToArray();
  136. }
  137. public override void BeginInit()
  138. {
  139. this.components = new HashSet<Component>();
  140. this.componentDict = new Dictionary<Type, Component>();
  141. }
  142. public override void EndInit()
  143. {
  144. try
  145. {
  146. ObjectEvents.Instance.Add(this);
  147. this.componentDict.Clear();
  148. if (this.components != null)
  149. {
  150. foreach (Component component in this.components)
  151. {
  152. component.Entity = this;
  153. this.componentDict.Add(component.GetType(), component);
  154. }
  155. }
  156. }
  157. catch (Exception e)
  158. {
  159. Log.Error(e.ToString());
  160. }
  161. }
  162. }
  163. }