BuffComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using Common.Base;
  4. using MongoDB.Bson;
  5. using MongoDB.Bson.Serialization.Attributes;
  6. #pragma warning disable 4014
  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 (Buff buff in this.buffs)
  34. {
  35. this.idBuff.Add(buff.Id, buff);
  36. this.typeBuff.Add(buff.Config.Type, buff);
  37. }
  38. }
  39. public void Add(Buff buff)
  40. {
  41. if (this.buffs.Contains(buff))
  42. {
  43. throw new ArgumentException($"already exist same buff, Id: {buff.Id} ConfigId: {buff.Config.Id}");
  44. }
  45. if (this.idBuff.ContainsKey(buff.Id))
  46. {
  47. throw new ArgumentException($"already exist same buff, Id: {buff.Id} ConfigId: {buff.Config.Id}");
  48. }
  49. Env env = new Env();
  50. env[EnvKey.Owner] = this.Owner;
  51. env[EnvKey.Buff] = buff;
  52. World.Instance.GetComponent<EventComponent<EventAttribute>>().RunAsync(EventType.BeforeAddBuff, env);
  53. this.buffs.Add(buff);
  54. this.idBuff.Add(buff.Id, buff);
  55. this.typeBuff.Add(buff.Config.Type, buff);
  56. World.Instance.GetComponent<EventComponent<EventAttribute>>().RunAsync(EventType.AfterAddBuff, env);
  57. }
  58. public Buff GetById(ObjectId id)
  59. {
  60. if (!this.idBuff.ContainsKey(id))
  61. {
  62. return null;
  63. }
  64. return this.idBuff[id];
  65. }
  66. public Buff GetOneByType(BuffType type)
  67. {
  68. return this.typeBuff.GetOne(type);
  69. }
  70. public Buff[] GetByType(BuffType type)
  71. {
  72. return this.typeBuff.GetAll(type);
  73. }
  74. private void Remove(Buff buff)
  75. {
  76. if (buff == null)
  77. {
  78. return;
  79. }
  80. Env env = new Env();
  81. env[EnvKey.Owner] = this.Owner;
  82. env[EnvKey.Buff] = buff;
  83. World.Instance.GetComponent<EventComponent<EventAttribute>>()
  84. .RunAsync(EventType.BeforeRemoveBuff, env);
  85. this.buffs.Remove(buff);
  86. this.idBuff.Remove(buff.Id);
  87. this.typeBuff.Remove(buff.Config.Type, buff);
  88. World.Instance.GetComponent<EventComponent<EventAttribute>>().RunAsync(EventType.AfterRemoveBuff, env);
  89. buff.Dispose();
  90. }
  91. public void RemoveById(ObjectId id)
  92. {
  93. Buff buff = this.GetById(id);
  94. this.Remove(buff);
  95. }
  96. public void RemoveByType(BuffType type)
  97. {
  98. Buff[] allbuffs = this.GetByType(type);
  99. foreach (Buff buff in allbuffs)
  100. {
  101. this.Remove(buff);
  102. }
  103. }
  104. }
  105. }