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

86 lines
2.6 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;
private void InitUnit()
{
setUnit = new HashSet<CampUnitBase>();
}
private void ExitUnit()
{
foreach (CampUnitBase unit in setUnit)
{
unit.Dispose();
}
setUnit.Clear();
}
/// <summary>
/// 检查能否放置
/// </summary>
/// <returns></returns>
public bool CheckPlace(int cellIndex, params Vector2[] offsets)
{
if (!TryGetCellByIndex(cellIndex, out Cell centerCell))
return false;
foreach (Vector2 offset in offsets)
{
Vector2Int cellPos = new(centerCell.row + (int)offset.x, centerCell.column + (int)offset.y); // 计算格子位置
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 (Vector2 offset in unit.Cfg.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
Vector3 pos = GridSystem.CellGetPosition(centerCell.index);
unit.SetPos(new Vector3(pos.x, 0f, pos.z)); // 设置建筑物位置
setUnit.Add(unit);
}
}
}