BuffComponent.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. private Dictionary<ObjectId, Buff> idBuff;
  14. private MultiMap<BuffType, Buff> typeBuff;
  15. public BuffComponent()
  16. {
  17. this.buffs = new HashSet<Buff>();
  18. this.idBuff = new Dictionary<ObjectId, Buff>();
  19. this.typeBuff = new MultiMap<BuffType, Buff>();
  20. }
  21. public override void BeginInit()
  22. {
  23. base.BeginInit();
  24. this.buffs = new HashSet<Buff>();
  25. this.idBuff = new Dictionary<ObjectId, Buff>();
  26. this.typeBuff = new MultiMap<BuffType, Buff>();
  27. }
  28. public override void EndInit()
  29. {
  30. base.EndInit();
  31. foreach (var buff in this.buffs)
  32. {
  33. this.idBuff.Add(buff.Id, buff);
  34. this.typeBuff.Add(buff.Config.Type, buff);
  35. }
  36. }
  37. public void Add(Buff buff)
  38. {
  39. if (this.buffs.Contains(buff))
  40. {
  41. throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
  42. }
  43. if (this.idBuff.ContainsKey(buff.Id))
  44. {
  45. throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
  46. }
  47. Env env = new Env();
  48. env[EnvKey.Owner] = this.Owner;
  49. env[EnvKey.Buff] = buff;
  50. World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.BeforeAddBuff, env);
  51. this.buffs.Add(buff);
  52. this.idBuff.Add(buff.Id, buff);
  53. this.typeBuff.Add(buff.Config.Type, buff);
  54. World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.AfterAddBuff, env);
  55. }
  56. public Buff GetById(ObjectId id)
  57. {
  58. if (!this.idBuff.ContainsKey(id))
  59. {
  60. return null;
  61. }
  62. return this.idBuff[id];
  63. }
  64. public Buff GetOneByType(BuffType type)
  65. {
  66. return this.typeBuff.GetOne(type);
  67. }
  68. public Buff[] GetByType(BuffType type)
  69. {
  70. return this.typeBuff.GetByKey(type);
  71. }
  72. private bool Remove(Buff buff)
  73. {
  74. if (buff == null)
  75. {
  76. return false;
  77. }
  78. Env env = new Env();
  79. env[EnvKey.Owner] = this.Owner;
  80. env[EnvKey.Buff] = buff;
  81. World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.BeforeRemoveBuff, env);
  82. this.buffs.Remove(buff);
  83. this.idBuff.Remove(buff.Id);
  84. this.typeBuff.Remove(buff.Config.Type, buff);
  85. World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.AfterRemoveBuff, env);
  86. return true;
  87. }
  88. public bool RemoveById(ObjectId id)
  89. {
  90. Buff buff = this.GetById(id);
  91. return this.Remove(buff);
  92. }
  93. public void RemoveByType(BuffType type)
  94. {
  95. Buff[] allbuffs = this.GetByType(type);
  96. foreach (Buff buff in allbuffs)
  97. {
  98. this.Remove(buff);
  99. }
  100. }
  101. }
  102. }