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

253 lines
7.1 KiB
C#
Raw Normal View History

2023-10-19 15:00:19 +08:00
using System.Collections.Generic;
2023-08-22 19:08:45 +08:00
using System.Diagnostics;
using cfg.BuffCfg;
using Gameplay.Common;
namespace Gameplay.Buff
{
public class UnitBuffManager
{
2023-10-19 15:00:19 +08:00
public readonly GameUnit owner;
2023-08-22 19:08:45 +08:00
2023-10-19 15:00:19 +08:00
public readonly List<BaseBuff> totalBuffList;
private readonly Dictionary<int, List<BaseBuff>> _groupList;
private readonly List<BaseBuff> _needRemoveList;
2023-08-22 19:08:45 +08:00
public UnitBuffManager(GameUnit owner)
{
this.owner = owner;
totalBuffList = new List<BaseBuff>();
_groupList = new Dictionary<int, List<BaseBuff>>();
_needRemoveList = new List<BaseBuff>();
}
2023-08-22 19:08:45 +08:00
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)
{
2023-10-19 15:00:19 +08:00
var addBuff = BuffManager.instance.CreateBuffById(owner, buffId);
2023-08-22 19:08:45 +08:00
AddBuff(addBuff);
}
2023-08-22 19:08:45 +08:00
public void RemoveBuffByIds(List<int> buffIds)
{
for (var i = 0; i < buffIds.Count; ++i)
{
var buffId = buffIds[i];
RemoveBuffById(buffId);
}
}
2023-10-19 15:00:19 +08:00
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;
}
2023-08-22 19:08:45 +08:00
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;
}
var repeatType = buff.configData.RepeatType;
var noAdd = false;
2023-08-22 19:08:45 +08:00
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;
2023-08-22 19:08:45 +08:00
default:
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("暂未处理的repeatType:{0}", repeatType);
2023-08-22 19:08:45 +08:00
break;
}
if (!noAdd)
{
_AddBuff(buff);
}
2023-08-22 19:08:45 +08:00
}
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();
2023-08-22 19:08:45 +08:00
totalBuffList.RemoveAt(index);
2023-10-19 15:00:19 +08:00
var findList = _groupList[buff.configData.GroupId];
findList.Remove(buff);
2023-10-19 15:00:19 +08:00
BuffManager.ReturnBuff(buff);
2023-08-22 19:08:45 +08:00
}
2023-10-24 13:20:13 +08:00
public void AddNeedRemoveBuff(BaseBuff buff)
{
if (_needRemoveList.Contains(buff))
{
return;
}
_needRemoveList.Add(buff);
}
2023-08-22 19:08:45 +08:00
public void LogicUpdate(float dt)
{
for (var i = 0; i < totalBuffList.Count; ++i)
{
var buff = totalBuffList[i];
buff.LogicUpdate(dt);
if (buff.needRemove)
{
2023-10-24 13:20:13 +08:00
AddNeedRemoveBuff(buff);
2023-08-22 19:08:45 +08:00
}
}
if (_needRemoveList.Count > 0)
{
for (var i = 0; i < _needRemoveList.Count; ++i)
{
var buff = _needRemoveList[i];
_RemoveBuff(buff);
}
_needRemoveList.Clear();
}
2023-08-22 19:08:45 +08:00
}
2023-10-31 19:33:19 +08:00
public void TriggerReload()
2023-11-15 16:09:10 +08:00
{
_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)
2023-10-31 19:33:19 +08:00
{
for (int i = 0; i < totalBuffList.Count; i++)
{
var buff = totalBuffList[i];
if (buff.needRemove) continue;
2023-11-15 16:09:10 +08:00
buff.SpecialTrigger(triggerType);
2023-10-31 19:33:19 +08:00
}
}
2023-11-15 16:09:10 +08:00
2023-08-22 19:08:45 +08:00
}
}