Entity.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MongoDB.Bson.Serialization.Attributes;
  5. namespace Model
  6. {
  7. public class Entity: Disposer
  8. {
  9. public EntityType Type { get; set; }
  10. [BsonElement]
  11. private HashSet<Component> components = new HashSet<Component>();
  12. [BsonIgnore]
  13. private readonly Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  14. protected Entity()
  15. {
  16. this.Type = EntityType.None;
  17. }
  18. protected Entity(EntityType entityType)
  19. {
  20. this.Type = entityType;
  21. }
  22. protected Entity(long id, EntityType entityType): base(id)
  23. {
  24. this.Type = entityType;
  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. }
  45. public K AddComponent<K>() where K : Component, new()
  46. {
  47. K component = (K) Activator.CreateInstance(typeof (K));
  48. component.Owner = this;
  49. if (this.componentDict.ContainsKey(component.GetType()))
  50. {
  51. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  52. }
  53. if (this.components == null)
  54. {
  55. this.components = new HashSet<Component>();
  56. }
  57. this.components.Add(component);
  58. this.componentDict.Add(component.GetType(), component);
  59. IAwake awake = component as IAwake;
  60. awake?.Awake();
  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. IAwake<P1> awake = component as IAwake<P1>;
  78. awake?.Awake(p1);
  79. return component;
  80. }
  81. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  82. {
  83. K component = (K) Activator.CreateInstance(typeof (K));
  84. component.Owner = this;
  85. if (this.componentDict.ContainsKey(component.GetType()))
  86. {
  87. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  88. }
  89. if (this.components == null)
  90. {
  91. this.components = new HashSet<Component>();
  92. }
  93. this.components.Add(component);
  94. this.componentDict.Add(component.GetType(), component);
  95. IAwake<P1, P2> awake = component as IAwake<P1, P2>;
  96. awake?.Awake(p1, p2);
  97. return component;
  98. }
  99. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  100. {
  101. K component = (K) Activator.CreateInstance(typeof (K));
  102. component.Owner = this;
  103. if (this.componentDict.ContainsKey(component.GetType()))
  104. {
  105. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  106. }
  107. if (this.components == null)
  108. {
  109. this.components = new HashSet<Component>();
  110. }
  111. this.components.Add(component);
  112. this.componentDict.Add(component.GetType(), component);
  113. IAwake<P1, P2, P3> awake = component as IAwake<P1, P2, P3>;
  114. awake?.Awake(p1, p2, p3);
  115. return component;
  116. }
  117. public void RemoveComponent<K>() where K : Component
  118. {
  119. Component component;
  120. if (!this.componentDict.TryGetValue(typeof (K), out component))
  121. {
  122. return;
  123. }
  124. this.components.Remove(component);
  125. this.componentDict.Remove(typeof (K));
  126. if (this.components.Count == 0)
  127. {
  128. this.components = null;
  129. }
  130. component.Dispose();
  131. }
  132. public void RemoveComponent(Type type)
  133. {
  134. Component component;
  135. if (!this.componentDict.TryGetValue(type, out component))
  136. {
  137. return;
  138. }
  139. this.components.Remove(component);
  140. this.componentDict.Remove(type);
  141. if (this.components.Count == 0)
  142. {
  143. this.components = null;
  144. }
  145. component.Dispose();
  146. }
  147. public K GetComponent<K>() where K : Component
  148. {
  149. Component component;
  150. if (!this.componentDict.TryGetValue(typeof (K), out component))
  151. {
  152. return default(K);
  153. }
  154. return (K) component;
  155. }
  156. public Component[] GetComponents()
  157. {
  158. return components.ToArray();
  159. }
  160. }
  161. }