712 lines
26 KiB
C#
712 lines
26 KiB
C#
using System;
|
|
using Framework;
|
|
using Gameplay.Area;
|
|
using Gameplay.Unit;
|
|
using Gameplay.Level;
|
|
using Gameplay.Unit.Data;
|
|
using Gameplay.Area.Around;
|
|
using System.Collections.Generic;
|
|
using static Gameplay.Level.LevelData;
|
|
|
|
namespace Gameplay.AI
|
|
{
|
|
public static class AIUtils
|
|
{
|
|
private static List<GameUnit> inSightEnemies = new List<GameUnit>(16);
|
|
private static Dictionary<GameUnit, int> inSightDistanceList = new Dictionary<GameUnit, int>(16);
|
|
|
|
private static List<GameUnit> allFriends = new List<GameUnit>(16);
|
|
private static List<GameUnit> friendsInSight = new List<GameUnit>(16);
|
|
|
|
private static Dictionary<int, int> patrolDistanceList = new Dictionary<int, int>(64);
|
|
|
|
/// <summary>
|
|
/// 寻找锁敌范围内最近的敌人
|
|
/// </summary>
|
|
public static GameUnit FindNearestEnemyInSightDistance(GameUnit owner)
|
|
{
|
|
try
|
|
{
|
|
if (owner == null) return null;
|
|
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
if (map == null) return null;
|
|
|
|
inSightEnemies.Clear();
|
|
inSightDistanceList.Clear();
|
|
GameUnitManager.instance.characterManager.SearchEnemies(owner, inSightEnemies);
|
|
var ownerCellIndex = owner.transData?.cellIndex;
|
|
for (var i = 0; i < inSightEnemies.Count; i++)
|
|
{
|
|
var enemy = inSightEnemies[i];
|
|
if (enemy == null || enemy.statusData.isDead)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var enemyCellIndex = enemy.transData?.cellIndex;
|
|
|
|
if (!ownerCellIndex.HasValue || !enemyCellIndex.HasValue)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var distance = MapUtils.Distance(map, ownerCellIndex.Value, enemyCellIndex.Value);
|
|
if (distance <= owner.fightData.findEnemyRange &&
|
|
enemy.statusData.CheckCanBeTarget(owner, (int)owner.fightData.findEnemyRange))
|
|
{
|
|
inSightDistanceList.Add(enemy, distance);
|
|
}
|
|
}
|
|
|
|
if (inSightDistanceList.Count <= 0)
|
|
{
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
var minDistance = float.MaxValue;
|
|
GameUnit nearestEnemy = null;
|
|
foreach (var pair in inSightDistanceList)
|
|
{
|
|
if (minDistance > pair.Value)
|
|
{
|
|
minDistance = pair.Value;
|
|
nearestEnemy = pair.Key;
|
|
}
|
|
}
|
|
|
|
return nearestEnemy;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.FindNearestEnemyInSightDistance error: {0}", e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 寻找锁敌范围内的友军
|
|
/// </summary>
|
|
public static List<GameUnit> FindFriendsInSightDistance(GameUnit owner)
|
|
{
|
|
try
|
|
{
|
|
if (owner == null) return null;
|
|
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
if (map == null) return null;
|
|
|
|
allFriends.Clear();
|
|
friendsInSight.Clear();
|
|
|
|
GameUnitManager.instance.characterManager.SearchFriends(owner, allFriends);
|
|
|
|
var ownerCellIndex = owner.transData?.cellIndex;
|
|
for (var i = 0; i < allFriends.Count; i++)
|
|
{
|
|
var friend = allFriends[i];
|
|
if (friend == null || friend.GetID() == owner.GetID())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var friendCellIndex = friend.transData?.cellIndex;
|
|
if (!ownerCellIndex.HasValue || !friendCellIndex.HasValue)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var distance = MapUtils.Distance(map, ownerCellIndex.Value, friendCellIndex.Value);
|
|
if (distance <= owner.fightData.findEnemyRange)
|
|
{
|
|
friendsInSight.Add(friend);
|
|
}
|
|
}
|
|
|
|
return friendsInSight;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.FindFriendsInSightDistance error: {0}", e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 寻找范围内最近的敌方npc
|
|
/// </summary>
|
|
public static GameUnit FindNearestEnemyNpcInShootRange(GameUnit owner)
|
|
{
|
|
try
|
|
{
|
|
var enemyNpc = FindEnemyNpcInSight(owner);
|
|
if (enemyNpc == null || enemyNpc.Count <= 0) return null;
|
|
GameUnit nearestEnemy = null;
|
|
var minDistance = float.MaxValue;
|
|
foreach (var pair in enemyNpc)
|
|
{
|
|
if (minDistance > pair.Value)
|
|
{
|
|
minDistance = pair.Value;
|
|
nearestEnemy = pair.Key;
|
|
}
|
|
}
|
|
|
|
return nearestEnemy;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.FindNearestEnemyNpcInShootRange error: {0}", e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取距离自己最近的单位
|
|
/// </summary>
|
|
public static GameUnit GetNearestGameUnit(GameUnit owner, GameUnit newUnit, GameUnit unit)
|
|
{
|
|
try
|
|
{
|
|
if (owner == null || newUnit == null || unit == null) return null;
|
|
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
if (map == null) return null;
|
|
|
|
var ownerCellIndex = owner.transData?.cellIndex;
|
|
var unitCellIndex = unit.transData?.cellIndex;
|
|
var newUnitCellIndex = newUnit.transData?.cellIndex;
|
|
if (!ownerCellIndex.HasValue || !unitCellIndex.HasValue || !newUnitCellIndex.HasValue) return null;
|
|
|
|
var distance = MapUtils.Distance(map, ownerCellIndex.Value, unitCellIndex.Value);
|
|
var newDistance = MapUtils.Distance(map, ownerCellIndex.Value, newUnitCellIndex.Value);
|
|
return distance <= newDistance ? unit : newUnit;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.GetNearestGameUnit error: {0}", e);
|
|
// 异常返回默认单位
|
|
return unit;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 寻找最近的巡逻点位
|
|
/// </summary>
|
|
public static int FindNearestPointInPatrolInfo(GameUnit owner, List<PatrolInfo> patrolInfos)
|
|
{
|
|
try
|
|
{
|
|
if (owner == null) return Constants.INVALID_ID;
|
|
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
if (map == null) return Constants.INVALID_ID;
|
|
|
|
patrolDistanceList.Clear();
|
|
var ownerCellIndex = owner.transData?.cellIndex;
|
|
for (var i = 0; i < patrolInfos.Count; i++)
|
|
{
|
|
var point = map.BlockPosition2TGSCellIndex(patrolInfos[i].patrolPoint);
|
|
|
|
if (!ownerCellIndex.HasValue || point == Constants.INVALID_ID)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
patrolDistanceList.Add(i,
|
|
MapUtils.Distance(map, ownerCellIndex.Value, point));
|
|
}
|
|
|
|
var minDistance = int.MaxValue;
|
|
int nearestPoint = 0;
|
|
foreach (var pair in patrolDistanceList)
|
|
{
|
|
if (minDistance > pair.Value)
|
|
{
|
|
minDistance = pair.Value;
|
|
nearestPoint = pair.Key;
|
|
}
|
|
}
|
|
|
|
return nearestPoint;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.FindNearestPointInPatrolInfo error: {0}", e);
|
|
// 异常返回第一个索引点
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 巡逻点位转换为格子索引
|
|
/// </summary>
|
|
public static int GetPatrolPointIndexFromVector2Int(Map map, int patrolIndex, List<PatrolInfo> patrolInfos)
|
|
{
|
|
try
|
|
{
|
|
if (map == null || patrolIndex >= patrolInfos.Count) return -1;
|
|
var patrolInfo = patrolInfos[patrolIndex];
|
|
if (patrolInfo == null) return -1;
|
|
return map.BlockPosition2TGSCellIndex(patrolInfo.patrolPoint);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.GetPatrolPointIndexFromVector2Int error: {0}", e);
|
|
// 异常返回无效格子索引
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否需要停掉AI
|
|
/// </summary>
|
|
public static bool IsNeedStop(GameUnit owner)
|
|
{
|
|
return owner.statusData.isRetreat || owner.statusData.isDead || owner.statusData.isOnVehicle ||
|
|
owner.statusData.isChaos || owner.statusData.isStun || owner.statusData.isLockPos;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否停止循环逻辑
|
|
/// </summary>
|
|
public static bool AINeedStopUpdate(Level.Level level)
|
|
{
|
|
if (level == null) return false;
|
|
return level.IsPreparing() || level.IsEnd() || Math.Abs(level.GetSpeed() - 0f) < 0.01f;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 激活AI攻击
|
|
/// </summary>
|
|
public static void ActiveAIAttack(GameUnit owner)
|
|
{
|
|
try
|
|
{
|
|
if (owner == null) return;
|
|
if (owner.statusData.isAttackActive) return;
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
if (map == null) return;
|
|
|
|
if (owner.controlData.targetEnemy != null)
|
|
{
|
|
owner.statusData.isAttackActive = true;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.ActiveAIAttack error: {0}", e);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 单位移动
|
|
/// </summary>
|
|
public static bool MoveTo(GameUnit owner, int cellIndex, bool cancelFollow = true)
|
|
{
|
|
try
|
|
{
|
|
if (owner == null || cellIndex == -1) return false;
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
if (map == null) return false;
|
|
var mapData = map.MapData;
|
|
if (mapData == null) return false;
|
|
var blockData = mapData.GetBlockData(cellIndex);
|
|
if (blockData == null) return false;
|
|
var typeProperty = blockData.GetTerrainTypeProperty();
|
|
if (typeProperty == null || !typeProperty.selectable) return false;
|
|
|
|
owner.MoveTo(cellIndex, cancelFollow);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.MoveTo error: {0}", e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 单位撤退
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static bool MoveAway(GameUnit owner, GameUnit enemyUnit, bool cancelFollow = true)
|
|
{
|
|
try
|
|
{
|
|
if (enemyUnit == null) return false;
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
if (map == null) return false;
|
|
var mapData = map.MapData;
|
|
if (mapData == null) return false;
|
|
map.WorldPosition2BlockPosition(enemyUnit.Node.GetPosition(), out var targetEnemy2IntPos);
|
|
var self2IntPos = map.TGSCellIndex2BlockPosition(owner.transData.cellIndex);
|
|
var newSelf2IntPos = self2IntPos;
|
|
newSelf2IntPos.x = Math.Clamp(
|
|
(self2IntPos.x - targetEnemy2IntPos.x) > 0 ? self2IntPos.x + 1 : self2IntPos.x - 1, 0,
|
|
map.MapData.Height - 1);
|
|
newSelf2IntPos.y = Math.Clamp(
|
|
(self2IntPos.y - targetEnemy2IntPos.y) > 0 ? self2IntPos.y + 1 : self2IntPos.y - 1, 0,
|
|
map.MapData.Width - 1);
|
|
if (self2IntPos == newSelf2IntPos) return false;
|
|
var cellIndex = map.BlockPosition2TGSCellIndex(newSelf2IntPos);
|
|
if (PathFinder.CanPass(cellIndex, owner))
|
|
{
|
|
owner.MoveTo(cellIndex, cancelFollow);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.MoveAway error: {0}", e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// AI 移动向敌人并检查
|
|
/// </summary>
|
|
public static void CheckAndMove(GameUnit owner, int cellIndex, bool cancelFollow = true)
|
|
{
|
|
try
|
|
{
|
|
if (!MoveTo(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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断是否有敌人重合
|
|
/// </summary>
|
|
public static void CheckAreaIsOtherEnemyUnit(GameUnit owner, uint target)
|
|
{
|
|
try
|
|
{
|
|
if (owner == null || owner.statusData.isMoveing) return;
|
|
var cellIndex = owner.transData.cellIndex;
|
|
var units = AreaManager.instance.GetUnitsByCellIndex(cellIndex);
|
|
var hasOther = false;
|
|
foreach (var unit in units)
|
|
{
|
|
if (unit == owner) continue;
|
|
if (!unit.commonData.canClaimStandCellIndex) continue;
|
|
if (unit.statusData.isMoveing) continue;
|
|
hasOther = true;
|
|
break;
|
|
}
|
|
|
|
if (!hasOther) return;
|
|
if (target == Constants.INVALID_UINT_ID)
|
|
{
|
|
MoveOtherCell(owner);
|
|
}
|
|
else
|
|
{
|
|
var enemy = GameUnitManager.instance.infightUnits.Search(target);
|
|
if (enemy == null || enemy.statusData.isDead) return;
|
|
MoveOtherCanAttackCell(owner, enemy);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.CheckAreaIsOtherEnemyUnit error: {0}", e);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移动至周围可到达格子
|
|
/// </summary>
|
|
private static void MoveOtherCell(GameUnit owner, int maxDistance = 2)
|
|
{
|
|
try
|
|
{
|
|
var map = LevelManager.Instance.CurrentLevel?.Map;
|
|
if (map == null || owner == null) return;
|
|
var cellIndex = owner.transData.cellIndex;
|
|
if (!AreaManager.instance.CheckContainsMoveUnit(cellIndex))
|
|
return;
|
|
for (var i = 1; i <= maxDistance; i++)
|
|
{
|
|
var minSteps = i - 1;
|
|
var maxSteps = i;
|
|
var cells = map.TerrainGridSystem.CellGetNeighboursWithinRange(cellIndex, minSteps, maxSteps,
|
|
canCrossCheckType: TGS.CanCrossCheckType.IgnoreCanCrossCheckOnAllCells);
|
|
foreach (var cell in cells)
|
|
{
|
|
if (!CheckCellCanSelect(cell)) continue;
|
|
if (!PathFinder.CanBasePass(cell, owner)) continue;
|
|
if (AreaManager.instance.CheckContainsMoveUnit(cell)) continue;
|
|
if (maxSteps > 1)
|
|
{
|
|
if (CheckPathExits(owner, cell))
|
|
{
|
|
owner.controlData.targetCellIndex = cell;
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
owner.controlData.targetCellIndex = cell;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 无处可去
|
|
owner.MoveTo(owner.transData.bornCellIndex, true);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.MoveOtherCell error: {0}", e);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移动至周围可攻击格子
|
|
/// </summary>
|
|
private static void MoveOtherCanAttackCell(GameUnit owner, GameUnit enemy, int maxDistance = 2)
|
|
{
|
|
try
|
|
{
|
|
var map = LevelManager.Instance.CurrentLevel?.Map;
|
|
if (map == null || owner == null || enemy == null) return;
|
|
var enemyCellIndex = enemy.transData.cellIndex;
|
|
var cellIndex = owner.transData.cellIndex;
|
|
if (!AreaManager.instance.CheckContainsMoveUnit(cellIndex))
|
|
return;
|
|
for (var i = 1; i <= maxDistance; i++)
|
|
{
|
|
var minSteps = i - 1;
|
|
var maxSteps = i;
|
|
var cells = map.TerrainGridSystem.CellGetNeighboursWithinRange(cellIndex, minSteps, maxSteps,
|
|
canCrossCheckType: TGS.CanCrossCheckType.IgnoreCanCrossCheckOnAllCells);
|
|
foreach (var cell in cells)
|
|
{
|
|
if (!CheckCellCanSelect(cell)) continue;
|
|
if (!PathFinder.CanBasePass(cell, owner)) continue;
|
|
if (AreaManager.instance.CheckContainsMoveUnit(cell)) continue;
|
|
// 判断该位置可否攻击目标
|
|
var searchArea = AreaManager.instance.attackAreaManager.GetAttackArea(cell,
|
|
owner.fightData.attackDistance);
|
|
if (!searchArea.canAttackCells.Contains(enemyCellIndex)) continue;
|
|
if (maxSteps > 1)
|
|
{
|
|
if (CheckPathExits(owner, cell))
|
|
{
|
|
owner.controlData.targetCellIndex = cell;
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
owner.controlData.targetCellIndex = cell;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 无处可去
|
|
owner.MoveTo(owner.transData.bornCellIndex, true);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.AvoidEnemy error: {0}", e);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计算当前位置与出生点位置是否超过防守距离
|
|
/// </summary>
|
|
public static bool CheckDistanceIsOver(GameUnit owner)
|
|
{
|
|
try
|
|
{
|
|
if (owner == null) return true;
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
if (map == null) return true;
|
|
var curPos = owner.transData.cellIndex;
|
|
var bornPos = owner.unitLevelData.BornPosition;
|
|
var distance = MapUtils.Distance(map, curPos, bornPos);
|
|
return owner.unitLevelData.DefensiveDistance < distance;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.CheckDistanceIsOver error: {0}", e);
|
|
// 默认超出
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 顺时针获取周围第一个能够到达的一圈格子
|
|
/// </summary>
|
|
public static int GetCanReachCellAround(Map map, GameUnit owner, int centerIndex, int step = 1)
|
|
{
|
|
var cellList = GetAroundCells(centerIndex, step);
|
|
foreach (var cellIndex in cellList)
|
|
{
|
|
var blockData = map.GetBlockData(cellIndex);
|
|
if (blockData == null) continue;
|
|
var typeProperty = blockData.GetTerrainTypeProperty();
|
|
if (typeProperty == null) continue;
|
|
if (CheckPathExits(owner, cellIndex))
|
|
{
|
|
return cellIndex;
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查该点是否能通过
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static bool CheckCellCanPass(GameUnit owner, int cellIndex)
|
|
{
|
|
return PathFinder.CanPass(cellIndex, owner);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检测是否存在路径
|
|
/// </summary>
|
|
public static bool CheckPathExits(GameUnit owner, int cellIndex)
|
|
{
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
if (owner == null || cellIndex == -1 || map == null) return false;
|
|
var path = PathFinder.FindPath(map, cellIndex, owner);
|
|
return path != null && path.Count > 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置cellIndex周围的通行级别
|
|
/// </summary>
|
|
/// <returns>Dic(格子,原来的通行级别)</returns>
|
|
public static Dictionary<int, int> SetCellCrossLevel(Map map, GameUnit owner, int cellIndex, int step = 1)
|
|
{
|
|
if (map == null || owner == null) return null;
|
|
var cells = new Dictionary<int, int>();
|
|
var cellList = GetAroundCells(cellIndex, step);
|
|
var crossLevel = owner.commonData.crossLevel + 1;
|
|
foreach (var cell in cellList)
|
|
{
|
|
var blockData = map.GetBlockData(cell);
|
|
if (blockData == null) continue;
|
|
map.GetRuntimeBlockProperty(cell, out var data);
|
|
var oldCrossLevel = data.crossLevel;
|
|
if (data.crossLevel > crossLevel) continue;
|
|
data.crossLevel = crossLevel + 1;
|
|
map.SetRuntimeBlockProperty(cell, data);
|
|
cells[cell] = oldCrossLevel;
|
|
//DebugUtil.LogWarning("设置地格{0}的通行级别为:{1}", cell, crossLevel);
|
|
}
|
|
|
|
return cells;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置cellList通行级别
|
|
/// </summary>
|
|
public static void SetCellCrossLevel(Map map, Dictionary<int, int> cells)
|
|
{
|
|
if (map == null) return;
|
|
foreach (var cell in cells)
|
|
{
|
|
var blockData = map.GetBlockData(cell.Key);
|
|
if (blockData == null) continue;
|
|
map.GetRuntimeBlockProperty(cell.Key, out var data);
|
|
data.crossLevel = cell.Value;
|
|
map.SetRuntimeBlockProperty(cell.Key, data);
|
|
//DebugUtil.LogError("还原格子:{0}到{1}", cell.Key, cell.Value);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检测地格是否可选
|
|
/// </summary>
|
|
private static bool CheckCellCanSelect(int cellIndex)
|
|
{
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
if (map == null) return false;
|
|
return MapUtils.GetTypeProperty(map, cellIndex) != null &&
|
|
MapUtils.GetTypeProperty(map, cellIndex).selectable;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获得周围格子
|
|
/// </summary>
|
|
private static List<int> GetAroundCells(int centerIndex, int step = 1)
|
|
{
|
|
var cellList = new List<int>();
|
|
AroundHelper.GetAround(centerIndex, step, cellList);
|
|
return cellList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 寻找锁敌范围内的敌方npc
|
|
/// </summary>
|
|
private static Dictionary<GameUnit, int> FindEnemyNpcInSight(GameUnit owner)
|
|
{
|
|
try
|
|
{
|
|
var enemyNpc = new Dictionary<GameUnit, int>();
|
|
var level = LevelManager.Instance.CurrentLevel;
|
|
if (level == null) return enemyNpc;
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
if (map == null || level.SelfNpcLocalID == null || level.SelfNpcLocalID.Count <= 0) return enemyNpc;
|
|
var enemyList = new List<GameUnit>();
|
|
GameUnitManager.instance.characterManager.SearchEnemies(owner, enemyList);
|
|
foreach (var enemy in enemyList)
|
|
{
|
|
if (enemy.unitLevelData == null) continue;
|
|
if (level.SelfNpcLocalID.Contains(enemy.unitLevelData.LocalId))
|
|
{
|
|
var distance = MapUtils.Distance(map, enemy.transData.cellIndex, owner.transData.cellIndex);
|
|
if (distance <= owner.fightData.findEnemyRange)
|
|
enemyNpc.Add(enemy, distance);
|
|
}
|
|
}
|
|
|
|
return enemyNpc;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("AIUtils.FindEnemyNpcInSight error: {0}", e);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
} |