NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Bounds/Data/CombieBounds.cs

109 lines
3.3 KiB
C#

using System.Collections.Generic;
using Gameplay.Common;
using Gameplay.Unit;
namespace Utils.Bounds.Data
{
using cfg.BoundsCfg;
public class CombieBounds
{
public int tagId;
public readonly List<int> triggerCounts;
public readonly List<CombieSingleBounds> boundsList;
private int _oldBoundsCount = -1;
public CombieBounds(DataBounds data)
{
tagId = (int)data.BoundTag;
triggerCounts = new List<int>();
boundsList = new List<CombieSingleBounds>();
triggerCounts.Add(data.TriggerCount);
boundsList.Add(new CombieSingleBounds(data));
}
public void HandleForBattle(int boundsCount, GameUnit unit)
{
// Modify:2023-9-04 改成一次性的了,不再移除
// 移除旧的
// _TriggerRemove(_oldBoundsCount, unit);
// 触发新的
_TriggerAdd(boundsCount, unit);
_oldBoundsCount = boundsCount;
DebugUtil.Log("触发羁绊:{0} 数量:{1}",tagId ,boundsCount);
}
private CombieSingleBounds _SelectBoundsByCount(int boundsCount)
{
var selectIndex = -1;
for (int i = 0; i < triggerCounts.Count; i++)
{
var triggerCount = triggerCounts[i];
if (triggerCount <= boundsCount)
{
selectIndex = i;
}
else
{
break;
}
}
if (selectIndex < 0)
{
return null;
}
return boundsList[selectIndex];
}
private void _TriggerAdd(int boundsCount, GameUnit unit)
{
var newBounds = _SelectBoundsByCount(boundsCount);
if (newBounds == null) return;
newBounds.TriggerAdd(unit);
}
private void _TriggerRemove(int boundsCount, GameUnit unit)
{
var oldBounds = _SelectBoundsByCount(boundsCount);
if (oldBounds == null) return;
oldBounds.TriggerRemove(unit);
}
public void AddBounds(DataBounds data)
{
var triggerCountIndex = triggerCounts.IndexOf(data.TriggerCount);
if (triggerCountIndex < 0)
{
// 不存在,添加新的
var newBounds = new CombieSingleBounds(data);
boundsList.Add(newBounds);
triggerCounts.Add(data.TriggerCount);
// 根据triggerCounts从小到大排序,并且boundsList也跟着排序
for (int i = 0; i < triggerCounts.Count; i++)
{
for (int j = i + 1; j < triggerCounts.Count; j++)
{
if (triggerCounts[i] <= triggerCounts[j])
continue;
(triggerCounts[i], triggerCounts[j]) = (triggerCounts[j], triggerCounts[i]);
(boundsList[i], boundsList[j]) = (boundsList[j], boundsList[i]);
}
}
}
else
{
// 已经存在,添加到对应的triggerCount的bounds中
var bounds = boundsList[triggerCountIndex];
bounds.AddBounds(data);
}
}
}
}