BuffComponent.cs 4.4 KB

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