using cfg.LegionBattleCfg; using Framework; using Google.Protobuf.Collections; using NLDPB; using System.Collections.Generic; using UnityEngine; namespace Gameplay.LegionBattle { /// /// 军团战建筑数据 /// public sealed class LegionBuildingData { /// /// 建筑索引 /// public int Index { get; private set; } /// /// 建筑坐标 /// public Vector2Int Coordinate { get; private set; } /// /// 建筑内的玩家 /// public List listActorId { get; private set; } /// /// 当前阵营 /// public BattlefieldActorCamp CurCamp { get; private set; } /// /// 防守成功次数 /// public int DefenceCount { get; private set; } /// /// 被攻占次数 /// public int AttackedTotal { get; private set; } /// /// 据点配置 /// public DataLegionBattleBuilding Cfg { get; private set; } /// /// 所占所有格子坐标 /// public HashSet SetAllIndex { get; private set; } /// /// 格子外一圈的坐标 /// public List ListOutSide { get; private set; } /// /// 是否集结中 /// public bool IsAssemble; /// /// 战斗id /// public uint FightId; /// /// 建筑名 /// public string Name { get { return CommonUtils.GetLocalizeText(Cfg.NameKey); } } public LegionBuildingData(LegionLevelData.Building building, int width, int height, Map map) { Index = building.position; Coordinate = map.TGSCellIndex2BlockPosition(Index); listActorId = new List(); SetAllIndex = new HashSet(); ListOutSide = new List(); Cfg = TableManager.Instance.Tables.LegionBattleBuilding.Get(building.buildingID); #region 处理计算建筑所占地格 Dictionary dicPos = new() // 记录建筑所占地格 { { map.TGSCellIndex2BlockPosition(Index), false } }; HashSet setNew = new(); List listOld = new(); #region TODO 临时处理, 建筑自身所占格子 int range = 1; while (range < Cfg.BuildingRange) { foreach (var kv in dicPos) { if (kv.Value) continue; if (kv.Key.x < 0 || kv.Key.x >= width || kv.Key.y < 0 || kv.Key.y > height) continue; if (1 == (kv.Key.x & 1)) // 中心在奇数行 { Vector2Int newPos = new(kv.Key.x, kv.Key.y - 1); // 左 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x - 1, kv.Key.y); // 左下 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x - 1, kv.Key.y + 1); // 右下 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x, kv.Key.y + 1); // 右 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x + 1, kv.Key.y + 1); // 右上 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x + 1, kv.Key.y); // 左上 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); } else // 中心在偶数行 { Vector2Int newPos = new(kv.Key.x, kv.Key.y - 1); // 左 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x - 1, kv.Key.y - 1); // 左下 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x - 1, kv.Key.y); // 右下 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x, kv.Key.y + 1); // 右 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x + 1, kv.Key.y); // 右上 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x + 1, kv.Key.y - 1); // 左上 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); } listOld.Add(kv.Key); } foreach (Vector2Int pos in listOld) { dicPos[pos] = true; } foreach (Vector2Int pos in setNew) { dicPos[pos] = false; } listOld.Clear(); setNew.Clear(); ++range; } foreach (Vector2Int pos in dicPos.Keys) { if (pos.x < 0 || pos.x >= width || pos.y < 0 || pos.y > height) continue; SetAllIndex.Add(map.BlockPosition2TGSCellIndex(pos)); } #endregion #region 记录外一圈 foreach (var kv in dicPos) { if (kv.Value) continue; if (kv.Key.x < 0 || kv.Key.x >= width || kv.Key.y < 0 || kv.Key.y > height) continue; if (1 == (kv.Key.x & 1)) // 中心在奇数行 { Vector2Int newPos = new(kv.Key.x, kv.Key.y - 1); // 左 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x - 1, kv.Key.y); // 左下 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x - 1, kv.Key.y + 1); // 右下 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x, kv.Key.y + 1); // 右 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x + 1, kv.Key.y + 1); // 右上 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x + 1, kv.Key.y); // 左上 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); } else // 中心在偶数行 { Vector2Int newPos = new(kv.Key.x, kv.Key.y - 1); // 左 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x - 1, kv.Key.y - 1); // 左下 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x - 1, kv.Key.y); // 右下 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x, kv.Key.y + 1); // 右 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x + 1, kv.Key.y); // 右上 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); newPos = new(kv.Key.x + 1, kv.Key.y - 1); // 左上 if (!dicPos.ContainsKey(newPos)) setNew.Add(newPos); } } foreach (Vector2Int pos in setNew) { ListOutSide.Add(pos); } #endregion #endregion } public void CopyData(MSG_LB_Building data) { Coordinate = new Vector2Int((int)data.Nz, (int)data.Nx); listActorId.Clear(); listActorId.AddRange(data.Actors); if (listActorId.Count <= 0) CurCamp = BattlefieldActorCamp.BacNone; else CurCamp = LegionBattleManager.Instance.GetCampByActorId(listActorId[0]); } public void CopyData(BattlefieldActorCamp camp, int defCount, int total, RepeatedField actorIds) { CurCamp = camp; DefenceCount = defCount; AttackedTotal = total; listActorId.Clear(); listActorId.AddRange(actorIds); if (listActorId.Count <= 0) CurCamp = BattlefieldActorCamp.BacNone; else CurCamp = LegionBattleManager.Instance.GetCampByActorId(listActorId[0]); } } }