Entity.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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(
  31. string.Format("AddComponent, component already exist, id: {0}, component: {1}", this.Id,
  32. typeof (K).Name));
  33. }
  34. if (this.components == null)
  35. {
  36. this.components = new HashSet<Component<T>>();
  37. }
  38. this.components.Add(component);
  39. this.componentDict.Add(component.GetComponentType(), component);
  40. return component;
  41. }
  42. public void AddComponent(Component<T> component)
  43. {
  44. if (this.componentDict.ContainsKey(component.GetComponentType()))
  45. {
  46. throw new Exception(
  47. string.Format("AddComponent, component already exist, id: {0}, component: {1}", this.Id,
  48. component.GetComponentType().Name));
  49. }
  50. if (this.components == null)
  51. {
  52. this.components = new HashSet<Component<T>>();
  53. }
  54. this.components.Add(component);
  55. this.componentDict.Add(component.GetComponentType(), component);
  56. }
  57. public void RemoveComponent<K>() where K : Component<T>
  58. {
  59. Component<T> component;
  60. if (!this.componentDict.TryGetValue(typeof (K), out component))
  61. {
  62. throw new Exception(
  63. string.Format("RemoveComponent, component not exist, id: {0}, component: {1}", this.Id,
  64. typeof (K).Name));
  65. }
  66. this.components.Remove(component);
  67. this.componentDict.Remove(typeof (K));
  68. if (this.components.Count == 0)
  69. {
  70. this.components = null;
  71. }
  72. }
  73. public K GetComponent<K>() where K : Component<T>
  74. {
  75. Component<T> component;
  76. if (!this.componentDict.TryGetValue(typeof (K), out component))
  77. {
  78. return default (K);
  79. }
  80. return (K) component;
  81. }
  82. public Component<T>[] GetComponents()
  83. {
  84. return this.components.ToArray();
  85. }
  86. public override void BeginInit()
  87. {
  88. base.BeginInit();
  89. this.components = new HashSet<Component<T>>();
  90. this.componentDict = new Dictionary<Type, Component<T>>();
  91. }
  92. public override void EndInit()
  93. {
  94. base.EndInit();
  95. if (this.components.Count == 0)
  96. {
  97. this.components = null;
  98. return;
  99. }
  100. foreach (Component<T> component in this.components)
  101. {
  102. component.Owner = (T) this;
  103. this.componentDict.Add(component.GetComponentType(), component);
  104. }
  105. }
  106. }
  107. }