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 { /// /// 营地单位集合 /// private HashSet setUnit; /// /// 所有建筑单位 /// private Dictionary dicUnit; private void InitUnit() { setUnit = new HashSet(); dicUnit = new Dictionary(); } private void ExitUnit() { foreach (CampUnitBase unit in setUnit) { unit.Dispose(); } setUnit.Clear(); dicUnit.Clear(); } /// /// 检查能否放置 /// /// public bool CheckPlace(int cellIndex, params Vector2[] offsets) { if (!TryGetCellByIndex(cellIndex, out Cell centerCell)) return false; foreach (Vector2 offset in offsets) { Vector2Int cellPos = GetCellPosByOffset(new Vector2Int(centerCell.row, centerCell.column), 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; } /// /// 放置营地物件 /// /// /// public void PlaceCampItem(BuildingCampUnit unit) { #region 设置营地地板状态 if (!dicCellByIndex.TryGetValue(unit.CellIndex, out Cell centerCell)) return; foreach (Vector2 offset in unit.Cfg.ListPosOffset) { Vector2Int cellPos = GetCellPosByOffset(new Vector2Int(centerCell.row, centerCell.column), 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); } /// /// 尝试获取已经放置的单位 /// /// public bool TryGetPlaceUnit(int index, out CampUnitBase campUnitBase) { if (!dicUnit.TryGetValue(index, out campUnitBase)) return false; return null != campUnitBase; } } }