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

172 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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>
/// 所有Cellkey: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>
private Dictionary<int, E_CampCellState> dicCellState;
/// <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>();
dicCellState = new Dictionary<int, E_CampCellState>();
}
private void SetInfoByCell(TerrainGridSystem terrainGridSystem)
{
GridSystem = terrainGridSystem;
dicCellByIndex.Clear();
dicCellByRowCol.Clear();
dicCellState.Clear();
foreach (Cell cell in GridSystem.cells)
{
dicCellByIndex[cell.index] = cell;
dicCellByRowCol[new Vector2Int(cell.row, cell.column)] = cell;
dicCellState[cell.index] = E_CampCellState.Empty;
}
}
private void ExitCell()
{
GridSystem = null;
dicCellByIndex.Clear();
dicCellByRowCol.Clear();
dicCellState.Clear();
}
/// <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)
{
if (!dicCellState.TryGetValue(index, out cellState))
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,
E_CampCellState.Valid => Color.green,
E_CampCellState.Invalid => Color.red,
_ => throw new System.Exception("未处理的格子类型")
};
GridSystem.CellSetColor(cell.index, color);
}
/// <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;
}
}
}