using System.Text; using Gameplay.Common; using Gameplay.Effect; using Gameplay.Pool; namespace Gameplay.Buff { using cfg.BuffCfg; public class BaseBuff: IPoolData { /// /// buff被挂载的对象 /// public GameUnit owner; /// /// buff的创建者 /// public GameUnit creator { get; private set; } public EBuffType buffType { get { return configData.BuffType; } } private bool _needRemove; public bool needRemove { get { switch (configData.ExistType) { case EExistType.Once: return true; case EExistType.During: return runtimeData.passTime >= configData.MaxExistTime; case EExistType.Allways: return false; case EExistType.AfterReload: return _needRemove; default: DebugUtil.LogError("暂未处理的buff existType:{0}",configData.ExistType); return true; } } } public DataBuff configData; public BuffRuntimeData runtimeData; private float _remainTriggerTime; private EffectPlayingData _existEffectData; public virtual void Ctor(GameUnit creator, DataBuff configData) { this.creator = creator; this.configData = configData; 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() { } protected virtual void _OnTrigger() { } public void TriggerAdd() { _OnAdd(); if (configData.TriggerType == ETriggerType.ON_ADD) { _OnTrigger(); } 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() { _OnRemove(); if (configData.TriggerType == ETriggerType.ON_REMOVE) { _OnTrigger(); } 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) { _OnTrigger(); _remainTriggerTime += configData.TiggerUnitTime; } } _OnLogicUpdate(dt); } public void TriggerOnReload() { if (configData.TriggerType == ETriggerType.ON_RELOAD) { _OnTrigger(); } if (configData.ExistType == EExistType.AfterReload) { _needRemove = true; } _OnReload(); } protected virtual void _OnReload() { } 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 (configData.FloatParams.Count > index) { return configData.FloatParams[index]; } return 0; } } }