using System.Text; using Framework; using Gameplay.Effect; using Gameplay.Pool; using Gameplay.Unit; using UnityEngine; namespace Gameplay.Buff { using cfg.BuffCfg; public class BaseBuff: IPoolData { /// /// buff被挂载的对象 /// public GameUnit owner; /// /// buff的创建者 /// 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 float maxExistTime { get; private set; } 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; } if (this.configData.ExistType == EExistType.During) { maxExistTime = this.configData.MaxExistTime; } } public void Init() { _createCellIndex = owner.transData.cellIndex; _OnInit(); } protected virtual void _OnInit() { } public void ForceSetExistTime(float time) { maxExistTime = time; } 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 != null && 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) { if (!owner.statusData.isOnFloor) { // 不再地面上,不执行update逻辑 return; } 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; } } } public void TriggerOnTakeDamage() { if (configData.ExExitType == EExitType.OnTakeDamage) { _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[] _TryGetFloatParams() { if (isParamOverride) { return _overrideParams; } return configData.FloatParams.ToArray(); } 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 (maxExistTime > 0) { if (runtimeData.passTime >= maxExistTime) { return true; } } // 否则,只要被触发过就移除 return _existNeedRemove; case EExistType.During: return runtimeData.passTime >= 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()); case EExitType.OnTakeDamage: return _extNeedRemove; default: DebugUtil.LogError("暂未处理的buff extExitType:{0}",configData.ExExitType); return _extNeedRemove; } } } }