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

256 lines
8.3 KiB
C#

using System.Collections.Generic;
using Framework;
using Gameplay.Character;
using Gameplay.Common;
using Gameplay.Unit;
using Utils.Bounds.Data;
namespace Utils.Bounds
{
public class BoundsManager
{
private static BoundsManager _instance;
public static BoundsManager instance
{
get
{
if (_instance == null)
{
_instance = new BoundsManager();
}
return _instance;
}
}
private Dictionary<int, CombieBounds> _boundsDict;
private TriggerBoundsInfo _triggerBoundsInfo;
private List<uint> _triggeredUnitIds;
private TriggerBoundsInfo _triggerBoundsInfoEdit;
public TriggerBoundsInfo TriggerBoundsInfoEdit
{
get => _triggerBoundsInfoEdit;
}
private BoundsManager()
{
_Init();
}
private void _Init()
{
_boundsDict = new Dictionary<int, CombieBounds>();
// 遍历处理所有的bounds
var boundsDataList = TableManager.Instance.Tables.BoundsConfig.DataList;
for (int i = 0; i < boundsDataList.Count; i++)
{
var boundsData = boundsDataList[i];
if (_boundsDict.TryGetValue((int)boundsData.BoundTag, out var bounds))
{
bounds.AddBounds(boundsData);
}
else
{
bounds = new CombieBounds(boundsData);
_boundsDict.Add((int)boundsData.BoundTag, bounds);
}
}
DebugUtil.Log("羁绊数据初始化完成,共有羁绊类型:{0}", _boundsDict.Count);
}
public void Init()
{
_triggeredUnitIds = new List<uint>();
// 计算羁绊信息
var uIdList = TeamManager.Instance.GetAllIDList();
var characterIdList = new List<cfg.CharacterCfg.Efaction>();
for (int i = 0; i < uIdList.Count; i++)
{
var uId = uIdList[i];
var unit = TeamManager.Instance.GetSingleCharacByUid(uId);
if (unit == null)
{
DebugUtil.LogError("获取角色失败:{0}", uId);
continue;
}
if (unit.IsVehicle())
{
continue;
}
var characterInfo = unit.GetConfigInfo();
if (characterInfo == null)
{
DebugUtil.LogError("获取角色配置失败:{0}",uId);
continue;
}
characterIdList.Add(characterInfo.Cfg.Fraction);
}
_triggerBoundsInfo = AutoCalcBounds(characterIdList);
// 使用Log输出羁绊信息
var boundsTagList = _triggerBoundsInfo.boundsTagList;
var boundsCountList = _triggerBoundsInfo.boundsCountList;
for (int i = 0; i < boundsTagList.Count; i++)
{
var boundsTag = boundsTagList[i];
var boundsCount = boundsCountList[i];
DebugUtil.Log("羁绊类型:{0} 数量:{1}",boundsTag, boundsCount);
}
// 对所有角色进行处理
var units = GameUnitManager.instance.infightUnits;
for (int i = 0; i < units.count; i++)
{
_OnUnitReady(units.GetByIndex(i));
}
EventManager.Instance.Register<GameUnit>(EventManager.EventName.INFIGHT_UNIT_READY, _OnUnitReady);
}
//编队中羁绊触发
public void InitInEdit(Team team)
{
var uIdList = team.GetCharacterIDs();
var characterIdList = new List<cfg.CharacterCfg.Efaction>();
for (int i = 0; i < uIdList.Length; i++)
{
var uId = uIdList[i];
var characterInfo = CharacterDataInfoManager.Instance.GetCharacterInfo(uId);
if (characterInfo == null)
{
DebugUtil.LogError("获取角色配置失败:{0}",uId);
continue;
}
characterIdList.Add(characterInfo.Cfg.Fraction);
}
_triggerBoundsInfoEdit = AutoCalcBounds(characterIdList);
}
public void ReleaseInEdit()
{
_triggerBoundsInfoEdit = null;
}
public TriggerBoundsInfo GetTeamTriggerBounds(Team team)
{
if (team == null)
{
return null;
}
TriggerBoundsInfo triggerBoundsInfo;
var uIdList = team.GetCharacterIDs();
var characterIdList = new List<cfg.CharacterCfg.Efaction>();
for (int i = 0; i < uIdList.Length; i++)
{
var uId = uIdList[i];
if (uId <= 0)
{
continue;
}
var characterInfo = CharacterDataInfoManager.Instance.GetCharacterInfo(uId);
if (characterInfo == null)
{
DebugUtil.LogError("获取角色配置失败:{0}",uId);
continue;
}
characterIdList.Add(characterInfo.Cfg.Fraction);
}
triggerBoundsInfo = AutoCalcBounds(characterIdList);
//检测一下触发的羁绊有没有到达作用人数限制
var tagList = triggerBoundsInfo.boundsTagList;
var countList = triggerBoundsInfo.boundsCountList;
for (int i = tagList.Count - 1; i >=0 ; i--)
{
var fraction = tagList[i];
int curCount = countList[i];
if (_boundsDict.TryGetValue(fraction, out var bounds))
{
var bound = bounds.boundsList[0];
int limitCount = bound.triggerCount;
if (curCount < limitCount)
{
tagList.RemoveAt(i);
countList.RemoveAt(i);
}
}
}
return triggerBoundsInfo;
}
public CombieBounds GetBoundsByFraction(int fraction)
{
if (_boundsDict.TryGetValue(fraction, out var bounds))
{
return bounds;
}
return null;
}
private void _OnUnitReady(GameUnit unit)
{
if (unit == null) return;
if (unit.statusData.isDead) return;
if (_triggeredUnitIds.Contains(unit.GetID())) return;
_triggeredUnitIds.Add(unit.GetID());
// 触发羁绊
TriggerBounds(_triggerBoundsInfo, unit);
}
public void Dispose()
{
EventManager.Instance.Unregister<GameUnit>(EventManager.EventName.INFIGHT_UNIT_READY, _OnUnitReady);
}
// 自动处理战斗中的羁绊
public TriggerBoundsInfo AutoCalcBounds(List<cfg.CharacterCfg.Efaction> idList)
{
// 统计羁绊
var result = new TriggerBoundsInfo();
for (int i = 0; i < idList.Count; i++)
{
var boundTag = idList[i];
var boundId = (int)boundTag;
if (result.boundsTagList.Contains(boundId))
{
var index = result.boundsTagList.IndexOf(boundId);
result.boundsCountList[index]++;
}
else
{
result.boundsTagList.Add(boundId);
result.boundsCountList.Add(1);
}
}
return result;
}
public void TriggerBounds(TriggerBoundsInfo triggerBoundsInfo,
GameUnit unit)
{
// 根据羁绊触发羁绊效果
for (int i = 0; i < triggerBoundsInfo.boundsTagList.Count; i++)
{
var boundTag = triggerBoundsInfo.boundsTagList[i];
var boundCount = triggerBoundsInfo.boundsCountList[i];
if (_boundsDict.TryGetValue(boundTag, out var bounds))
{
bounds.HandleForBattle(boundCount, unit);
}
}
}
}
}