Entity.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Base;
  5. using MongoDB.Bson.Serialization.Attributes;
  6. namespace Base
  7. {
  8. public sealed class Entity: Object
  9. {
  10. public string Type { get; set; }
  11. [BsonElement, BsonIgnoreIfNull]
  12. private HashSet<Component> components = new HashSet<Component>();
  13. [BsonIgnore]
  14. private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  15. public Entity(string entityType)
  16. {
  17. this.Type = entityType;
  18. ObjectManager.Add(this);
  19. }
  20. public Entity(long id, string entityType) : base(id)
  21. {
  22. this.Type = entityType;
  23. ObjectManager.Add(this);
  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. ObjectManager.Remove(this.Id);
  44. }
  45. public K AddComponent<K>() where K : Component, new()
  46. {
  47. K component = (K) Activator.CreateInstance(typeof (K));
  48. component.Owner = 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. this.components.Add(component);
  58. this.componentDict.Add(component.GetType(), component);
  59. ObjectManager.Awake(component.Id);
  60. return component;
  61. }
  62. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  63. {
  64. K component = (K)Activator.CreateInstance(typeof(K));
  65. component.Owner = this;
  66. if (this.componentDict.ContainsKey(component.GetType()))
  67. {
  68. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  69. }
  70. if (this.components == null)
  71. {
  72. this.components = new HashSet<Component>();
  73. }
  74. this.components.Add(component);
  75. this.componentDict.Add(component.GetType(), component);
  76. ObjectManager.Awake(component.Id, p1);
  77. return component;
  78. }
  79. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  80. {
  81. K component = (K)Activator.CreateInstance(typeof(K));
  82. component.Owner = this;
  83. if (this.componentDict.ContainsKey(component.GetType()))
  84. {
  85. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  86. }
  87. if (this.components == null)
  88. {
  89. this.components = new HashSet<Component>();
  90. }
  91. this.components.Add(component);
  92. this.componentDict.Add(component.GetType(), component);
  93. ObjectManager.Awake(component.Id, p1, p2);
  94. return component;
  95. }
  96. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  97. {
  98. K component = (K)Activator.CreateInstance(typeof(K));
  99. component.Owner = this;
  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. this.components.Add(component);
  109. this.componentDict.Add(component.GetType(), component);
  110. ObjectManager.Awake(component.Id, p1, p2, p3);
  111. return component;
  112. }
  113. public void AddComponent(Component component)
  114. {
  115. if (this.componentDict.ContainsKey(component.GetType()))
  116. {
  117. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {component.GetType().Name}");
  118. }
  119. if (this.components == null)
  120. {
  121. this.components = new HashSet<Component>();
  122. }
  123. this.components.Add(component);
  124. this.componentDict.Add(component.GetType(), component);
  125. ObjectManager.Awake(component.Id);
  126. }
  127. public void RemoveComponent<K>() where K : Component
  128. {
  129. Component component;
  130. if (!this.componentDict.TryGetValue(typeof (K), out component))
  131. {
  132. return;
  133. }
  134. this.components.Remove(component);
  135. this.componentDict.Remove(typeof (K));
  136. if (this.components.Count == 0)
  137. {
  138. this.components = null;
  139. }
  140. component.Dispose();
  141. }
  142. public K GetComponent<K>() where K : Component
  143. {
  144. Component component;
  145. if (!this.componentDict.TryGetValue(typeof (K), out component))
  146. {
  147. return default(K);
  148. }
  149. return (K) component;
  150. }
  151. public Component[] GetComponents()
  152. {
  153. return components.ToArray();
  154. }
  155. public override void BeginInit()
  156. {
  157. base.BeginInit();
  158. this.components = new HashSet<Component>();
  159. this.componentDict = new Dictionary<Type, Component>();
  160. }
  161. public override void EndInit()
  162. {
  163. base.EndInit();
  164. if (this.components.Count == 0)
  165. {
  166. this.components = null;
  167. return;
  168. }
  169. foreach (Component component in this.components)
  170. {
  171. component.Owner = this;
  172. this.componentDict.Add(component.GetType(), component);
  173. }
  174. }
  175. }
  176. }