BuffComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. }
  38. }
  39. public void Add(Buff buff)
  40. {
  41. if (this.buffs.Contains(buff))
  42. {
  43. throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}",
  44. buff.Id, buff.Config.Id));
  45. }
  46. if (this.idBuff.ContainsKey(buff.Id))
  47. {
  48. throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}",
  49. buff.Id, buff.Config.Id));
  50. }
  51. Env env = new Env();
  52. env[EnvKey.Owner] = this.Owner;
  53. env[EnvKey.Buff] = buff;
  54. World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.BeforeAddBuff, env);
  55. this.buffs.Add(buff);
  56. this.idBuff.Add(buff.Id, buff);
  57. this.typeBuff.Add(buff.Config.Type, buff);
  58. World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.AfterAddBuff, env);
  59. }
  60. public Buff GetById(ObjectId id)
  61. {
  62. if (!this.idBuff.ContainsKey(id))
  63. {
  64. return null;
  65. }
  66. return this.idBuff[id];
  67. }
  68. public Buff GetOneByType(BuffType type)
  69. {
  70. return this.typeBuff.GetOne(type);
  71. }
  72. public Buff[] GetByType(BuffType type)
  73. {
  74. return this.typeBuff.GetAll(type);
  75. }
  76. private void Remove(Buff buff)
  77. {
  78. if (buff == null)
  79. {
  80. return;
  81. }
  82. Env env = new Env();
  83. env[EnvKey.Owner] = this.Owner;
  84. env[EnvKey.Buff] = buff;
  85. World.Instance.GetComponent<EventComponent<EventAttribute>>()
  86. .Run(EventType.BeforeRemoveBuff, env);
  87. this.buffs.Remove(buff);
  88. this.idBuff.Remove(buff.Id);
  89. this.typeBuff.Remove(buff.Config.Type, buff);
  90. World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.AfterRemoveBuff, env);
  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. }