203 lines
6.3 KiB
C#
203 lines
6.3 KiB
C#
using cfg.CampCfg;
|
||
using Cysharp.Threading.Tasks;
|
||
using Framework;
|
||
using Gameplay.Common;
|
||
using PhxhSDK;
|
||
using PhxhSDK.Res;
|
||
using System.Collections.Generic;
|
||
using TGS;
|
||
using UnityEngine;
|
||
|
||
namespace Gameplay.Camp
|
||
{
|
||
/// <summary>
|
||
/// 营地
|
||
/// </summary>
|
||
public sealed class Camp
|
||
{
|
||
/// <summary>
|
||
/// 营地物件根节点
|
||
/// </summary>
|
||
private Transform tsCampObjectRoot;
|
||
/// <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>
|
||
private Dictionary<int, E_CampCellState> dicCellStateByIndex;
|
||
/// <summary>
|
||
/// 记录加载的数据
|
||
/// </summary>
|
||
private HashSet<string> setLoad;
|
||
|
||
/// <summary>
|
||
/// 网格系统
|
||
/// </summary>
|
||
public TerrainGridSystem GridSystem
|
||
{
|
||
get;
|
||
private set;
|
||
}
|
||
|
||
public Camp()
|
||
{
|
||
dicCellByIndex = new Dictionary<int, Cell>();
|
||
dicCellByRowCol = new Dictionary<Vector2Int, Cell>();
|
||
dicCellStateByIndex = new Dictionary<int, E_CampCellState>();
|
||
setLoad = new();
|
||
}
|
||
|
||
public void SetInfo(SceneHandle sceneHandle)
|
||
{
|
||
TerrainGridSystem terrainGridSystem = SceneHandle.GetSceneRootComponent<TerrainGridSystem>(sceneHandle.Scene);
|
||
if (null == terrainGridSystem)
|
||
{
|
||
DebugUtil.LogError($"场景中没有{nameof(TerrainGridSystem)}");
|
||
return;
|
||
}
|
||
|
||
GameObject[] gameObjects = sceneHandle.Scene.GetRootGameObjects();
|
||
foreach (GameObject goRoot in gameObjects)
|
||
{
|
||
if ("CampObjects" == goRoot.name)
|
||
{
|
||
tsCampObjectRoot = goRoot.transform;
|
||
break;
|
||
}
|
||
}
|
||
|
||
GridSystem = terrainGridSystem;
|
||
dicCellByIndex.Clear();
|
||
dicCellByRowCol.Clear();
|
||
dicCellStateByIndex.Clear();
|
||
|
||
foreach (Cell cell in GridSystem.cells)
|
||
{
|
||
dicCellByIndex[cell.index] = cell;
|
||
dicCellByRowCol[new Vector2Int(cell.row, cell.column)] = cell;
|
||
dicCellStateByIndex[cell.index] = E_CampCellState.Empty;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加载建筑
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public async UniTask Load()
|
||
{
|
||
// 测试数据
|
||
int gridIndex = 25;
|
||
int campObjectId = 100004;
|
||
|
||
DataCampObject dataCampObject = TableManager.Instance.Tables.CampObjectConfig.Get(campObjectId);
|
||
if (null == dataCampObject)
|
||
return;
|
||
|
||
GameObject goCampObject = await LoadCampObject(dataCampObject.Path);
|
||
PlaceCampObject(goCampObject, gridIndex, dataCampObject);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 尝试通过索引获取cell
|
||
/// </summary>
|
||
/// <param name="index"></param>
|
||
/// <param name="cell"></param>
|
||
/// <returns></returns>
|
||
public 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>
|
||
public 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>
|
||
public bool TryGetCellStateByIndex(int index, out E_CampCellState cellState)
|
||
{
|
||
if (!dicCellStateByIndex.TryGetValue(index, out cellState))
|
||
return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加载物件
|
||
/// </summary>
|
||
public async UniTask<GameObject> LoadCampObject(string path)
|
||
{
|
||
GameObject goCampObject = await AssetManager.Instance.LoadAssetAsync<GameObject>(path);
|
||
if (null == goCampObject)
|
||
return null;
|
||
|
||
setLoad.Add(path);
|
||
GameObject go = Object.Instantiate(goCampObject, tsCampObjectRoot);
|
||
go.SetLayerEx(LayerDefine.UIView);
|
||
return go;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 放置营地物件
|
||
/// </summary>
|
||
/// <param name="goCampObject"></param>
|
||
/// <param name="gridIndex"></param>
|
||
public void PlaceCampObject(GameObject goCampObject, int gridIndex, DataCampObject dataCampObject)
|
||
{
|
||
Vector3 pos = GridSystem.CellGetPosition(gridIndex);
|
||
goCampObject.transform.position = new Vector3(pos.x, 0f, pos.z);
|
||
|
||
#region 设置营地地板状态
|
||
if (!dicCellByIndex.TryGetValue(gridIndex, out Cell centerCell))
|
||
return;
|
||
|
||
foreach (Vector2 offset in dataCampObject.ListPosOffset)
|
||
{
|
||
Vector2Int gridPos = new(centerCell.row + (int)offset.x, centerCell.column + (int)offset.y); // 计算格子位置
|
||
if (!dicCellByRowCol.TryGetValue(gridPos, out Cell cell))
|
||
continue;
|
||
|
||
if (dicCellStateByIndex.ContainsKey(cell.index))
|
||
dicCellStateByIndex[cell.index] = E_CampCellState.Occupancy;
|
||
}
|
||
#endregion
|
||
}
|
||
|
||
public void Exit()
|
||
{
|
||
tsCampObjectRoot = null;
|
||
GridSystem = null;
|
||
dicCellByIndex.Clear();
|
||
dicCellByRowCol.Clear();
|
||
dicCellStateByIndex.Clear();
|
||
|
||
foreach (string path in setLoad)
|
||
{
|
||
AssetManager.Instance.Unload(path);
|
||
}
|
||
}
|
||
}
|
||
} |