Entity.cs 5.4 KB

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