Entity.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  14. public Entity(string entityType)
  15. {
  16. this.Type = entityType;
  17. ObjectManager.Add(this);
  18. }
  19. public Entity(long id, string entityType) : base(id)
  20. {
  21. this.Type = entityType;
  22. ObjectManager.Add(this);
  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. ObjectManager.Remove(this.Id);
  43. }
  44. public K AddComponent<K>() where K : Component, new()
  45. {
  46. K component = (K) Activator.CreateInstance(typeof (K));
  47. component.Owner = this;
  48. if (this.componentDict.ContainsKey(component.GetType()))
  49. {
  50. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  51. }
  52. if (this.components == null)
  53. {
  54. this.components = new HashSet<Component>();
  55. }
  56. this.components.Add(component);
  57. this.componentDict.Add(component.GetType(), component);
  58. ObjectManager.Awake(component.Id);
  59. return component;
  60. }
  61. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  62. {
  63. K component = (K)Activator.CreateInstance(typeof(K));
  64. component.Owner = this;
  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 (this.components == null)
  70. {
  71. this.components = new HashSet<Component>();
  72. }
  73. this.components.Add(component);
  74. this.componentDict.Add(component.GetType(), component);
  75. ObjectManager.Awake(component.Id, p1);
  76. return component;
  77. }
  78. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  79. {
  80. K component = (K)Activator.CreateInstance(typeof(K));
  81. component.Owner = this;
  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. this.components.Add(component);
  91. this.componentDict.Add(component.GetType(), component);
  92. ObjectManager.Awake(component.Id, p1, p2);
  93. return component;
  94. }
  95. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  96. {
  97. K component = (K)Activator.CreateInstance(typeof(K));
  98. component.Owner = this;
  99. if (this.componentDict.ContainsKey(component.GetType()))
  100. {
  101. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  102. }
  103. if (this.components == null)
  104. {
  105. this.components = new HashSet<Component>();
  106. }
  107. this.components.Add(component);
  108. this.componentDict.Add(component.GetType(), component);
  109. ObjectManager.Awake(component.Id, p1, p2, p3);
  110. return component;
  111. }
  112. public void AddComponent(Component component)
  113. {
  114. if (this.componentDict.ContainsKey(component.GetType()))
  115. {
  116. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {component.GetType().Name}");
  117. }
  118. if (this.components == null)
  119. {
  120. this.components = new HashSet<Component>();
  121. }
  122. this.components.Add(component);
  123. this.componentDict.Add(component.GetType(), component);
  124. ObjectManager.Awake(component.Id);
  125. }
  126. public void RemoveComponent<K>() where K : Component
  127. {
  128. Component component;
  129. if (!this.componentDict.TryGetValue(typeof (K), out component))
  130. {
  131. return;
  132. }
  133. this.components.Remove(component);
  134. this.componentDict.Remove(typeof (K));
  135. if (this.components.Count == 0)
  136. {
  137. this.components = null;
  138. }
  139. component.Dispose();
  140. }
  141. public K GetComponent<K>() where K : Component
  142. {
  143. Component component;
  144. if (!this.componentDict.TryGetValue(typeof (K), out component))
  145. {
  146. return default(K);
  147. }
  148. return (K) component;
  149. }
  150. public Component[] GetComponents()
  151. {
  152. return components.ToArray();
  153. }
  154. public override void BeginInit()
  155. {
  156. base.BeginInit();
  157. this.components = new HashSet<Component>();
  158. this.componentDict = new Dictionary<Type, Component>();
  159. }
  160. public override void EndInit()
  161. {
  162. base.EndInit();
  163. if (this.components.Count == 0)
  164. {
  165. this.components = null;
  166. return;
  167. }
  168. foreach (Component component in this.components)
  169. {
  170. component.Owner = this;
  171. this.componentDict.Add(component.GetType(), component);
  172. }
  173. }
  174. }
  175. }