NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleHexesData.cs

190 lines
6.1 KiB
C#

using Gameplay.Area.Around;
using NLDPB;
using System.Collections.Generic;
using TGS;
using UnityEngine;
namespace Gameplay.LegionBattle
{
/// <summary>
/// 军团战格子数据
/// </summary>
public sealed class LegionBattleHexesData
{
/// <summary>
/// A阵营地板
/// </summary>
private GameObject goACell;
/// <summary>
/// B阵营地板
/// </summary>
private GameObject goBCell;
/// <summary>
/// 索引
/// </summary>
public int Index { get; private set; }
/// <summary>
/// 坐标
/// </summary>
public Vector2Int Coordinate { get; private set; }
/// <summary>
/// 所属阵营
/// </summary>
public BattlefieldActorCamp Camp { get; private set; }
/// <summary>
/// 周围格子
/// </summary>
public List<Vector2Int> ListAroundCell { get; private set; }
/// <summary>
/// 周围边界索引
/// </summary>
public List<int> ListAroundSideIndex { get; private set; }
public LegionBattleHexesData(Cell cell, Vector2Int coordinate, GameObject aCell, GameObject bCell)
{
Index = cell.index;
Coordinate = coordinate;
Camp = BattlefieldActorCamp.BacNone;
goACell = aCell;
goBCell = bCell;
ListAroundCell = new(LegionBattle.MAX_SIDE);
ListAroundSideIndex = new(LegionBattle.MAX_SIDE);
GetAroundCell();
GetBoardSideMath(cell, ListAroundSideIndex);
}
/// <summary>
/// 设置阵营
/// </summary>
public void SetCamp(BattlefieldActorCamp camp)
{
Camp = camp;
if (BattlefieldActorCamp.CampA == Camp)
{
goACell.SetActive(true);
goBCell.SetActive(false);
}
else if (BattlefieldActorCamp.CampB == Camp)
{
goACell.SetActive(false);
goBCell.SetActive(true);
}
else
{
goACell.SetActive(false);
goBCell.SetActive(false);
}
}
/// <summary>
/// 更新边界
/// </summary>
public void UpdateSide()
{
for (int i = 0; i < ListAroundSideIndex.Count; ++i)
{
LegionBattleManager.Instance.CurBattle.UpdateSideShow(Camp, ListAroundCell[i], ListAroundSideIndex[i]);
}
}
/// <summary>
/// 获取周围地板
/// </summary>
private void GetAroundCell()
{
if (1 == (Coordinate.x & 1)) // 中心在奇数行
{
Vector2Int newPos = new(Coordinate.x - 1, Coordinate.y + 1); // 右下
ListAroundCell.Add(newPos);
newPos = new(Coordinate.x - 1, Coordinate.y); // 左下
ListAroundCell.Add(newPos);
newPos = new(Coordinate.x, Coordinate.y + 1); // 右
ListAroundCell.Add(newPos);
newPos = new(Coordinate.x, Coordinate.y - 1); // 左
ListAroundCell.Add(newPos);
newPos = new(Coordinate.x + 1, Coordinate.y + 1); // 右上
ListAroundCell.Add(newPos);
newPos = new(Coordinate.x + 1, Coordinate.y); // 左上
ListAroundCell.Add(newPos);
}
else // 中心在偶数行
{
Vector2Int newPos = new(Coordinate.x - 1, Coordinate.y); // 右下
ListAroundCell.Add(newPos);
newPos = new(Coordinate.x - 1, Coordinate.y - 1); // 左下
ListAroundCell.Add(newPos);
newPos = new(Coordinate.x, Coordinate.y + 1); // 右
ListAroundCell.Add(newPos);
newPos = new(Coordinate.x, Coordinate.y - 1); // 左
ListAroundCell.Add(newPos);
newPos = new(Coordinate.x + 1, Coordinate.y); // 右上
ListAroundCell.Add(newPos);
newPos = new(Coordinate.x + 1, Coordinate.y - 1); // 左上
ListAroundCell.Add(newPos);
}
}
private int GetBoardIndexBySide(int centerCellIndex, int side)
{
return centerCellIndex * 10 + side;
}
private void GetBoardSideMath(Cell centerCell, List<int> boardSideCellIndexList)
{
Vector3Int centerPos = centerCell.mathPointV3;
int minX = centerPos.x - 1;
int maxX = centerPos.x + 1;
int minY = centerPos.y - 1;
int maxY = centerPos.y + 1;
int minZ = centerPos.z - 1;
int maxZ = centerPos.z + 1;
int cacheBoardIndex = 0;
for (int x = minX; x <= maxX; ++x)
{
for (int y = minY; y <= maxY; ++y)
{
if (x == centerPos.x && y == centerPos.y)
continue;
int z = -x - y;
if (z < minZ || z > maxZ)
continue;
cacheBoardIndex++;
Vector3Int pos = new(x, y, z);
int calcIndex = AroundHelper.GetCellIndexV3(pos);
int centerCellIndex = centerCell.index;
if (calcIndex < 0)
{
// 说明是边界
int sideIndex = GetBoardIndexBySide(centerCellIndex, cacheBoardIndex);
boardSideCellIndexList.Add(sideIndex);
}
else
{
Cell cell = LegionBattleManager.Instance.CurBattle.Map.TerrainGridSystem.cells[calcIndex];
if (!cell.isInArea)
{
int sideIndex = GetBoardIndexBySide(centerCellIndex, cacheBoardIndex);
boardSideCellIndexList.Add(sideIndex);
}
}
}
}
}
}
}