NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Area/Around/AroundHelper.cs

131 lines
4.4 KiB
C#

using System.Collections.Generic;
using Gameplay.Level;
using UnityEngine;
namespace Gameplay.Area.Around
{
public static class AroundHelper
{
public static bool useMath = true;
public static void GetAround(int centerIndex,
int stepCount,
List<int> result)
{
if (useMath)
{
GetAroundMath(centerIndex, stepCount, result);
}
else
{
GetAroundLogic(centerIndex, stepCount, result);
}
}
public static void GetAroundMath(int centerIndex,
int stepCount,
List<int> result)
{
var centerPos = GetMathPosV3(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 = LevelManager.Instance.CurrentLevel.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;
}
var cellData = blockDataList[index];
if (!cellData.GetTerrainTypeProperty().selectable)
{
continue;
}
result.Add(index);
}
}
}
public static int GetCellIndexV3(Vector3Int pos)
{
var Nz = pos.z;
var Nx = pos.x + Nz / 2;
return LevelManager.Instance.CurrentLevel.Map.BlockPosition2TGSCellIndex(new Vector2Int(Nz, Nx));
}
public static Vector3Int GetMathPosV3(int cellIndex)
{
var gridPosV2 = LevelManager.Instance.CurrentLevel.Map.TGSCellIndex2BlockPosition(cellIndex);
var Nz = gridPosV2.x;
var Nx = gridPosV2.y;
var Az = Nz;
var Ax = Nx - Nz / 2;
var Ay = -Ax - Az;
return new Vector3Int(Ax, Ay, Az);
}
private static List<int> _cacheWorkList = new List<int>();
private static List<int> _cacheNextWorkList = new List<int>();
private static List<int> _cacheVisitList = new List<int>();
private static List<TGS.Cell> _cacheCellList = new List<TGS.Cell>();
public static void GetAroundLogic(int centerIndex, int stepCount, List<int> result)
{
var workStep = 0;
_cacheWorkList.Clear();
_cacheVisitList.Clear();
_cacheCellList.Clear();
_cacheWorkList.Add(centerIndex);
_cacheVisitList.Add(centerIndex);
var mapTgs = LevelManager.Instance.CurrentLevel.Map.TerrainGridSystem;
while (true)
{
_cacheNextWorkList.Clear();
for (var i = 0; i < _cacheWorkList.Count; ++i)
{
var index = _cacheWorkList[i];
mapTgs.CellGetNeighbours(index, _cacheCellList);
for (int j = 0; j < _cacheCellList.Count; j++)
{
var cell = _cacheCellList[j];
if (_cacheVisitList.Contains(cell.index))
{
continue;
}
result.Add(cell.index);
_cacheNextWorkList.Add(cell.index);
_cacheVisitList.Add(cell.index);
}
}
workStep++;
if (workStep >= stepCount)
{
break;
}
_cacheWorkList.Clear();
_cacheWorkList.AddRange(_cacheNextWorkList);
}
}
}
}