Entity.cs 4.9 KB

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