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

101 lines
3.7 KiB
C#

using System.Collections.Generic;
using Gameplay.Level;
using TGS;
using UnityEngine;
namespace Gameplay.Area.BoardView
{
public class BoardViewHelper
{
private static List<Cell> _cacheCellIndexList = new List<Cell>();
private static List<int> _cacheWorkCellIndexList = new List<int>();
private static List<int> _cacheNextWorkCellIndexList = new List<int>();
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;
}
public static void GenerateBoard(List<int> cellIndexList, List<BoardViewData> result, TerrainGridSystem tgs)
{
_cacheCellIndexList.Clear();
var allCells = tgs.cells;
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];
var cell = tgs.cells[cellIndex];
_cacheCellIndexList.Add(cell);
cell.isVisited = false;
}
var tgsSystem = tgs;
BoardViewData workViewData = null;
_cacheWorkCellIndexList.Clear();
_cacheNextWorkCellIndexList.Clear();
var workCellIndexList = _cacheWorkCellIndexList;
var nextWorkCellIndexList = _cacheNextWorkCellIndexList;
while (true)
{
if (workViewData == null)
{
var searchCell = _SearchFirstUnVisitedCell(_cacheCellIndexList);
if (searchCell == null)
{
break;
}
workCellIndexList.Clear();
searchCell.isVisited = true;
var searchCellIndex = searchCell.index;
workViewData = new BoardViewData();
workViewData.cellIndexList.Add(searchCellIndex);
result.Add(workViewData);
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];
if (cell.isVisited) continue;
workViewData.cellIndexList.Add(cell.index);
nextWorkCellIndexList.Add(cell.index);
cell.isVisited = true;
}
}
if (nextWorkCellIndexList.Count == 0)
{
workViewData = null;
}
else
{
// switch work list
workCellIndexList.Clear();
workCellIndexList.AddRange(nextWorkCellIndexList);
}
}
}
}
}
}