Entity.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Entity Parent { get; set; }
  10. private HashSet<Component> components = new HashSet<Component>();
  11. private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
  12. protected Entity()
  13. {
  14. this.Id = IdGenerater.GenerateId();
  15. }
  16. protected Entity(long id)
  17. {
  18. this.Id = id;
  19. }
  20. public override void Dispose()
  21. {
  22. if (this.Id == 0)
  23. {
  24. return;
  25. }
  26. base.Dispose();
  27. foreach (Component component in this.GetComponents())
  28. {
  29. try
  30. {
  31. component.Dispose();
  32. }
  33. catch (Exception e)
  34. {
  35. Log.Error(e.ToString());
  36. }
  37. }
  38. }
  39. public K AddComponent<K>() where K : Component, new()
  40. {
  41. K component = ComponentFactory.Create<K>(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. if (component is ISerializeToEntity)
  51. {
  52. this.components.Add(component);
  53. }
  54. this.componentDict.Add(component.GetType(), component);
  55. return component;
  56. }
  57. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  58. {
  59. K component = ComponentFactory.Create<K, P1>(this, p1);
  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. if (component is ISerializeToEntity)
  69. {
  70. this.components.Add(component);
  71. }
  72. this.componentDict.Add(component.GetType(), component);
  73. return component;
  74. }
  75. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  76. {
  77. K component = ComponentFactory.Create<K, P1, P2>(this, p1, p2);
  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. if (component is ISerializeToEntity)
  87. {
  88. this.components.Add(component);
  89. }
  90. this.componentDict.Add(component.GetType(), component);
  91. return component;
  92. }
  93. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  94. {
  95. K component = ComponentFactory.Create<K, P1, P2, P3>(this, p1, p2, p3);
  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. if (component is ISerializeToEntity)
  105. {
  106. this.components.Add(component);
  107. }
  108. this.componentDict.Add(component.GetType(), component);
  109. return component;
  110. }
  111. public void RemoveComponent<K>() where K : Component
  112. {
  113. Component component;
  114. if (!this.componentDict.TryGetValue(typeof(K), out component))
  115. {
  116. return;
  117. }
  118. this.components?.Remove(component);
  119. this.componentDict.Remove(typeof(K));
  120. if (this.components != null && this.components.Count == 0)
  121. {
  122. this.components = null;
  123. }
  124. component.Dispose();
  125. }
  126. public void RemoveComponent(Type type)
  127. {
  128. Component component;
  129. if (!this.componentDict.TryGetValue(type, out component))
  130. {
  131. return;
  132. }
  133. this.components?.Remove(component);
  134. this.componentDict.Remove(type);
  135. if (this.components != null && this.components.Count == 0)
  136. {
  137. this.components = null;
  138. }
  139. component.Dispose();
  140. }
  141. public K GetComponent<K>() where K : Component
  142. {
  143. Component component;
  144. if (!this.componentDict.TryGetValue(typeof(K), out component))
  145. {
  146. return default(K);
  147. }
  148. return (K)component;
  149. }
  150. public Component[] GetComponents()
  151. {
  152. return this.componentDict.Values.ToArray();
  153. }
  154. }
  155. }