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

75 lines
2.4 KiB
C#

using System;
using Gameplay.Area;
using Gameplay.Level;
using Gameplay.Unit;
using Gameplay.Unit.Data;
public static class AIMoveHelper
{
/// <summary>
/// 单位移动
/// </summary>
public static bool MoveToCell(GameUnit owner, int cellIndex, bool cancelFollow = true)
{
try
{
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;
owner.MoveTo(cellIndex, cancelFollow);
return true;
}
catch (Exception e)
{
DebugUtil.LogError("AIUtils.MoveToCell error: {0}", e);
return false;
}
}
/// <summary>
/// 移动向敌人并检查
/// </summary>
public static void MoveToTarget(GameUnit owner, GameUnit target, bool cancelFollow = true)
{
try
{
if (owner == null || target == null) return;
var cellIndex = target.transData.cellIndex;
if (!MoveToCell(owner, cellIndex, cancelFollow)) return;
if (owner.gameunitType != EGameUnitType.Character &&
owner.gameunitType != EGameUnitType.Vehicle) return;
var units = AreaManager.instance.GetUnitsByCellIndex(cellIndex);
foreach (var unit in units)
{
if (unit.commonData.troopId == owner.commonData.troopId)
{
}
else
{
// 如果是敌人, 则设置敌人目标
if (unit.statusData.CheckCanBeTarget(owner, -1))
{
owner.controlData.markEnemy = unit;
owner.controlData.forceUpdateTarget = true;
return;
}
}
}
// 走到这里,说明没有目标,清空标记
owner.controlData.markVehicle = null;
owner.controlData.markEnemy = null;
}
catch (Exception e)
{
DebugUtil.LogError("AIUtils.CheckAndMove error: {0}", e);
}
}
}