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

72 lines
2.8 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using Gameplay.Level;
using UnityEngine;
namespace Gameplay.Area.BoardView
{
public class BoardViewHelper
{
private static List<int> _cacheCellIndexList = new List<int>();
public static List<BoardViewData> GenerateBoard(List<int> cellIndexList)
{
_cacheCellIndexList.Clear();
_cacheCellIndexList.AddRange(cellIndexList);
var result = new List<BoardViewData>();
var tgsSystem = LevelManager.Instance.CurrentLevel.Map.TerrainGridSystem;
BoardViewData workViewData = null;
List<int> workCellIndexList = new List<int>();
List<int> nextWorkCellIndexList = new List<int>();
while (_cacheCellIndexList.Count > 0)
{
if (workViewData == null)
{
workCellIndexList.Clear();
var workCellIndex = _cacheCellIndexList[^1];
_cacheCellIndexList.RemoveAt(_cacheCellIndexList.Count - 1);
workViewData = new BoardViewData();
workViewData.cellIndexList.Add(workCellIndex);
result.Add(workViewData);
workCellIndexList.Add(workCellIndex);
}
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];
var findIndex = _cacheCellIndexList.IndexOf(cell.index);
if (findIndex == -1)
{
continue;
}
workViewData.cellIndexList.Add(cell.index);
// fast remove
_cacheCellIndexList[findIndex] = _cacheCellIndexList[^1];
_cacheCellIndexList.RemoveAt(_cacheCellIndexList.Count - 1);
nextWorkCellIndexList.Add(cell.index);
}
}
if (nextWorkCellIndexList.Count == 0)
{
workViewData = null;
}
else
{
// switch work list
workCellIndexList.Clear();
workCellIndexList.AddRange(nextWorkCellIndexList);
}
}
}
return result;
}
}
}