Entity.cs 4.7 KB

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