Entity.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. private HashSet<Component> components = new HashSet<Component>();
  11. [BsonIgnore]
  12. private readonly Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  13. protected Entity()
  14. {
  15. this.Type = EntityType.None;
  16. }
  17. protected Entity(EntityType entityType)
  18. {
  19. this.Type = entityType;
  20. }
  21. protected Entity(long id, EntityType entityType): base(id)
  22. {
  23. this.Type = entityType;
  24. }
  25. public override void Dispose()
  26. {
  27. if (this.Id == 0)
  28. {
  29. return;
  30. }
  31. base.Dispose();
  32. foreach (Component component in this.GetComponents())
  33. {
  34. try
  35. {
  36. component.Dispose();
  37. }
  38. catch (Exception e)
  39. {
  40. Log.Error(e.ToString());
  41. }
  42. }
  43. }
  44. public K AddComponent<K>() where K : Component, new()
  45. {
  46. K component = (K) Activator.CreateInstance(typeof (K));
  47. component.Owner = this;
  48. if (this.componentDict.ContainsKey(component.GetType()))
  49. {
  50. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  51. }
  52. if (this.components == null)
  53. {
  54. this.components = new HashSet<Component>();
  55. }
  56. this.components.Add(component);
  57. this.componentDict.Add(component.GetType(), component);
  58. IAwake awake = component as IAwake;
  59. awake?.Awake();
  60. return component;
  61. }
  62. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  63. {
  64. K component = (K) Activator.CreateInstance(typeof (K));
  65. component.Owner = this;
  66. if (this.componentDict.ContainsKey(component.GetType()))
  67. {
  68. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  69. }
  70. if (this.components == null)
  71. {
  72. this.components = new HashSet<Component>();
  73. }
  74. this.components.Add(component);
  75. this.componentDict.Add(component.GetType(), component);
  76. IAwake<P1> awake = component as IAwake<P1>;
  77. awake?.Awake(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. IAwake<P1, P2> awake = component as IAwake<P1, P2>;
  95. awake?.Awake(p1, p2);
  96. return component;
  97. }
  98. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  99. {
  100. K component = (K) Activator.CreateInstance(typeof (K));
  101. component.Owner = this;
  102. if (this.componentDict.ContainsKey(component.GetType()))
  103. {
  104. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  105. }
  106. if (this.components == null)
  107. {
  108. this.components = new HashSet<Component>();
  109. }
  110. this.components.Add(component);
  111. this.componentDict.Add(component.GetType(), component);
  112. IAwake<P1, P2, P3> awake = component as IAwake<P1, P2, P3>;
  113. awake?.Awake(p1, p2, p3);
  114. return component;
  115. }
  116. public void RemoveComponent<K>() where K : Component
  117. {
  118. Component component;
  119. if (!this.componentDict.TryGetValue(typeof (K), out component))
  120. {
  121. return;
  122. }
  123. this.components.Remove(component);
  124. this.componentDict.Remove(typeof (K));
  125. if (this.components.Count == 0)
  126. {
  127. this.components = null;
  128. }
  129. component.Dispose();
  130. }
  131. public void RemoveComponent(Type type)
  132. {
  133. Component component;
  134. if (!this.componentDict.TryGetValue(type, out component))
  135. {
  136. return;
  137. }
  138. this.components.Remove(component);
  139. this.componentDict.Remove(type);
  140. if (this.components.Count == 0)
  141. {
  142. this.components = null;
  143. }
  144. component.Dispose();
  145. }
  146. public K GetComponent<K>() where K : Component
  147. {
  148. Component component;
  149. if (!this.componentDict.TryGetValue(typeof (K), out component))
  150. {
  151. return default(K);
  152. }
  153. return (K) component;
  154. }
  155. public Component[] GetComponents()
  156. {
  157. return components.ToArray();
  158. }
  159. }
  160. }