88 lines
2.4 KiB
C#
88 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
using Gameplay;
|
|
using Gameplay.Area;
|
|
using Gameplay.Level;
|
|
using Gameplay.Unit;
|
|
using UnityEngine;
|
|
namespace Code.Scripts.Gameplay.Unit.Data
|
|
{
|
|
|
|
/// <summary>
|
|
/// 用于储存单位的路径数据
|
|
/// </summary>
|
|
public class UnitPathData
|
|
{
|
|
public readonly GameUnit owner;
|
|
|
|
public List<Vector3> pathInWorldPos = new List<Vector3>();
|
|
public List<int> pathInCellIndex = new List<int>();
|
|
|
|
public int startCellIndex
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
public int targetCellIndex
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
public UnitPathData(GameUnit owner)
|
|
{
|
|
this.owner = owner;
|
|
}
|
|
|
|
public void FindPath(int toCellIndex)
|
|
{
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
startCellIndex = owner.transData.cellIndex;
|
|
targetCellIndex = toCellIndex;
|
|
pathInCellIndex = PathFinder.FindPath(map, targetCellIndex, owner);
|
|
}
|
|
|
|
public void FindOrCache(int toCellIndex)
|
|
{
|
|
var nowCellIndex = owner.transData.cellIndex;
|
|
if (nowCellIndex == startCellIndex && toCellIndex == targetCellIndex)
|
|
{
|
|
// 如果路径数据没有变化,直接返回
|
|
return;
|
|
}
|
|
|
|
FindPath(toCellIndex);
|
|
}
|
|
|
|
public void GetPath(List<int> result, int toCellIndex)
|
|
{
|
|
result.Clear();
|
|
var nowCellIndex = owner.transData.cellIndex;
|
|
if (nowCellIndex == startCellIndex && toCellIndex == targetCellIndex)
|
|
{
|
|
// 如果路径数据没有变化,直接返回
|
|
DebugUtil.LogG("路径数据没有变化,直接返回");
|
|
}
|
|
else
|
|
{
|
|
FindPath(toCellIndex);
|
|
}
|
|
result.AddRange(pathInCellIndex);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用于计算路径的世界坐标, 用于显示路径
|
|
/// </summary>
|
|
public void CalcPathInWorldPos()
|
|
{
|
|
pathInWorldPos.Clear();
|
|
for (int i = pathInCellIndex.Count - 1; i >= 0; i--)
|
|
{
|
|
var cellIndex = pathInCellIndex[i];
|
|
var worldPos = AreaManager.instance.GetWorldPosByIndex(cellIndex);
|
|
pathInWorldPos.Add(worldPos);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|