Entity.cs 4.7 KB

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