NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Buff/BaseBuff.cs

233 lines
5.6 KiB
C#

using System.Text;
using Gameplay.Common;
using Gameplay.Effect;
using Gameplay.Pool;
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 EBuffType buffType
{
get
{
return configData.BuffType;
}
}
private bool _needRemove = false;
public bool needRemove
{
get
{
switch (configData.ExistType)
{
case EExistType.Once:
return _needRemove;
case EExistType.During:
return runtimeData.passTime >= configData.MaxExistTime;
case EExistType.Allways:
return false;
default:
DebugUtil.LogError("暂未处理的buff existType:{0}",configData.ExistType);
return true;
}
}
}
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;
_needRemove = false;
if (runtimeData == null)
{
runtimeData = new BuffRuntimeData();
}
else
{
runtimeData.Reset();
}
if (configData.TriggerType == ETriggerType.ON_LOOP)
{
_remainTriggerTime = configData.TiggerUnitTime;
}
}
public void Init()
{
_OnInit();
}
protected virtual void _OnInit()
{
}
public void Trigger()
{
_OnTrigger();
if (configData.ExistType == EExistType.Once)
{
_needRemove = 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;
}
}
}
protected virtual void _OnAdd()
{
}
protected virtual void _OnRemove()
{
}
public void TriggerRemove()
{
if (configData.TriggerType == ETriggerType.ON_REMOVE)
{
Trigger();
}
_OnRemove();
if (_existEffectData != null)
{
EffectManager.instance.ReturnEffect(_existEffectData);
_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();
}
}
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()
{
_needRemove = false;
}
protected float _TryGetFloatParam(int index)
{
if (isParamOverride)
{
// 使用覆写的
if (_overrideParams.Length > index)
{
return _overrideParams[index];
}
return 0;
}
// 使用配置的
if (configData.FloatParams.Count > index)
{
return configData.FloatParams[index];
}
return 0;
}
}
}