Entity.cs 2.9 KB

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