using System.Collections.Generic; using Gameplay.Level; using TGS; using UnityEngine; namespace Gameplay.Area.BoardView { public class BoardViewHelper { private static List _cacheCellIndexList = new List(); private static List _cacheWorkCellIndexList = new List(); private static List _cacheNextWorkCellIndexList = new List(); private static Cell _SearchFirstUnVisitedCell(List 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 cellIndexList, List 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); } } } } } }