using System.Collections.Generic; using Gameplay.Level; using UnityEngine; namespace Gameplay.Area.Around { public static class AroundHelper { public static bool useMath = true; private static Map _map; public static void Init() { _map = LevelManager.Instance.CurrentLevel.Map; } public static void GetAround(int centerIndex, int stepCount, List result) { if (useMath) { GetAroundMath(centerIndex, stepCount, result); } else { GetAroundLogic(centerIndex, stepCount, result); } } public static Vector3Int GetMathPosV3FromTable(int cellIndex) { var cellList = _map.TerrainGridSystem.cells; var cell = cellList[cellIndex]; return cell.mathPointV3; } public static void GetAroundMath(int centerIndex, int stepCount, List result) { var centerPos = GetMathPosV3FromTable(centerIndex); var minX = centerPos.x - stepCount; var maxX = centerPos.x + stepCount; var minY = centerPos.y - stepCount; var maxY = centerPos.y + stepCount; var minZ = centerPos.z - stepCount; var maxZ = centerPos.z + stepCount; var blockDataList = _map.MapData.BlocksData; for (var x = minX; x <= maxX; ++x) { for (var y = minY; y <= maxY; ++y) { var z = -x - y; if (z < minZ || z > maxZ) { continue; } var pos = new Vector3Int(x, y, z); var index = GetCellIndexV3(pos); if (index == centerIndex) continue; if (index < 0 || index >= blockDataList.Count) { continue; } result.Add(index); } } } public static int GetCellIndexV3(Vector3Int pos) { var col = pos.x; var row = pos.z + (pos.x - (pos.x & 1)) / 2; // DebugUtil.Log("GetCellIndexV3 col:" + col + " row:" + row); if (col < 0 || col >= _map.MapData.Height) { return -1; } if (row < 0 || row >= _map.MapData.Width) { return -1; } return _map.BlockPosition2TGSCellIndex(new Vector2Int(col, row)); } public static Vector3Int GetMathPosV3(int cellIndex) { var gridPosV2 = _map.TGSCellIndex2BlockPosition(cellIndex); var col = gridPosV2.x; var row = gridPosV2.y; var nX = col; var nZ = row - (col - (col & 1)) / 2; var nY = -nX - nZ; return new Vector3Int(nX, nY, nZ); } private static List _cacheCellList = new List(); private static List _workCellList = new List(); private static List _cacheWorkList = new List(); public static void GetAroundLogic(int centerIndex, int stepCount, List result) { var workStep = 0; _cacheCellList.Clear(); var mapTgs = _map.TerrainGridSystem; var allCell = mapTgs.cells; for (int i = 0; i < allCell.Count; i++) { var iCell = allCell[i]; iCell.isVisited = false; } _workCellList.Clear(); var centerCell = allCell[centerIndex]; _workCellList.Add(centerCell); centerCell.isVisited = true; while (true) { _cacheWorkList.Clear(); for (var i = 0; i < _workCellList.Count; ++i) { var workCell = _workCellList[i]; var neighbors = workCell.neighbours; for (int j = 0; j < neighbors.Count; j++) { var iCell = neighbors[j]; if (iCell.isVisited) { continue; } result.Add(iCell.index); _cacheWorkList.Add(iCell); iCell.isVisited = true; } } workStep++; if (workStep >= stepCount) { break; } _workCellList.Clear(); _workCellList.AddRange(_cacheWorkList); } } } }