NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Area/BoardView/BoardViewHelper.cs

101 lines
3.7 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using Gameplay.Level;
2025-03-17 15:03:17 +08:00
using TGS;
using UnityEngine;
namespace Gameplay.Area.BoardView
{
public class BoardViewHelper
{
2025-03-17 15:03:17 +08:00
private static List<Cell> _cacheCellIndexList = new List<Cell>();
private static List<int> _cacheWorkCellIndexList = new List<int>();
private static List<int> _cacheNextWorkCellIndexList = new List<int>();
2025-03-17 15:03:17 +08:00
private static Cell _SearchFirstUnVisitedCell(List<Cell> cellIndexList)
{
for (int i = 0; i < cellIndexList.Count; i++)
{
var cell = cellIndexList[i];
if (!cell.isVisited)
{
return cell;
}
}
return null;
}
2025-03-17 17:52:27 +08:00
public static void GenerateBoard(List<int> cellIndexList, List<BoardViewData> result, TerrainGridSystem tgs)
{
_cacheCellIndexList.Clear();
2025-03-17 17:52:27 +08:00
var allCells = tgs.cells;
2025-03-17 15:03:17 +08:00
var allCellCount = allCells.Count;
for (int i = 0; i < allCellCount; i++)
{
var cell = allCells[i];
cell.isVisited = true;
}
var cellCount = cellIndexList.Count;
for (int i = 0; i < cellCount; i++)
{
var cellIndex = cellIndexList[i];
2025-03-17 17:52:27 +08:00
var cell = tgs.cells[cellIndex];
2025-03-17 15:03:17 +08:00
_cacheCellIndexList.Add(cell);
cell.isVisited = false;
}
2025-03-17 17:52:27 +08:00
var tgsSystem = tgs;
BoardViewData workViewData = null;
2025-03-17 15:03:17 +08:00
_cacheWorkCellIndexList.Clear();
_cacheNextWorkCellIndexList.Clear();
var workCellIndexList = _cacheWorkCellIndexList;
var nextWorkCellIndexList = _cacheNextWorkCellIndexList;
while (true)
{
if (workViewData == null)
{
2025-03-17 15:03:17 +08:00
var searchCell = _SearchFirstUnVisitedCell(_cacheCellIndexList);
if (searchCell == null)
{
break;
}
workCellIndexList.Clear();
2025-03-17 15:03:17 +08:00
searchCell.isVisited = true;
var searchCellIndex = searchCell.index;
workViewData = new BoardViewData();
2025-03-17 15:03:17 +08:00
workViewData.cellIndexList.Add(searchCellIndex);
result.Add(workViewData);
2025-03-17 15:03:17 +08:00
workCellIndexList.Add(searchCellIndex);
}
else
{
nextWorkCellIndexList.Clear();
for (int j = 0; j < workCellIndexList.Count; j++)
{
var workCellIndex = workCellIndexList[j];
var aroundCell = tgsSystem.CellGetNeighbours(workCellIndex);
for (int i = 0; i < aroundCell.Count; i++)
{
var cell = aroundCell[i];
2025-03-17 15:03:17 +08:00
if (cell.isVisited) continue;
workViewData.cellIndexList.Add(cell.index);
nextWorkCellIndexList.Add(cell.index);
2025-03-17 15:03:17 +08:00
cell.isVisited = true;
}
}
if (nextWorkCellIndexList.Count == 0)
{
workViewData = null;
}
else
{
// switch work list
workCellIndexList.Clear();
workCellIndexList.AddRange(nextWorkCellIndexList);
}
}
}
}
}
}