Entity.cs 2.8 KB

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