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

306 lines
9.6 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System.Collections.Generic;
using Framework;
2024-11-14 15:44:17 +08:00
using Gameplay;
2023-10-19 15:00:19 +08:00
using Gameplay.Character;
using Gameplay.Common;
2023-12-13 16:20:41 +08:00
using Gameplay.Unit;
2023-08-22 19:08:45 +08:00
using Utils.Bounds.Data;
2024-10-12 11:49:08 +08:00
2023-08-22 19:08:45 +08:00
namespace Utils.Bounds
{
public class BoundsManager
{
private static BoundsManager _instance;
2024-10-12 11:49:08 +08:00
2023-08-22 19:08:45 +08:00
public static BoundsManager instance
{
get
{
if (_instance == null)
{
_instance = new BoundsManager();
}
2024-10-12 11:49:08 +08:00
2023-08-22 19:08:45 +08:00
return _instance;
}
}
private Dictionary<int, CombieBounds> _boundsDict;
2023-10-19 15:00:19 +08:00
private TriggerBoundsInfo _triggerBoundsInfo;
private List<uint> _triggeredUnitIds;
private TriggerBoundsInfo _triggerBoundsInfoEdit;
public TriggerBoundsInfo TriggerBoundsInfoEdit
{
get => _triggerBoundsInfoEdit;
}
2023-08-22 19:08:45 +08:00
private BoundsManager()
{
_Init();
}
private void _Init()
{
_boundsDict = new Dictionary<int, CombieBounds>();
2023-10-19 15:00:19 +08:00
2023-08-22 19:08:45 +08:00
// 遍历处理所有的bounds
var boundsDataList = TableManager.Instance.Tables.BoundsConfig.DataList;
for (int i = 0; i < boundsDataList.Count; i++)
{
var boundsData = boundsDataList[i];
2023-10-19 15:00:19 +08:00
if (_boundsDict.TryGetValue((int)boundsData.BoundTag, out var bounds))
2023-08-22 19:08:45 +08:00
{
bounds.AddBounds(boundsData);
}
else
{
bounds = new CombieBounds(boundsData);
2023-10-19 15:00:19 +08:00
_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.GetTeamUnitInfoByUid(uId);
2023-10-19 15:00:19 +08:00
if (unit == null)
{
DebugUtil.LogError("获取角色失败:{0}", uId);
continue;
2023-08-22 19:08:45 +08:00
}
2024-10-12 11:49:08 +08:00
2023-10-19 15:00:19 +08:00
if (unit.IsVehicle())
{
continue;
}
2024-10-12 11:49:08 +08:00
2023-10-19 15:00:19 +08:00
var characterInfo = unit.GetConfigInfo();
if (characterInfo == null)
{
2024-10-12 11:49:08 +08:00
DebugUtil.LogError("获取角色配置失败:{0}", uId);
2023-10-19 15:00:19 +08:00
continue;
}
2024-10-12 11:49:08 +08:00
2024-11-14 15:44:17 +08:00
characterIdList.Add(CommonUtils.GetEfactionByCfg(characterInfo.Cfg));
2023-08-22 19:08:45 +08:00
}
2024-10-12 11:49:08 +08:00
2023-10-19 15:00:19 +08:00
_triggerBoundsInfo = AutoCalcBounds(characterIdList);
2024-10-12 11:49:08 +08:00
2023-10-19 15:00:19 +08:00
// 使用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];
2024-10-12 11:49:08 +08:00
DebugUtil.Log("羁绊类型:{0} 数量:{1}", boundsTag, boundsCount);
2023-10-19 15:00:19 +08:00
}
// 对所有角色进行处理
2023-12-14 18:25:52 +08:00
var units = GameUnitManager.instance.infightUnits;
for (int i = 0; i < units.count; i++)
2023-10-19 15:00:19 +08:00
{
2023-12-14 18:25:52 +08:00
_OnUnitReady(units.GetByIndex(i));
2023-10-19 15:00:19 +08:00
}
EventManager.Instance.Register<GameUnit>(EventManager.EventName.INFIGHT_UNIT_READY, _OnUnitReady);
}
//编队中羁绊触发
2024-10-15 16:30:20 +08:00
public void InitInEdit(Team team)
2023-10-19 15:00:19 +08:00
{
2024-10-11 15:22:03 +08:00
var uIdList = team.GetAllCharacters();
2023-10-19 15:00:19 +08:00
var characterIdList = new List<cfg.CharacterCfg.Efaction>();
2024-10-11 15:22:03 +08:00
for (int i = 0; i < uIdList.Count; i++)
2023-10-19 15:00:19 +08:00
{
var uId = uIdList[i];
var characterInfo = CharacterDataInfoManager.Instance.GetCharacterInfo(uId);
if (characterInfo == null)
{
2024-10-12 11:49:08 +08:00
DebugUtil.LogError("获取角色配置失败:{0}", uId);
2023-10-19 15:00:19 +08:00
continue;
}
2024-10-12 11:49:08 +08:00
2024-11-14 15:44:17 +08:00
characterIdList.Add(CommonUtils.GetEfactionByCfg(characterInfo.Cfg));
2023-10-19 15:00:19 +08:00
}
2024-10-12 11:49:08 +08:00
2023-10-19 15:00:19 +08:00
_triggerBoundsInfoEdit = AutoCalcBounds(characterIdList);
}
public void ReleaseInEdit()
{
_triggerBoundsInfoEdit = null;
}
2024-10-15 16:30:20 +08:00
public TriggerBoundsInfo GetTeamTriggerBounds(Team team)
2023-10-19 15:00:19 +08:00
{
if (team == null)
{
return null;
}
TriggerBoundsInfo triggerBoundsInfo;
2024-10-11 15:22:03 +08:00
var uIdList = team.GetAllCharacters();
2023-10-19 15:00:19 +08:00
var characterIdList = new List<cfg.CharacterCfg.Efaction>();
2024-10-11 15:22:03 +08:00
for (int i = 0; i < uIdList.Count; i++)
2023-10-19 15:00:19 +08:00
{
var uId = uIdList[i];
if (uId <= 0)
{
continue;
}
2024-10-12 11:49:08 +08:00
2023-10-19 15:00:19 +08:00
var characterInfo = CharacterDataInfoManager.Instance.GetCharacterInfo(uId);
if (characterInfo == null)
{
2024-10-12 11:49:08 +08:00
DebugUtil.LogError("获取角色配置失败:{0}", uId);
2023-10-19 15:00:19 +08:00
continue;
}
2024-10-12 11:49:08 +08:00
2024-11-14 15:44:17 +08:00
characterIdList.Add(CommonUtils.GetEfactionByCfg(characterInfo.Cfg));
2023-10-19 15:00:19 +08:00
}
2024-10-12 11:49:08 +08:00
2023-10-19 15:00:19 +08:00
triggerBoundsInfo = AutoCalcBounds(characterIdList);
2024-10-12 11:49:08 +08:00
// 检测一下触发的羁绊有没有到达作用人数限制
2023-10-19 15:00:19 +08:00
var tagList = triggerBoundsInfo.boundsTagList;
var countList = triggerBoundsInfo.boundsCountList;
2024-10-12 11:49:08 +08:00
for (int i = tagList.Count - 1; i >= 0; i--)
2023-10-19 15:00:19 +08:00
{
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;
}
2024-10-15 16:30:20 +08:00
public TriggerBoundsInfo GetTeamTriggerBoundsForDisplay(Team team)
2024-10-12 11:49:08 +08:00
{
if (team == null)
{
return null;
}
var uIdList = team.GetAllCharacters();
var characterIdList = new List<cfg.CharacterCfg.Efaction>();
foreach (var uId in uIdList)
{
if (uId <= 0)
{
continue;
}
var characterInfo = CharacterDataInfoManager.Instance.GetCharacterInfo(uId);
if (characterInfo == null)
{
DebugUtil.LogError("获取角色配置失败:{0}", uId);
continue;
}
2024-11-14 15:44:17 +08:00
characterIdList.Add(CommonUtils.GetEfactionByCfg(characterInfo.Cfg));
2024-10-12 11:49:08 +08:00
}
var triggerBoundsInfo = AutoCalcBounds(characterIdList);
return triggerBoundsInfo;
}
2023-10-19 15:00:19 +08:00
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);
2023-08-22 19:08:45 +08:00
}
// 自动处理战斗中的羁绊
2023-10-19 15:00:19 +08:00
public TriggerBoundsInfo AutoCalcBounds(List<cfg.CharacterCfg.Efaction> idList)
2023-08-22 19:08:45 +08:00
{
2023-10-19 15:00:19 +08:00
// 统计羁绊
var result = new TriggerBoundsInfo();
for (int i = 0; i < idList.Count; i++)
{
var boundTag = idList[i];
var boundId = (int)boundTag;
2024-10-12 11:49:08 +08:00
2023-10-19 15:00:19 +08:00
if (result.boundsTagList.Contains(boundId))
{
var index = result.boundsTagList.IndexOf(boundId);
result.boundsCountList[index]++;
}
else
{
result.boundsTagList.Add(boundId);
result.boundsCountList.Add(1);
}
}
2024-10-12 11:49:08 +08:00
2023-10-19 15:00:19 +08:00
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);
}
}
2023-08-22 19:08:45 +08:00
}
2023-10-19 15:00:19 +08:00
2024-07-05 18:48:40 +08:00
public string GetBoundsDescKey(int boundsId)
{
return $"BoundsDesc_{boundsId}";
}
2024-07-05 18:55:08 +08:00
public string GetBoundsNameKey(int boundsId)
{
return $"BoundsName_{boundsId}";
}
2023-08-22 19:08:45 +08:00
}
2024-10-12 11:49:08 +08:00
}