Entity.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. long id = this.Id;
  32. base.Dispose();
  33. foreach (Component component in this.GetComponents())
  34. {
  35. try
  36. {
  37. component.Dispose();
  38. }
  39. catch (Exception e)
  40. {
  41. Log.Error(e.ToString());
  42. }
  43. }
  44. ObjectManager.Remove(id);
  45. }
  46. public K AddComponent<K>() where K : Component, new()
  47. {
  48. K component = (K) Activator.CreateInstance(typeof (K));
  49. component.Owner = this;
  50. if (this.componentDict.ContainsKey(component.GetType()))
  51. {
  52. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  53. }
  54. if (this.components == null)
  55. {
  56. this.components = new HashSet<Component>();
  57. }
  58. this.components.Add(component);
  59. this.componentDict.Add(component.GetType(), component);
  60. ObjectManager.Awake(component.Id);
  61. return component;
  62. }
  63. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  64. {
  65. K component = (K)Activator.CreateInstance(typeof(K));
  66. component.Owner = this;
  67. if (this.componentDict.ContainsKey(component.GetType()))
  68. {
  69. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  70. }
  71. if (this.components == null)
  72. {
  73. this.components = new HashSet<Component>();
  74. }
  75. this.components.Add(component);
  76. this.componentDict.Add(component.GetType(), component);
  77. ObjectManager.Awake(component.Id, p1);
  78. return component;
  79. }
  80. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  81. {
  82. K component = (K)Activator.CreateInstance(typeof(K));
  83. component.Owner = this;
  84. if (this.componentDict.ContainsKey(component.GetType()))
  85. {
  86. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  87. }
  88. if (this.components == null)
  89. {
  90. this.components = new HashSet<Component>();
  91. }
  92. this.components.Add(component);
  93. this.componentDict.Add(component.GetType(), component);
  94. ObjectManager.Awake(component.Id, p1, p2);
  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 = (K)Activator.CreateInstance(typeof(K));
  100. component.Owner = this;
  101. if (this.componentDict.ContainsKey(component.GetType()))
  102. {
  103. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  104. }
  105. if (this.components == null)
  106. {
  107. this.components = new HashSet<Component>();
  108. }
  109. this.components.Add(component);
  110. this.componentDict.Add(component.GetType(), component);
  111. ObjectManager.Awake(component.Id, p1, p2, p3);
  112. return component;
  113. }
  114. public void AddComponent(Component component)
  115. {
  116. if (this.componentDict.ContainsKey(component.GetType()))
  117. {
  118. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {component.GetType().Name}");
  119. }
  120. if (this.components == null)
  121. {
  122. this.components = new HashSet<Component>();
  123. }
  124. this.components.Add(component);
  125. this.componentDict.Add(component.GetType(), component);
  126. ObjectManager.Awake(component.Id);
  127. }
  128. public void RemoveComponent<K>() where K : Component
  129. {
  130. Component component;
  131. if (!this.componentDict.TryGetValue(typeof (K), out component))
  132. {
  133. return;
  134. }
  135. this.components.Remove(component);
  136. this.componentDict.Remove(typeof (K));
  137. if (this.components.Count == 0)
  138. {
  139. this.components = null;
  140. }
  141. component.Dispose();
  142. }
  143. public K GetComponent<K>() where K : Component
  144. {
  145. Component component;
  146. if (!this.componentDict.TryGetValue(typeof (K), out component))
  147. {
  148. return default(K);
  149. }
  150. return (K) component;
  151. }
  152. public Component[] GetComponents()
  153. {
  154. return components.ToArray();
  155. }
  156. public override void BeginInit()
  157. {
  158. base.BeginInit();
  159. this.components = new HashSet<Component>();
  160. this.componentDict = new Dictionary<Type, Component>();
  161. }
  162. public override void EndInit()
  163. {
  164. base.EndInit();
  165. if (this.components.Count == 0)
  166. {
  167. this.components = null;
  168. return;
  169. }
  170. foreach (Component component in this.components)
  171. {
  172. component.Owner = this;
  173. this.componentDict.Add(component.GetType(), component);
  174. }
  175. }
  176. }
  177. }