Files
Main/Assets/Plugins/Code/FuncellEditor/PathEditor/Proxy/PathData.cs

180 lines
5.7 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00
using UnityEngine;
//路径数据
#if FUNCELL_EDITOR
namespace PathEditor.Proxy.Editor
#else
namespace PathEditor.Proxy.Plugin
#endif
{
public class PathData
{
public static PathData instance = new PathData();
public const int DefaultWeightRadius = 1;
public float m_bakeHeightRangeMin = -2.0f;
public float m_bakeHeightRangeMax = 50.0f;
public int m_weightRadius = DefaultWeightRadius;
public PathGridType[,] m_mergedPathGrid = null;
public PathGridType[,] m_BakePathGrid = null;
public byte[,] m_mergedWeightPathGrid = null;
public float[,] m_heightMap = null;
public int m_numberOfRows = 10;
public int m_numberOfColumns = 10;
public float m_cellSize = 1;
public Transform transform = null;
public int numberOfRows { get; set; }
public int numberOfColumns { get; set; }
public int weightRadius
{
get
{
return m_weightRadius;
}
set
{
m_weightRadius = value;
if (m_weightRadius <= 0 || m_weightRadius > 16)
{
m_weightRadius = DefaultWeightRadius;
}
}
}
public void MergeOutput()
{
if (m_mergedPathGrid != null)
{
UpdateWeightPathGridData();
}
}
public void ClearMergedData()
{
if (m_mergedPathGrid != null)
{
for (int j = 0; j < m_numberOfRows; ++j)
{
for (int i = 0; i < m_numberOfColumns; ++i)
{
m_mergedPathGrid[j, i] = PathGridType.None;
}
}
}
}
public void ResetBakedData(PathGridType defGrid = PathGridType.Jump)
{
m_BakePathGrid = null;
numberOfColumns = m_numberOfColumns;
numberOfRows = m_numberOfRows;
UpdatePathGridData(defGrid);
}
public void UpdatePathGridData(PathGridType defGrid = PathGridType.Jump)
{
m_mergedPathGrid = null;
m_BakePathGrid = null;
bool changed = numberOfRows != m_numberOfRows || numberOfColumns != m_numberOfColumns;
numberOfColumns = m_numberOfColumns;
numberOfRows = m_numberOfRows;
m_heightMap = new float[m_numberOfRows, m_numberOfColumns];
m_mergedPathGrid = new PathGridType[m_numberOfRows, m_numberOfColumns];
m_BakePathGrid = new PathGridType[m_numberOfRows, m_numberOfColumns];
m_heightMap = new float[m_numberOfRows, m_numberOfColumns];
for (int j = 0; j < m_numberOfRows; ++j)
{
for (int i = 0; i < m_numberOfColumns; ++i)
{
m_mergedPathGrid[j, i] = defGrid;
m_BakePathGrid[j, i] = defGrid;
m_heightMap[j, i] = float.MinValue;
}
}
}
public bool IsBlockPassable(int rowPos, int columnPos)
{
bool bRet = true;
if (rowPos < 0 || columnPos < 0 || rowPos >= m_numberOfRows || columnPos >= m_numberOfColumns)
bRet = false;
else
{
if (m_mergedPathGrid[rowPos, columnPos] != PathGridType.Block &&
m_mergedPathGrid[rowPos, columnPos] != PathGridType.Jump &&
m_mergedPathGrid[rowPos, columnPos] != PathGridType.UserBlock)
{
bRet = true;
}
else
{
bRet = false;
}
}
return bRet;
}
public byte CalBlockWeight(int rowPos, int columnPos, int nCalRange)
{
byte weight = 0;
int nStartRowPos = rowPos - nCalRange;
int nStartColumnPos = columnPos - nCalRange;
int nEndRowPos = rowPos + nCalRange;
int nEndColumnPos = columnPos + nCalRange;
int total = 0;
int totalx = 0;
for (int j = nStartRowPos; j <= nEndRowPos; j++)
{
for (int i = nStartColumnPos; i <= nEndColumnPos; i++)
{
++totalx;
if (j != rowPos || i != columnPos)
{
++total;
if (!IsBlockPassable(j, i))
{
++weight;
}
}
}
}
return weight;
}
public void UpdateWeightPathGridData(int rowPos, int columnPos)
{
if (m_mergedWeightPathGrid == null)
{
m_mergedWeightPathGrid = new byte[m_numberOfRows, m_numberOfColumns];
}
if (rowPos < 0 || columnPos < 0 ||
rowPos >= m_numberOfRows || columnPos >= m_numberOfColumns)
{
return;
}
m_mergedWeightPathGrid[rowPos, columnPos] = CalBlockWeight(rowPos, columnPos, m_weightRadius);
}
public void UpdateWeightPathGridData()
{
if (m_mergedWeightPathGrid == null)
{
m_mergedWeightPathGrid = new byte[m_numberOfRows, m_numberOfColumns];
}
for (int j = 0; j < m_numberOfRows; ++j)
{
for (int i = 0; i < m_numberOfColumns; ++i)
{
m_mergedWeightPathGrid[j, i] = CalBlockWeight(j, i, m_weightRadius);
}
}
}
}
}