Entity.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Model;
  5. using MongoDB.Bson.Serialization.Attributes;
  6. namespace Hotfix
  7. {
  8. [BsonIgnoreExtraElements]
  9. public class Entity : Component
  10. {
  11. [BsonElement]
  12. [BsonIgnoreIfNull]
  13. private readonly HashSet<Component> components;
  14. [BsonIgnore]
  15. private readonly Dictionary<Type, Component> componentDict;
  16. protected Entity()
  17. {
  18. this.Id = IdGenerater.GenerateId();
  19. this.components = new HashSet<Component>();
  20. this.componentDict = new Dictionary<Type, Component>();
  21. }
  22. protected Entity(long id)
  23. {
  24. this.Id = id;
  25. this.components = new HashSet<Component>();
  26. this.componentDict = new Dictionary<Type, Component>();
  27. }
  28. public override void Dispose()
  29. {
  30. if (this.IsDisposed)
  31. {
  32. return;
  33. }
  34. base.Dispose();
  35. foreach (Component component in this.GetComponents())
  36. {
  37. try
  38. {
  39. component.Dispose();
  40. }
  41. catch (Exception e)
  42. {
  43. Log.Error(e.ToString());
  44. }
  45. }
  46. this.components.Clear();
  47. this.componentDict.Clear();
  48. }
  49. public K AddComponent<K>() where K : Component, new()
  50. {
  51. K component = ComponentFactory.CreateWithParent<K>(this);
  52. if (this.componentDict.ContainsKey(component.GetType()))
  53. {
  54. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  55. }
  56. if (component is ISerializeToEntity)
  57. {
  58. this.components.Add(component);
  59. }
  60. this.componentDict.Add(component.GetType(), component);
  61. return component;
  62. }
  63. public K AddComponent<K, P1>(P1 p1) where K : Component, new()
  64. {
  65. K component = ComponentFactory.CreateWithParent<K, P1>(this, p1);
  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 (component is ISerializeToEntity)
  71. {
  72. this.components.Add(component);
  73. }
  74. this.componentDict.Add(component.GetType(), component);
  75. return component;
  76. }
  77. public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
  78. {
  79. K component = ComponentFactory.CreateWithParent<K, P1, P2>(this, p1, p2);
  80. if (this.componentDict.ContainsKey(component.GetType()))
  81. {
  82. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  83. }
  84. if (component is ISerializeToEntity)
  85. {
  86. this.components.Add(component);
  87. }
  88. this.componentDict.Add(component.GetType(), component);
  89. return component;
  90. }
  91. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
  92. {
  93. K component = ComponentFactory.CreateWithParent<K, P1, P2, P3>(this, p1, p2, p3);
  94. if (this.componentDict.ContainsKey(component.GetType()))
  95. {
  96. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
  97. }
  98. if (component is ISerializeToEntity)
  99. {
  100. this.components.Add(component);
  101. }
  102. this.componentDict.Add(component.GetType(), component);
  103. return component;
  104. }
  105. public void RemoveComponent<K>() where K : Component
  106. {
  107. Component component;
  108. if (!this.componentDict.TryGetValue(typeof(K), out component))
  109. {
  110. return;
  111. }
  112. this.components.Remove(component);
  113. this.componentDict.Remove(typeof(K));
  114. component.Dispose();
  115. }
  116. public void RemoveComponent(Type type)
  117. {
  118. Component component;
  119. if (!this.componentDict.TryGetValue(type, out component))
  120. {
  121. return;
  122. }
  123. this.components?.Remove(component);
  124. this.componentDict.Remove(type);
  125. component.Dispose();
  126. }
  127. public K GetComponent<K>() where K : Component
  128. {
  129. Component component;
  130. if (!this.componentDict.TryGetValue(typeof(K), out component))
  131. {
  132. return default(K);
  133. }
  134. return (K)component;
  135. }
  136. public Component[] GetComponents()
  137. {
  138. return this.componentDict.Values.ToArray();
  139. }
  140. }
  141. }