Entity.cs 4.2 KB

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