using System.Collections.Generic;
using Gameplay;
using Gameplay.Area;
using Gameplay.Level;
using Gameplay.Unit;
using UnityEngine;
namespace Code.Scripts.Gameplay.Unit.Data
{
///
/// 用于储存单位的路径数据
///
public class UnitPathData
{
public readonly GameUnit owner;
public List pathInWorldPos = new List();
public List pathInCellIndex = new List();
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 result, int toCellIndex)
{
result.Clear();
var nowCellIndex = owner.transData.cellIndex;
if (nowCellIndex == startCellIndex && toCellIndex == targetCellIndex)
{
// 如果路径数据没有变化,直接返回
DebugUtil.LogG("路径数据没有变化,直接返回");
}
else
{
FindPath(toCellIndex);
}
result.AddRange(pathInCellIndex);
}
///
/// 用于计算路径的世界坐标, 用于显示路径
///
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);
}
}
}
}