325 lines
8.5 KiB
C#
325 lines
8.5 KiB
C#
using System.Text;
|
||
using Framework;
|
||
using Gameplay.Common;
|
||
using Gameplay.Effect;
|
||
using Gameplay.Pool;
|
||
using Gameplay.Unit;
|
||
using UnityEngine;
|
||
|
||
namespace Gameplay.Buff
|
||
{
|
||
using cfg.BuffCfg;
|
||
|
||
public class BaseBuff: IPoolData
|
||
{
|
||
|
||
/// <summary>
|
||
/// buff被挂载的对象
|
||
/// </summary>
|
||
public GameUnit owner;
|
||
|
||
/// <summary>
|
||
/// buff的创建者
|
||
/// </summary>
|
||
public GameUnit creator { get; private set; }
|
||
public uint creatorId
|
||
{
|
||
get
|
||
{
|
||
if (creator == null)
|
||
{
|
||
return 0;
|
||
}
|
||
return creator.GetID();
|
||
}
|
||
}
|
||
|
||
public EBuffType buffType
|
||
{
|
||
get
|
||
{
|
||
return configData.BuffType;
|
||
}
|
||
}
|
||
|
||
private bool _existNeedRemove = false;
|
||
private bool _extNeedRemove = false;
|
||
|
||
private int _createCellIndex;
|
||
|
||
public bool needRemove
|
||
{
|
||
get
|
||
{
|
||
var existNeedRemove = _GetExistNeedRemove();
|
||
if (existNeedRemove)
|
||
{
|
||
return true;
|
||
}
|
||
return _GetExtNeedRemove();
|
||
}
|
||
}
|
||
|
||
public DataBuff configData;
|
||
public BuffRuntimeData runtimeData;
|
||
|
||
private float _remainTriggerTime;
|
||
|
||
private EffectPlayingData _existEffectData;
|
||
|
||
public bool isParamOverride;
|
||
private float[] _overrideParams;
|
||
|
||
public virtual void Ctor(GameUnit creator,
|
||
DataBuff configData)
|
||
{
|
||
this.creator = creator;
|
||
this.configData = configData;
|
||
isParamOverride = false;
|
||
_existNeedRemove = false;
|
||
_extNeedRemove = false;
|
||
if (runtimeData == null)
|
||
{
|
||
runtimeData = new BuffRuntimeData();
|
||
}
|
||
else
|
||
{
|
||
runtimeData.Reset();
|
||
}
|
||
if (configData.TriggerType == ETriggerType.ON_LOOP)
|
||
{
|
||
_remainTriggerTime = configData.TiggerUnitTime;
|
||
}
|
||
}
|
||
|
||
public void Init()
|
||
{
|
||
_createCellIndex = owner.transData.cellIndex;
|
||
_OnInit();
|
||
}
|
||
|
||
protected virtual void _OnInit()
|
||
{
|
||
|
||
|
||
}
|
||
|
||
public void Trigger()
|
||
{
|
||
_OnTrigger();
|
||
|
||
if (configData.ExistType == EExistType.Once)
|
||
{
|
||
_existNeedRemove = true;
|
||
}
|
||
}
|
||
|
||
protected virtual void _OnTrigger()
|
||
{
|
||
|
||
}
|
||
|
||
public void OverrideFloatParams(params float[] overrideParams)
|
||
{
|
||
isParamOverride = true;
|
||
_overrideParams = overrideParams;
|
||
}
|
||
|
||
public void TriggerAdd()
|
||
{
|
||
if (configData.TriggerType == ETriggerType.ON_ADD)
|
||
{
|
||
Trigger();
|
||
}
|
||
_OnAdd();
|
||
|
||
if (configData.ExistType == EExistType.During ||
|
||
configData.ExistType == EExistType.Allways)
|
||
{
|
||
if (configData.ExistEffectId != 0)
|
||
{
|
||
var ownerTrans = owner.Node.transform;
|
||
_existEffectData = EffectManager.instance.PlayEffectById(configData.ExistEffectId,
|
||
ownerTrans.position, ownerTrans.rotation, ownerTrans);
|
||
_existEffectData.autoDestroy = false;
|
||
}
|
||
|
||
}
|
||
|
||
if (!string.IsNullOrEmpty(configData.BuffDisplayIcon))
|
||
{
|
||
EventManager.Instance.Send(EventManager.EventName.INFIGHT_BUFF_ADDICON, this);
|
||
}
|
||
|
||
if (owner.debugShowInfo.cacheConfigId != 0 && configData.BuffEffectType == EBuffEffectType.PositiveBuff)
|
||
{
|
||
EventManager.Instance.Send(EventManager.EventName.INFIGHT_AUDIO_GET_P_BUFF, owner.debugShowInfo.cacheConfigId);
|
||
}
|
||
}
|
||
|
||
protected virtual void _OnAdd()
|
||
{
|
||
|
||
}
|
||
|
||
protected virtual void _OnRemove()
|
||
{
|
||
|
||
}
|
||
|
||
|
||
public void TriggerRemove()
|
||
{
|
||
if (configData.TriggerType == ETriggerType.ON_REMOVE)
|
||
{
|
||
Trigger();
|
||
}
|
||
|
||
if (!string.IsNullOrEmpty(configData.BuffDisplayIcon))
|
||
{
|
||
EventManager.Instance.Send(EventManager.EventName.INFIGHT_BUFF_REMOVEICON, this);
|
||
}
|
||
|
||
_OnRemove();
|
||
|
||
if (_existEffectData != null)
|
||
{
|
||
_existEffectData.Destroy();
|
||
_existEffectData = null;
|
||
}
|
||
}
|
||
|
||
public void LogicUpdate(float dt)
|
||
{
|
||
runtimeData.passTime += dt;
|
||
|
||
if (configData.TriggerType == ETriggerType.ON_LOOP)
|
||
{
|
||
_remainTriggerTime -= dt;
|
||
if (_remainTriggerTime <= 0)
|
||
{
|
||
Trigger();
|
||
_remainTriggerTime += configData.TiggerUnitTime;
|
||
}
|
||
}
|
||
|
||
_OnLogicUpdate(dt);
|
||
}
|
||
|
||
public void RefreshDuring()
|
||
{
|
||
runtimeData.passTime = 0;
|
||
}
|
||
|
||
public void SpecialTrigger(ETriggerType triggerType)
|
||
{
|
||
if (configData.TriggerType == triggerType)
|
||
{
|
||
Trigger();
|
||
}
|
||
}
|
||
|
||
public void TriggerCellIndexChange()
|
||
{
|
||
if (configData.ExExitType == EExitType.CellIndexChange)
|
||
{
|
||
var newCellIndex = owner.transData.cellIndex;
|
||
if (newCellIndex != _createCellIndex)
|
||
{
|
||
_extNeedRemove = true;
|
||
}
|
||
}
|
||
}
|
||
|
||
protected virtual void _OnLogicUpdate(float dt)
|
||
{
|
||
|
||
}
|
||
|
||
public override string ToString()
|
||
{
|
||
var sb = new StringBuilder();
|
||
sb.AppendLine("ID:" + configData.ID);
|
||
sb.AppendLine("Name:" + configData.Name);
|
||
return sb.ToString();
|
||
}
|
||
|
||
public void OnGet()
|
||
{
|
||
}
|
||
public void OnReturn()
|
||
{
|
||
_existNeedRemove = false;
|
||
}
|
||
|
||
protected float _TryGetFloatParam(int index, float defaultValue = 0)
|
||
{
|
||
if (isParamOverride)
|
||
{
|
||
// 使用覆写的
|
||
if (_overrideParams.Length > index)
|
||
{
|
||
return _overrideParams[index];
|
||
}
|
||
return defaultValue;
|
||
}
|
||
|
||
// 使用配置的
|
||
if (configData.FloatParams.Count > index)
|
||
{
|
||
return configData.FloatParams[index];
|
||
}
|
||
return defaultValue;
|
||
}
|
||
|
||
protected int _TryGetIntParam(int index)
|
||
{
|
||
var fValue = _TryGetFloatParam(index);
|
||
return Mathf.RoundToInt(fValue);
|
||
}
|
||
|
||
private bool _GetExistNeedRemove()
|
||
{
|
||
switch (configData.ExistType)
|
||
{
|
||
case EExistType.Once:
|
||
// 如果存在时间大于0,那么超过时间就移除
|
||
if (configData.MaxExistTime > 0)
|
||
{
|
||
if (runtimeData.passTime >= configData.MaxExistTime)
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
// 否则,只要被触发过就移除
|
||
return _existNeedRemove;
|
||
case EExistType.During:
|
||
return runtimeData.passTime >= configData.MaxExistTime;
|
||
case EExistType.Allways:
|
||
return false;
|
||
default:
|
||
DebugUtil.LogError("暂未处理的buff existType:{0}",configData.ExistType);
|
||
return true;
|
||
}
|
||
}
|
||
|
||
private bool _GetExtNeedRemove()
|
||
{
|
||
switch (configData.ExExitType)
|
||
{
|
||
case EExitType.None:
|
||
return false;
|
||
case EExitType.CreaterOutFight:
|
||
if (creator == null || creator.isDisposed)
|
||
{
|
||
return true;
|
||
}
|
||
return GameUnitManager.instance.infightUnits.Contains(creator.GetID());
|
||
default:
|
||
DebugUtil.LogError("暂未处理的buff extExitType:{0}",configData.ExExitType);
|
||
return _extNeedRemove;
|
||
}
|
||
}
|
||
}
|
||
}
|