NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Camp/Camp.Cell.cs

172 lines
5.3 KiB
C#
Raw Normal View History

2024-12-11 15:37:18 +08:00
using cfg.CampCfg;
using Cysharp.Threading.Tasks;
using Framework;
using Gameplay.Common;
using PhxhSDK;
using System.Collections;
using System.Collections.Generic;
using TGS;
using UnityEngine;
using static UnityEngine.InputSystem.Controls.AxisControl;
namespace Gameplay.Camp
{
public sealed partial class Camp
{
/// <summary>
/// 所有Cell,key:Cell索引,value:Cell
/// </summary>
private Dictionary<int, Cell> dicCellByIndex;
/// <summary>
/// 所有Cell,key:Cell行列,Value:Cell
/// </summary>
private Dictionary<Vector2Int, Cell> dicCellByRowCol;
/// <summary>
/// 格子状态,key:格子索引,value:格子状态
/// </summary>
2024-12-13 13:18:50 +08:00
private Dictionary<int, E_CampCellState> dicCellState;
2024-12-11 15:37:18 +08:00
/// <summary>
/// 网格系统
/// </summary>
public TerrainGridSystem GridSystem { get; private set; }
/// <summary>
/// 是否显示所有网格
/// </summary>
public bool IsShowAllGrid
{
set
{
GridSystem.showCells = value;
GridSystem.showTerritories = value;
GridSystem.showTerritoriesInteriorBorders = value;
if (value)
GridSystem.ShowAll();
else
GridSystem.HideAll();
}
}
private void InitCell()
{
dicCellByIndex = new Dictionary<int, Cell>();
dicCellByRowCol = new Dictionary<Vector2Int, Cell>();
2024-12-13 13:18:50 +08:00
dicCellState = new Dictionary<int, E_CampCellState>();
2024-12-11 15:37:18 +08:00
}
private void SetInfoByCell(TerrainGridSystem terrainGridSystem)
{
GridSystem = terrainGridSystem;
dicCellByIndex.Clear();
dicCellByRowCol.Clear();
2024-12-13 13:18:50 +08:00
dicCellState.Clear();
2024-12-11 15:37:18 +08:00
foreach (Cell cell in GridSystem.cells)
{
dicCellByIndex[cell.index] = cell;
dicCellByRowCol[new Vector2Int(cell.row, cell.column)] = cell;
2024-12-13 13:18:50 +08:00
dicCellState[cell.index] = E_CampCellState.Empty;
2024-12-11 15:37:18 +08:00
}
}
private void ExitCell()
{
GridSystem = null;
dicCellByIndex.Clear();
dicCellByRowCol.Clear();
2024-12-13 13:18:50 +08:00
dicCellState.Clear();
2024-12-11 15:37:18 +08:00
}
/// <summary>
/// 尝试通过索引获取cell
/// </summary>
/// <param name="index"></param>
/// <param name="cell"></param>
/// <returns></returns>
private bool TryGetCellByIndex(int index, out Cell cell)
{
if (!dicCellByIndex.TryGetValue(index, out cell))
return false;
return null != cell;
}
/// <summary>
/// 尝试通过行列索引获取cell
/// </summary>
/// <param name="index"></param>
/// <param name="cell"></param>
/// <returns></returns>
private bool TryGetCellByRowCol(Vector2Int rowAndCol, out Cell cell)
{
if (!dicCellByRowCol.TryGetValue(rowAndCol, out cell))
return false;
return null != cell;
}
/// <summary>
/// 尝试通过索引获取cell状态
/// </summary>
/// <param name="index"></param>
/// <param name="cellState"></param>
/// <returns></returns>
private bool TryGetCellStateByIndex(int index, out E_CampCellState cellState)
{
2024-12-13 13:18:50 +08:00
if (!dicCellState.TryGetValue(index, out cellState))
2024-12-11 15:37:18 +08:00
return false;
return true;
}
/// <summary>
/// 设置格子颜色
/// </summary>
private void SetCellColorByCellState(int cellIndex, E_CampCellState cellState)
{
Color color = cellState switch
{
E_CampCellState.Empty => new Color(0f, 0f, 0f, 0f),
E_CampCellState.Occupancy => Color.black,
_ => throw new System.Exception("未处理的格子类型")
};
GridSystem.CellSetColor(cellIndex, color);
}
/// <summary>
/// 设置格子颜色
/// </summary>
private void SetCellColorByCellState(Cell cell, E_CampCellState cellState)
{
Color color = cellState switch
{
E_CampCellState.Empty => new Color(0f, 0f, 0f, 0f),
E_CampCellState.Occupancy => Color.black,
2024-12-11 17:04:41 +08:00
E_CampCellState.Valid => Color.green,
E_CampCellState.Invalid => Color.red,
2024-12-11 15:37:18 +08:00
_ => throw new System.Exception("未处理的格子类型")
};
GridSystem.CellSetColor(cell.index, color);
}
2024-12-12 13:56:12 +08:00
/// <summary>
/// 获取地块位置通过偏移
/// </summary>
/// <param name="x"></param>
/// <param name="pos"></param>
/// <returns></returns>
private Vector2Int GetCellPosByOffset(Vector2Int centerPos, Vector2 offset)
{
Vector2Int offsetInt = new((int)offset.x, (int)offset.y);
if (1 == (centerPos.x & 1) && // 中心在奇数行
1 == (offsetInt.x & 1)) // 偏移格子相距中心奇数行
++offsetInt.y;
return centerPos + offsetInt;
}
2024-12-11 15:37:18 +08:00
}
}