271 lines
7.7 KiB
C#
271 lines
7.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using cfg.BuffCfg;
|
|
using Gameplay.Common;
|
|
using Gameplay.Common.Attri;
|
|
|
|
namespace Gameplay.Buff
|
|
{
|
|
|
|
public class UnitBuffManager
|
|
{
|
|
|
|
public readonly GameUnit owner;
|
|
|
|
public readonly List<BaseBuff> totalBuffList;
|
|
private readonly Dictionary<int, List<BaseBuff>> _groupList;
|
|
private readonly List<BaseBuff> _needRemoveList;
|
|
|
|
public UnitBuffManager(GameUnit owner)
|
|
{
|
|
this.owner = owner;
|
|
totalBuffList = new List<BaseBuff>();
|
|
_groupList = new Dictionary<int, List<BaseBuff>>();
|
|
_needRemoveList = new List<BaseBuff>();
|
|
}
|
|
|
|
public void AddBuffByIds(List<int> buffIds)
|
|
{
|
|
for (var i = 0; i < buffIds.Count; ++i)
|
|
{
|
|
var buffId = buffIds[i];
|
|
AddBuffById(buffId);
|
|
}
|
|
}
|
|
|
|
public void AddBuffById(int buffId)
|
|
{
|
|
var addBuff = BuffManager.instance.CreateBuffById(owner, buffId);
|
|
AddBuff(addBuff);
|
|
}
|
|
|
|
public void RemoveBuffByIds(List<int> buffIds)
|
|
{
|
|
for (var i = 0; i < buffIds.Count; ++i)
|
|
{
|
|
var buffId = buffIds[i];
|
|
RemoveBuffById(buffId);
|
|
}
|
|
}
|
|
|
|
public bool CheckHasBuffType(EBuffType buffType)
|
|
{
|
|
for (var i = 0; i < totalBuffList.Count; ++i)
|
|
{
|
|
var buff = totalBuffList[i];
|
|
if (buff.configData.BuffType == buffType)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool CheckHasBuffTypeExcept(EBuffType buffType,
|
|
BaseBuff exceptBuff)
|
|
{
|
|
for (var i = 0; i < totalBuffList.Count; ++i)
|
|
{
|
|
var buff = totalBuffList[i];
|
|
if (buff == exceptBuff)
|
|
{
|
|
continue;
|
|
}
|
|
if (buff.configData.BuffType == buffType)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public List<BaseBuff> GetBuffsByType(EBuffType buffType)
|
|
{
|
|
var findList = new List<BaseBuff>();
|
|
for (var i = 0; i < totalBuffList.Count; ++i)
|
|
{
|
|
var buff = totalBuffList[i];
|
|
if (buff.configData.BuffType == buffType)
|
|
{
|
|
findList.Add(buff);
|
|
}
|
|
}
|
|
return findList;
|
|
}
|
|
|
|
public void RemoveBuffById(int buffId)
|
|
{
|
|
var findBuff = totalBuffList.Find(buff => buff.configData.ID == buffId);
|
|
if (findBuff == null)
|
|
{
|
|
return;
|
|
}
|
|
_needRemoveList.Add(findBuff);
|
|
}
|
|
|
|
public void AddBuff(BaseBuff buff)
|
|
{
|
|
if (buff == null)
|
|
{
|
|
DebugUtil.LogError("buff为空");
|
|
return;
|
|
}
|
|
if (buff.configData.BuffEffectType == EBuffEffectType.NegativeBuff)
|
|
{
|
|
// 负面buff
|
|
var randomValue = UnityEngine.Random.Range(0, 1f);
|
|
var ignoreRate = owner.fightData.ignoreDebuffRate;
|
|
if (randomValue < ignoreRate)
|
|
{
|
|
// 免疫
|
|
DebugUtil.Log("触发免疫debuff");
|
|
return;
|
|
}
|
|
}
|
|
var repeatType = buff.configData.RepeatType;
|
|
var noAdd = false;
|
|
switch (repeatType)
|
|
{
|
|
case EBuffRepeatType.None:
|
|
// 不做处理
|
|
break;
|
|
case EBuffRepeatType.Replace:
|
|
// 找到旧的,全部删除
|
|
if (_groupList.TryGetValue(buff.configData.GroupId, out var findList))
|
|
{
|
|
// 全部标记为待删除
|
|
for (var i = 0; i < findList.Count; ++i)
|
|
{
|
|
var oldBuff = findList[i];
|
|
_needRemoveList.Add(oldBuff);
|
|
}
|
|
}
|
|
break;
|
|
case EBuffRepeatType.RefreshTime:
|
|
// 找到旧的,刷新持续时间
|
|
if (_groupList.TryGetValue(buff.configData.GroupId, out findList))
|
|
{
|
|
// 刷新持续时间
|
|
for (var i = 0; i < findList.Count; ++i)
|
|
{
|
|
var oldBuff = findList[i];
|
|
oldBuff.RefreshDuring();
|
|
}
|
|
noAdd = true;
|
|
}
|
|
break;
|
|
default:
|
|
DebugUtil.LogError("暂未处理的repeatType:{0}", repeatType);
|
|
break;
|
|
}
|
|
|
|
if (noAdd)
|
|
{
|
|
// 需要把buff返回池
|
|
BuffManager.ReturnBuff(buff);
|
|
}
|
|
else
|
|
{
|
|
_AddBuff(buff);
|
|
}
|
|
}
|
|
|
|
private void _AddBuff(BaseBuff buff)
|
|
{
|
|
// owner.Log("增加buff:" + buff);
|
|
buff.owner = owner;
|
|
|
|
// 这里会调用buff的OnInit方法
|
|
buff.Init();
|
|
buff.TriggerAdd();
|
|
totalBuffList.Add(buff);
|
|
if (_groupList.TryGetValue(buff.configData.GroupId, out var findList))
|
|
{
|
|
findList.Add(buff);
|
|
}
|
|
else
|
|
{
|
|
findList = new List<BaseBuff>();
|
|
findList.Add(buff);
|
|
_groupList.Add(buff.configData.GroupId, findList);
|
|
}
|
|
}
|
|
|
|
private void _RemoveBuff(BaseBuff buff)
|
|
{
|
|
var index = totalBuffList.IndexOf(buff);
|
|
if (index < 0)
|
|
{
|
|
return;
|
|
}
|
|
buff.TriggerRemove();
|
|
|
|
totalBuffList.RemoveAt(index);
|
|
var findList = _groupList[buff.configData.GroupId];
|
|
findList.Remove(buff);
|
|
|
|
BuffManager.ReturnBuff(buff);
|
|
}
|
|
|
|
public void AddNeedRemoveBuff(BaseBuff buff)
|
|
{
|
|
if (_needRemoveList.Contains(buff))
|
|
{
|
|
return;
|
|
}
|
|
_needRemoveList.Add(buff);
|
|
}
|
|
|
|
public void LogicUpdate(float dt)
|
|
{
|
|
for (var i = 0; i < totalBuffList.Count; ++i)
|
|
{
|
|
var buff = totalBuffList[i];
|
|
buff.LogicUpdate(dt);
|
|
|
|
if (buff.needRemove)
|
|
{
|
|
AddNeedRemoveBuff(buff);
|
|
}
|
|
}
|
|
|
|
if (_needRemoveList.Count > 0)
|
|
{
|
|
for (var i = 0; i < _needRemoveList.Count; ++i)
|
|
{
|
|
var buff = _needRemoveList[i];
|
|
_RemoveBuff(buff);
|
|
}
|
|
_needRemoveList.Clear();
|
|
}
|
|
|
|
}
|
|
|
|
public void TriggerReload()
|
|
{
|
|
_InnerTrigger(ETriggerType.ON_RELOAD);
|
|
}
|
|
|
|
public void TriggerEnterBattle()
|
|
{
|
|
_InnerTrigger(ETriggerType.ON_ENTER_BATTLE);
|
|
}
|
|
|
|
public void TriggerExitBattle()
|
|
{
|
|
_InnerTrigger(ETriggerType.ON_EXIT_BATTLE);
|
|
}
|
|
|
|
private void _InnerTrigger(ETriggerType triggerType)
|
|
{
|
|
for (int i = 0; i < totalBuffList.Count; i++)
|
|
{
|
|
var buff = totalBuffList[i];
|
|
if (buff.needRemove) continue;
|
|
buff.SpecialTrigger(triggerType);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|