Entity.cs 4.8 KB

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