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

105 lines
3.2 KiB
C#

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;
namespace Gameplay.Camp
{
public sealed partial class Camp
{
/// <summary>
/// 营地单位集合
/// </summary>
private HashSet<CampUnitBase> setUnit;
/// <summary>
/// 所有建筑单位
/// </summary>
private Dictionary<int, CampUnitBase> dicUnit;
private void InitUnit()
{
setUnit = new HashSet<CampUnitBase>();
dicUnit = new Dictionary<int, CampUnitBase>();
}
private void ExitUnit()
{
foreach (CampUnitBase unit in setUnit)
{
unit.Dispose();
}
setUnit.Clear();
dicUnit.Clear();
}
/// <summary>
/// 检查能否放置
/// </summary>
/// <returns></returns>
public bool CheckPlace(int cellIndex, params cfg.vector2[] offsets)
{
if (!TryGetCellByIndex(cellIndex, out Cell centerCell))
return false;
foreach (cfg.vector2 offset in offsets)
{
Vector2Int cellPos = GetCellPosByOffset(new Vector2Int(centerCell.row, centerCell.column), CommonUtils.VectorTrans(offset)); // 计算格子位置
if (!dicCellByRowCol.TryGetValue(cellPos, out Cell cell))
return false;
if (!TryGetCellStateByIndex(cell.index, out E_CampCellState cellState))
return false;
if (E_CampCellState.Empty != cellState)
return false;
}
return true;
}
/// <summary>
/// 放置营地物件
/// </summary>
/// <param name="goCampObject"></param>
/// <param name="gridIndex"></param>
public void PlaceCampItem(BuildingCampUnit unit)
{
#region 设置营地地板状态
if (!dicCellByIndex.TryGetValue(unit.CellIndex, out Cell centerCell))
return;
foreach (cfg.vector2 offset in unit.Cfg.ListPosOffset)
{
Vector2Int cellPos = GetCellPosByOffset(new Vector2Int(centerCell.row, centerCell.column), CommonUtils.VectorTrans(offset));
if (!dicCellByRowCol.TryGetValue(cellPos, out Cell cell))
continue;
dicCellState[cell.index] = E_CampCellState.Occupancy;
dicUnit[cell.index] = unit;
}
#endregion
Vector3 pos = GridSystem.CellGetPosition(centerCell.index);
unit.SetPos(new Vector3(pos.x, 0f, pos.z)); // 设置建筑物位置
setUnit.Add(unit);
}
/// <summary>
/// 尝试获取已经放置的单位
/// </summary>
/// <returns></returns>
public bool TryGetPlaceUnit(int index, out CampUnitBase campUnitBase)
{
if (!dicUnit.TryGetValue(index, out campUnitBase))
return false;
return null != campUnitBase;
}
}
}