Entity.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Model;
  5. namespace Hotfix
  6. {
  7. public class Entity: Disposer
  8. {
  9. public EntityType Type { get; set; }
  10. private HashSet<Component> components = new HashSet<Component>();
  11. private readonly Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  12. protected Entity()
  13. {
  14. this.Type = EntityType.None;
  15. }
  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. IAwake awake = component as IAwake;
  58. awake?.Awake();
  59. return component;
  60. }
  61. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  62. {
  63. K component = (K) Activator.CreateInstance(typeof (K));
  64. component.Owner = this;
  65. if (this.componentDict.ContainsKey(component.GetType()))
  66. {
  67. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  68. }
  69. if (this.components == null)
  70. {
  71. this.components = new HashSet<Component>();
  72. }
  73. this.components.Add(component);
  74. this.componentDict.Add(component.GetType(), component);
  75. IAwake<P1> awake = component as IAwake<P1>;
  76. awake?.Awake(p1);
  77. return component;
  78. }
  79. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  80. {
  81. K component = (K) Activator.CreateInstance(typeof (K));
  82. component.Owner = this;
  83. if (this.componentDict.ContainsKey(component.GetType()))
  84. {
  85. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  86. }
  87. if (this.components == null)
  88. {
  89. this.components = new HashSet<Component>();
  90. }
  91. this.components.Add(component);
  92. this.componentDict.Add(component.GetType(), component);
  93. IAwake<P1, P2> awake = component as IAwake<P1, P2>;
  94. awake?.Awake(p1, p2);
  95. return component;
  96. }
  97. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  98. {
  99. K component = (K) Activator.CreateInstance(typeof (K));
  100. component.Owner = this;
  101. if (this.componentDict.ContainsKey(component.GetType()))
  102. {
  103. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  104. }
  105. if (this.components == null)
  106. {
  107. this.components = new HashSet<Component>();
  108. }
  109. this.components.Add(component);
  110. this.componentDict.Add(component.GetType(), component);
  111. IAwake<P1, P2, P3> awake = component as IAwake<P1, P2, P3>;
  112. awake?.Awake(p1, p2, p3);
  113. return component;
  114. }
  115. public void RemoveComponent<K>() where K : Component
  116. {
  117. if (!this.componentDict.TryGetValue(typeof(K), out Component 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. if (!this.componentDict.TryGetValue(type, out Component component))
  132. {
  133. return;
  134. }
  135. this.components.Remove(component);
  136. this.componentDict.Remove(type);
  137. if (this.components.Count == 0)
  138. {
  139. this.components = null;
  140. }
  141. component.Dispose();
  142. }
  143. public K GetComponent<K>() where K : Component
  144. {
  145. if (!this.componentDict.TryGetValue(typeof(K), out Component component))
  146. {
  147. return default(K);
  148. }
  149. return (K) component;
  150. }
  151. public Component[] GetComponents()
  152. {
  153. return components.ToArray();
  154. }
  155. }
  156. }