Entity.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Common.Helper;
  5. using MongoDB.Bson;
  6. using MongoDB.Bson.Serialization.Attributes;
  7. namespace Common.Base
  8. {
  9. public abstract class Entity<T>: Object where T : Entity<T>
  10. {
  11. [BsonElement, BsonIgnoreIfNull]
  12. private HashSet<Component<T>> components;
  13. private Dictionary<Type, Component<T>> componentDict = new Dictionary<Type, Component<T>>();
  14. protected Entity()
  15. {
  16. }
  17. protected Entity(ObjectId id): base(id)
  18. {
  19. }
  20. public T Clone()
  21. {
  22. return MongoHelper.FromBson<T>(MongoHelper.ToBson(this));
  23. }
  24. public K AddComponent<K>() where K : Component<T>, new()
  25. {
  26. K component = (K) Activator.CreateInstance(typeof (K));
  27. component.Owner = (T) this;
  28. if (this.componentDict.ContainsKey(component.GetComponentType()))
  29. {
  30. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
  31. }
  32. if (this.components == null)
  33. {
  34. this.components = new HashSet<Component<T>>();
  35. }
  36. this.components.Add(component);
  37. this.componentDict.Add(component.GetComponentType(), component);
  38. return component;
  39. }
  40. public void AddComponent(Component<T> component)
  41. {
  42. if (this.componentDict.ContainsKey(component.GetComponentType()))
  43. {
  44. throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {component.GetComponentType().Name}");
  45. }
  46. if (this.components == null)
  47. {
  48. this.components = new HashSet<Component<T>>();
  49. }
  50. this.components.Add(component);
  51. this.componentDict.Add(component.GetComponentType(), component);
  52. }
  53. public void RemoveComponent<K>() where K : Component<T>
  54. {
  55. Component<T> component;
  56. if (!this.componentDict.TryGetValue(typeof (K), out component))
  57. {
  58. throw new Exception($"RemoveComponent, component not exist, id: {this.Id}, component: {typeof (K).Name}");
  59. }
  60. this.components.Remove(component);
  61. this.componentDict.Remove(typeof (K));
  62. if (this.components.Count == 0)
  63. {
  64. this.components = null;
  65. }
  66. }
  67. public K GetComponent<K>() where K : Component<T>
  68. {
  69. Component<T> component;
  70. if (!this.componentDict.TryGetValue(typeof (K), out component))
  71. {
  72. return default (K);
  73. }
  74. return (K) component;
  75. }
  76. public Component<T>[] GetComponents()
  77. {
  78. return this.components.ToArray();
  79. }
  80. public override void BeginInit()
  81. {
  82. base.BeginInit();
  83. this.components = new HashSet<Component<T>>();
  84. this.componentDict = new Dictionary<Type, Component<T>>();
  85. }
  86. public override void EndInit()
  87. {
  88. base.EndInit();
  89. if (this.components.Count == 0)
  90. {
  91. this.components = null;
  92. return;
  93. }
  94. foreach (Component<T> component in this.components)
  95. {
  96. component.Owner = (T) this;
  97. this.componentDict.Add(component.GetComponentType(), component);
  98. }
  99. }
  100. }
  101. }