BuffComponentExtension.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using Common.Event;
  4. using Model;
  5. using MongoDB.Bson;
  6. namespace Controller
  7. {
  8. /// <summary>
  9. /// 控制复杂的buff逻辑,可以reload
  10. /// </summary>
  11. public static class BuffComponentExtension
  12. {
  13. public static void Add(this BuffComponent buffComponent, Buff buff)
  14. {
  15. if (buffComponent.buffs.Contains(buff))
  16. {
  17. throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
  18. }
  19. if (buffComponent.idBuff.ContainsKey(buff.Id))
  20. {
  21. throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
  22. }
  23. Env env = new Env();
  24. env[EnvKey.Unit] = buffComponent.Owner;
  25. env[EnvKey.Buff] = buff;
  26. World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.BeforeAddBuff, env);
  27. buffComponent.buffs.Add(buff);
  28. buffComponent.idBuff.Add(buff.Id, buff);
  29. buffComponent.typeBuff.Add(buff.Config.Type, buff);
  30. World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.AfterAddBuff, env);
  31. }
  32. public static Buff GetById(this BuffComponent buffComponent, ObjectId id)
  33. {
  34. if (!buffComponent.idBuff.ContainsKey(id))
  35. {
  36. return null;
  37. }
  38. return buffComponent.idBuff[id];
  39. }
  40. public static Buff GetOneByType(this BuffComponent buffComponent, BuffType type)
  41. {
  42. return buffComponent.typeBuff.GetOne(type);
  43. }
  44. public static Buff[] GetByType(this BuffComponent buffComponent, BuffType type)
  45. {
  46. return buffComponent.typeBuff.GetByKey(type);
  47. }
  48. private static bool Remove(this BuffComponent buffComponent, Buff buff)
  49. {
  50. if (buff == null)
  51. {
  52. return false;
  53. }
  54. Env env = new Env();
  55. env[EnvKey.Unit] = buffComponent.Owner;
  56. env[EnvKey.Buff] = buff;
  57. World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.BeforeRemoveBuff, env);
  58. buffComponent.buffs.Remove(buff);
  59. buffComponent.idBuff.Remove(buff.Id);
  60. buffComponent.typeBuff.Remove(buff.Config.Type, buff);
  61. World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.AfterRemoveBuff, env);
  62. return true;
  63. }
  64. public static bool RemoveById(this BuffComponent buffComponent, ObjectId id)
  65. {
  66. Buff buff = buffComponent.GetById(id);
  67. return buffComponent.Remove(buff);
  68. }
  69. public static void RemoveByType(this BuffComponent buffComponent, BuffType type)
  70. {
  71. Buff[] allbuffs = buffComponent.GetByType(type);
  72. foreach (Buff buff in allbuffs)
  73. {
  74. buffComponent.Remove(buff);
  75. }
  76. }
  77. }
  78. }