Buff.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using FL.Battle.Components;
  2. using System;
  3. using UnityEngine;
  4. using XGame.Database;
  5. using XGame.Framework.Asyncs;
  6. using XGame.Framework.Interfaces;
  7. using XGame.Framework.Time;
  8. namespace FL.Battle.Buffs
  9. {
  10. public class Buff : IBuff, IReference, IDisposable
  11. {
  12. protected IBuffListener Listener { get; private set; }
  13. protected IBuffsContext Context { get; private set; }
  14. public BuffTable Table { get; private set; }
  15. public int TableId => Table.Id;
  16. public long EntityId { get; private set; }
  17. public long OwnerId => Context.Entity.EntityId;
  18. public int Duration => Table.EndureTime;
  19. private int _layers;
  20. /// <summary>
  21. /// 叠加层数
  22. /// </summary>
  23. public int Layers
  24. {
  25. get => _layers;
  26. set
  27. {
  28. //var last = _layers;
  29. _layers = Mathf.Clamp(value, 1, LayerLimit);
  30. (_dieTimer as IReset)?.Reset();
  31. //if (last != _layers)
  32. // OnLayerChanged();
  33. }
  34. }
  35. /// <summary>
  36. /// 叠加层数上限
  37. /// </summary>
  38. public virtual int LayerLimit => Table.AddUp;
  39. public EBuffType BuffType => (EBuffType)Table.BuffType;
  40. /// <summary>
  41. /// buff结束定时器
  42. /// </summary>
  43. private ITimer _dieTimer;
  44. public EElementType ElementType => Table.Element;
  45. public string IconName => Table.Icon;
  46. private IAsync _vfxLoadAsync;
  47. private GameObject _vfxGo;
  48. public virtual bool IsShowIcon => true;
  49. public void Init(BuffTable table, long entityId, IBuffsContext context, IBuffListener listener)
  50. {
  51. Table = table;
  52. EntityId = entityId;
  53. Context = context;
  54. Listener = listener;
  55. _layers = 1;
  56. _dieTimer = context.Time.AddDelayTimer(Duration, () =>
  57. {
  58. Listener?.OnCompleted(TableId);
  59. });
  60. LoadBuffVfx();
  61. OnInited();
  62. }
  63. void IDisposable.Dispose()
  64. {
  65. InnerDispose();
  66. }
  67. void IReference.Clear()
  68. {
  69. InnerDispose();
  70. }
  71. private void InnerDispose()
  72. {
  73. OnDispose();
  74. _dieTimer?.Cancel();
  75. _dieTimer = null;
  76. UnloadBuffVfx();
  77. Table = null;
  78. Context = null;
  79. Listener = null;
  80. }
  81. protected virtual void OnInited()
  82. {
  83. }
  84. protected virtual void OnDispose()
  85. {
  86. }
  87. //protected virtual void OnLayerChanged()
  88. //{
  89. //}
  90. /// <summary>
  91. /// 加载buff配套特效
  92. /// </summary>
  93. private void LoadBuffVfx()
  94. {
  95. var vfxName = Table.BuffEffect;
  96. if (string.IsNullOrEmpty(vfxName))
  97. return;
  98. var loadAsync = Context.Asset.LoadAsync<GameObject>(vfxName);
  99. _vfxLoadAsync = loadAsync;
  100. loadAsync.On(_ =>
  101. {
  102. _vfxLoadAsync = null;
  103. var go = loadAsync.Result;
  104. if (go == null)
  105. return;
  106. go.transform.SetParent(Context.OwnerTr, false);
  107. if (BuffType == EBuffType.Freeze)
  108. { // 冰冻特效根据模型大小缩放
  109. go.transform.localScale = Vector3.one * Context.Entity.Attr.Radius * 2;
  110. }
  111. go.SetActive(true);
  112. _vfxGo = go;
  113. });
  114. }
  115. /// <summary>
  116. /// 释放特效
  117. /// </summary>
  118. private void UnloadBuffVfx()
  119. {
  120. _vfxLoadAsync?.RemoveAll();
  121. _vfxLoadAsync = null;
  122. if (_vfxGo != null)
  123. {
  124. Context.Asset.Recycle(_vfxGo);
  125. _vfxGo = null;
  126. }
  127. }
  128. }
  129. }