NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/AI/Utils/AIMoveHelper.cs

75 lines
2.4 KiB
C#
Raw Normal View History

2025-03-11 16:09:51 +08:00
using System;
using Gameplay.Area;
using Gameplay.Level;
using Gameplay.Unit;
using Gameplay.Unit.Data;
public static class AIMoveHelper
{
/// <summary>
2025-03-12 13:05:35 +08:00
/// 单位移动
/// </summary>
public static bool MoveToCell(GameUnit owner, int cellIndex, bool cancelFollow = true)
{
try
2025-03-11 16:09:51 +08:00
{
2025-03-12 13:05:35 +08:00
if (owner == null || cellIndex == -1) return false;
var map = LevelManager.Instance.CurrentLevel.Map;
var mapData = map?.MapData;
var blockData = mapData?.GetBlockData(cellIndex);
var typeProperty = blockData?.GetTerrainTypeProperty();
if (typeProperty == null || !typeProperty.selectable) return false;
2025-03-11 16:09:51 +08:00
2025-03-12 13:05:35 +08:00
owner.MoveTo(cellIndex, cancelFollow);
return true;
}
catch (Exception e)
{
DebugUtil.LogError("AIUtils.MoveToCell error: {0}", e);
return false;
2025-03-11 16:09:51 +08:00
}
2025-03-12 13:05:35 +08:00
}
2025-03-11 16:09:51 +08:00
2025-03-12 13:05:35 +08:00
/// <summary>
/// 移动向敌人并检查
/// </summary>
public static void MoveToTarget(GameUnit owner, GameUnit target, bool cancelFollow = true)
{
try
2025-03-11 16:09:51 +08:00
{
2025-03-12 13:05:35 +08:00
if (owner == null || target == null) return;
var cellIndex = target.transData.cellIndex;
if (!MoveToCell(owner, cellIndex, cancelFollow)) return;
2025-03-11 16:09:51 +08:00
2025-03-12 13:05:35 +08:00
if (owner.gameunitType != EGameUnitType.Character &&
owner.gameunitType != EGameUnitType.Vehicle) return;
2025-03-11 16:09:51 +08:00
2025-03-12 13:05:35 +08:00
var units = AreaManager.instance.GetUnitsByCellIndex(cellIndex);
foreach (var unit in units)
{
if (unit.commonData.troopId == owner.commonData.troopId)
2025-03-11 16:09:51 +08:00
{
2025-03-12 13:05:35 +08:00
}
else
{
// 如果是敌人, 则设置敌人目标
if (unit.statusData.CheckCanBeTarget(owner, -1))
2025-03-11 16:09:51 +08:00
{
2025-03-12 13:05:35 +08:00
owner.controlData.markEnemy = unit;
owner.controlData.forceUpdateTarget = true;
2025-03-11 16:09:51 +08:00
2025-03-12 13:05:35 +08:00
return;
2025-03-11 16:09:51 +08:00
}
}
}
2025-03-12 13:05:35 +08:00
// 走到这里,说明没有目标,清空标记
owner.controlData.markVehicle = null;
owner.controlData.markEnemy = null;
}
catch (Exception e)
{
DebugUtil.LogError("AIUtils.CheckAndMove error: {0}", e);
2025-03-11 16:09:51 +08:00
}
2025-03-12 13:05:35 +08:00
}
}