Entity.cs 3.4 KB

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