Entity.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. [BsonElement]
  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. }
  16. protected Entity(long id): base(id)
  17. {
  18. }
  19. public override void Dispose()
  20. {
  21. if (this.Id == 0)
  22. {
  23. return;
  24. }
  25. base.Dispose();
  26. foreach (Component component in this.GetComponents())
  27. {
  28. try
  29. {
  30. component.Dispose();
  31. }
  32. catch (Exception e)
  33. {
  34. Log.Error(e.ToString());
  35. }
  36. }
  37. }
  38. public K AddComponent<K>() where K : Component, new()
  39. {
  40. K component = (K) Activator.CreateInstance(typeof (K));
  41. component.Owner = this;
  42. if (this.componentDict.ContainsKey(component.GetType()))
  43. {
  44. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  45. }
  46. if (this.components == null)
  47. {
  48. this.components = new HashSet<Component>();
  49. }
  50. this.components.Add(component);
  51. this.componentDict.Add(component.GetType(), component);
  52. IAwake awake = component as IAwake;
  53. awake?.Awake();
  54. return component;
  55. }
  56. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  57. {
  58. K component = (K) Activator.CreateInstance(typeof (K));
  59. component.Owner = this;
  60. if (this.componentDict.ContainsKey(component.GetType()))
  61. {
  62. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  63. }
  64. if (this.components == null)
  65. {
  66. this.components = new HashSet<Component>();
  67. }
  68. this.components.Add(component);
  69. this.componentDict.Add(component.GetType(), component);
  70. IAwake<P1> awake = component as IAwake<P1>;
  71. awake?.Awake(p1);
  72. return component;
  73. }
  74. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  75. {
  76. K component = (K) Activator.CreateInstance(typeof (K));
  77. component.Owner = this;
  78. if (this.componentDict.ContainsKey(component.GetType()))
  79. {
  80. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  81. }
  82. if (this.components == null)
  83. {
  84. this.components = new HashSet<Component>();
  85. }
  86. this.components.Add(component);
  87. this.componentDict.Add(component.GetType(), component);
  88. IAwake<P1, P2> awake = component as IAwake<P1, P2>;
  89. awake?.Awake(p1, p2);
  90. return component;
  91. }
  92. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  93. {
  94. K component = (K) Activator.CreateInstance(typeof (K));
  95. component.Owner = this;
  96. if (this.componentDict.ContainsKey(component.GetType()))
  97. {
  98. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  99. }
  100. if (this.components == null)
  101. {
  102. this.components = new HashSet<Component>();
  103. }
  104. this.components.Add(component);
  105. this.componentDict.Add(component.GetType(), component);
  106. IAwake<P1, P2, P3> awake = component as IAwake<P1, P2, P3>;
  107. awake?.Awake(p1, p2, p3);
  108. return component;
  109. }
  110. public void RemoveComponent<K>() where K : Component
  111. {
  112. Component component;
  113. if (!this.componentDict.TryGetValue(typeof (K), out component))
  114. {
  115. return;
  116. }
  117. this.components.Remove(component);
  118. this.componentDict.Remove(typeof (K));
  119. if (this.components.Count == 0)
  120. {
  121. this.components = null;
  122. }
  123. component.Dispose();
  124. }
  125. public void RemoveComponent(Type type)
  126. {
  127. Component component;
  128. if (!this.componentDict.TryGetValue(type, out component))
  129. {
  130. return;
  131. }
  132. this.components.Remove(component);
  133. this.componentDict.Remove(type);
  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. }
  154. }