BuffComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using Common.Base;
  4. using MongoDB.Bson;
  5. using MongoDB.Bson.Serialization.Attributes;
  6. namespace Model
  7. {
  8. public class BuffComponent: Component<Unit>
  9. {
  10. [BsonElement]
  11. private HashSet<Buff> buffs;
  12. [BsonIgnore]
  13. private Dictionary<ObjectId, Buff> idBuff;
  14. [BsonIgnore]
  15. private MultiMap<BuffType, Buff> typeBuff;
  16. public BuffComponent()
  17. {
  18. this.buffs = new HashSet<Buff>();
  19. this.idBuff = new Dictionary<ObjectId, Buff>();
  20. this.typeBuff = new MultiMap<BuffType, Buff>();
  21. }
  22. public override void BeginInit()
  23. {
  24. base.BeginInit();
  25. this.buffs = new HashSet<Buff>();
  26. this.idBuff = new Dictionary<ObjectId, Buff>();
  27. this.typeBuff = new MultiMap<BuffType, Buff>();
  28. }
  29. public override void EndInit()
  30. {
  31. base.EndInit();
  32. foreach (Buff buff in this.buffs)
  33. {
  34. this.idBuff.Add(buff.Id, buff);
  35. this.typeBuff.Add(buff.Config.Type, buff);
  36. }
  37. }
  38. public void Add(Buff buff)
  39. {
  40. if (this.buffs.Contains(buff))
  41. {
  42. throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}",
  43. buff.Id, buff.Config.Id));
  44. }
  45. if (this.idBuff.ContainsKey(buff.Id))
  46. {
  47. throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}",
  48. buff.Id, buff.Config.Id));
  49. }
  50. Env env = new Env();
  51. env[EnvKey.Owner] = this.Owner;
  52. env[EnvKey.Buff] = buff;
  53. World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.BeforeAddBuff, env);
  54. this.buffs.Add(buff);
  55. this.idBuff.Add(buff.Id, buff);
  56. this.typeBuff.Add(buff.Config.Type, buff);
  57. World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.AfterAddBuff, env);
  58. }
  59. public Buff GetById(ObjectId id)
  60. {
  61. if (!this.idBuff.ContainsKey(id))
  62. {
  63. return null;
  64. }
  65. return this.idBuff[id];
  66. }
  67. public Buff GetOneByType(BuffType type)
  68. {
  69. return this.typeBuff.GetOne(type);
  70. }
  71. public Buff[] GetByType(BuffType type)
  72. {
  73. return this.typeBuff.GetAll(type);
  74. }
  75. private void Remove(Buff buff)
  76. {
  77. if (buff == null)
  78. {
  79. return;
  80. }
  81. Env env = new Env();
  82. env[EnvKey.Owner] = this.Owner;
  83. env[EnvKey.Buff] = buff;
  84. World.Instance.GetComponent<EventComponent<EventAttribute>>()
  85. .Run(EventType.BeforeRemoveBuff, env);
  86. this.buffs.Remove(buff);
  87. this.idBuff.Remove(buff.Id);
  88. this.typeBuff.Remove(buff.Config.Type, buff);
  89. World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.AfterRemoveBuff, env);
  90. buff.Dispose();
  91. }
  92. public void RemoveById(ObjectId id)
  93. {
  94. Buff buff = this.GetById(id);
  95. this.Remove(buff);
  96. }
  97. public void RemoveByType(BuffType type)
  98. {
  99. Buff[] allbuffs = this.GetByType(type);
  100. foreach (Buff buff in allbuffs)
  101. {
  102. this.Remove(buff);
  103. }
  104. }
  105. }
  106. }