Entity.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Model;
  5. using MongoDB.Bson.Serialization.Attributes;
  6. namespace Hotfix
  7. {
  8. public class Entity : Disposer
  9. {
  10. [BsonIgnore]
  11. public Entity Parent { get; set; }
  12. public EntityType Type { get; set; }
  13. [BsonElement]
  14. [BsonIgnoreIfNull]
  15. private HashSet<Component> components = new HashSet<Component>();
  16. [BsonIgnore]
  17. private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  18. protected Entity()
  19. {
  20. }
  21. protected Entity(long id) : base(id)
  22. {
  23. }
  24. public override void Dispose()
  25. {
  26. if (this.Id == 0)
  27. {
  28. return;
  29. }
  30. base.Dispose();
  31. foreach (Component component in this.GetComponents())
  32. {
  33. try
  34. {
  35. component.Dispose();
  36. }
  37. catch (Exception e)
  38. {
  39. Log.Error(e.ToString());
  40. }
  41. }
  42. }
  43. public K AddComponent<K>() where K : Component, new()
  44. {
  45. K component = ComponentFactory.Create<K>(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 = ComponentFactory.Create<K, P1>(this, p1);
  64. if (this.componentDict.ContainsKey(component.GetType()))
  65. {
  66. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  67. }
  68. if (this.components == null)
  69. {
  70. this.components = new HashSet<Component>();
  71. }
  72. if (component is ComponentDB)
  73. {
  74. this.components.Add(component);
  75. }
  76. this.componentDict.Add(component.GetType(), component);
  77. return component;
  78. }
  79. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  80. {
  81. K component = ComponentFactory.Create<K, P1, P2>(this, p1, p2);
  82. if (this.componentDict.ContainsKey(component.GetType()))
  83. {
  84. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  85. }
  86. if (this.components == null)
  87. {
  88. this.components = new HashSet<Component>();
  89. }
  90. if (component is ComponentDB)
  91. {
  92. this.components.Add(component);
  93. }
  94. this.componentDict.Add(component.GetType(), component);
  95. return component;
  96. }
  97. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  98. {
  99. K component = ComponentFactory.Create<K, P1, P2, P3>(this, p1, p2, p3);
  100. if (this.componentDict.ContainsKey(component.GetType()))
  101. {
  102. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  103. }
  104. if (this.components == null)
  105. {
  106. this.components = new HashSet<Component>();
  107. }
  108. if (component is ComponentDB)
  109. {
  110. this.components.Add(component);
  111. }
  112. this.componentDict.Add(component.GetType(), component);
  113. return component;
  114. }
  115. public void RemoveComponent<K>() where K : Component
  116. {
  117. Component component;
  118. if (!this.componentDict.TryGetValue(typeof(K), out component))
  119. {
  120. return;
  121. }
  122. this.components?.Remove(component);
  123. this.componentDict.Remove(typeof(K));
  124. if (this.components != null && this.components.Count == 0)
  125. {
  126. this.components = null;
  127. }
  128. component.Dispose();
  129. }
  130. public void RemoveComponent(Type type)
  131. {
  132. Component component;
  133. if (!this.componentDict.TryGetValue(type, out component))
  134. {
  135. return;
  136. }
  137. this.components?.Remove(component);
  138. this.componentDict.Remove(type);
  139. if (this.components != null && this.components.Count == 0)
  140. {
  141. this.components = null;
  142. }
  143. component.Dispose();
  144. }
  145. public K GetComponent<K>() where K : Component
  146. {
  147. Component component;
  148. if (!this.componentDict.TryGetValue(typeof(K), out 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. }
  159. }