Entity.cs 5.4 KB

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