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

176 lines
6.2 KiB
C#

using System.Collections.Generic;
using Gameplay.Area.Around;
using Gameplay.Common;
using Gameplay.Level;
using PhxhSDK;
using TGS;
using UnityEngine;
namespace Gameplay.Area.BoardView
{
public class BoardViewData
{
public TGS.Territory tgsTerritory;
public GameObject borderObj;
public List<int> cellIndexList = new List<int>();
private List<int> _cacheBoardSideIndexList = new List<int>();
private static readonly int Prop_HintColor1 = Shader.PropertyToID("_Color");
private static readonly int Prop_HintColor2 = Shader.PropertyToID("_SecondColor");
public void Destroy(TerrainGridSystem tgs)
{
var tIndex = tgs.TerritoryGetIndex(tgsTerritory);
tgs.TerritoryDestroy(tIndex);
if (borderObj != null)
{
Object.Destroy(borderObj);
borderObj = null;
}
}
public void CreateBoardViewObj(TerrainGridSystem tgs, Color hintColor1, Color hintColor2)
{
tgsTerritory = tgs.TerritoryCreate(cellIndexList);
borderObj = tgs.ForceDrawTerritoryDrawInteriorBorder(tgsTerritory, 0, 1, hintColor1, hintColor2);
if (borderObj == null)
{
DebugUtil.LogError("GenerateBoard: borderObj is null: cellIndexList.count:" + cellIndexList.Count);
}
var getMaterial = borderObj.transform.GetChild(0).GetComponent<MeshRenderer>().material;
getMaterial.SetColor(Prop_HintColor1, hintColor1);
getMaterial.SetColor(Prop_HintColor2, hintColor2);
}
public void CreateBoardSideView(TerrainGridSystem tgs)
{
CalcLineBoard(tgs);
var objPool = AreaManager.instance.moveAreaView.boardLineObjPool;
for (int i = 0; i < _cacheBoardSideIndexList.Count; i++)
{
var boardIndex = _cacheBoardSideIndexList[i];
objPool.CreateBoardAt(boardIndex);
}
}
public void CreateLegionBattleBoardSideView(TerrainGridSystem tgs, BoardLineObjPool boardLineObjPool)
{
CalcLineBoard(tgs);
for (int i = 0; i < _cacheBoardSideIndexList.Count; i++)
{
int boardIndex = _cacheBoardSideIndexList[i];
boardLineObjPool.CreateBoardAt(boardIndex);
}
}
public void CalcLineBoard(TerrainGridSystem tgs)
{
// 取出所有在边界上的点
// 判断每个格子周围是否在这个区域内
var cells = tgs.cells;
// 先遍历一遍, 重置所有格子的状态
for (int i = 0; i < cells.Count; i++)
{
var cell = cells[i];
cell.isVisited = false;
cell.isInArea = false;
}
// 标记在区域内的格子
for (int i = 0; i < cellIndexList.Count; i++)
{
var checkCellIndex = cellIndexList[i];
var cell = cells[checkCellIndex];
cell.isInArea = true;
}
// 计算边界
_cacheBoardSideIndexList.Clear();
for (int i = 0; i < cellIndexList.Count; i++)
{
var checkCellIndex = cellIndexList[i];
var cell = cells[checkCellIndex];
if (_CheckAllAroundInArea(cell))
{
continue;
}
// 计算六个指定相邻格子是否在区域内
GetBoardSideMath(cell, 1, _cacheBoardSideIndexList, cellIndexList);
}
}
public static void GetBoardSideMath(TGS.Cell centerCell,
int stepCount, List<int> boardSideCellIndexList, List<int> areaCellIndexList)
{
var centerPos = centerCell.mathPointV3;
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 cacheBoardIndex = 0;
for (var x = minX; x <= maxX; ++x)
{
for (var y = minY; y <= maxY; ++y)
{
if (x == centerPos.x && y == centerPos.y)
{
continue;
}
var z = -x - y;
if (z < minZ || z > maxZ)
{
continue;
}
cacheBoardIndex++;
var pos = new Vector3Int(x, y, z);
var calcIndex = AroundHelper.GetCellIndexV3(pos);
var centerCellIndex = centerCell.index;
if (calcIndex < 0)
{
// 说明是边界
var sideIndex = _GetBoardIndexBySide(centerCellIndex, cacheBoardIndex);
boardSideCellIndexList.Add(sideIndex);
}
else
{
var cell = AreaManager.instance.GetCellByIndex(calcIndex);
if (!cell.isInArea)
{
var sideIndex = _GetBoardIndexBySide(centerCellIndex, cacheBoardIndex);
boardSideCellIndexList.Add(sideIndex);
}
}
}
}
}
private static int _GetBoardIndexBySide(int centerCellIndex, int side)
{
return centerCellIndex * 10 + side;
}
private static bool _CheckAllAroundInArea(TGS.Cell checkCell)
{
var aroundCellList = checkCell.neighbours;
if (aroundCellList.Count < 6)
{
// 边界格子
return false;
}
for (int i = 0; i < aroundCellList.Count; i++)
{
var cell = aroundCellList[i];
if (!cell.isInArea)
{
return false;
}
}
return true;
}
}
}