Entity.cs 3.7 KB

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