From 0df0a0e5be3e542ae011270b9ebea9096f05b7ac Mon Sep 17 00:00:00 2001 From: yurui <952870061@qq.com> Date: Mon, 31 Mar 2025 15:23:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=9B=E5=9B=A2=E6=88=98=E8=BE=B9=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Data/LegionBattleHexesData.cs | 136 +++++++++- .../LegionBattle/Data/LegionBattleSideData.cs | 36 +++ .../Data/LegionBattleSideData.cs.meta | 11 + .../LegionBattle/LegionBattle.Info.cs | 8 +- .../Gameplay/LegionBattle/LegionBattle.Map.cs | 234 +++++++++++------- 5 files changed, 318 insertions(+), 107 deletions(-) create mode 100644 ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleSideData.cs create mode 100644 ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleSideData.cs.meta diff --git a/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleHexesData.cs b/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleHexesData.cs index 294ad4d5056..21dae191cf2 100644 --- a/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleHexesData.cs +++ b/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleHexesData.cs @@ -1,8 +1,7 @@ -using Gameplay.Area; +using Gameplay.Area.Around; using NLDPB; -using PhxhSDK; -using System.Collections; using System.Collections.Generic; +using TGS; using UnityEngine; namespace Gameplay.LegionBattle @@ -20,27 +19,41 @@ namespace Gameplay.LegionBattle /// B阵营地板 /// private GameObject goBCell; + /// /// 索引 /// public int Index { get; private set; } /// + /// 坐标 + /// + public Vector2Int Coordinate { get; private set; } + /// /// 所属阵营 /// public BattlefieldActorCamp Camp { get; private set; } + /// + /// 周围格子 + /// + public List ListAroundCell { get; private set; } + /// + /// 周围边界索引 + /// + public List ListAroundSideIndex { get; private set; } - public LegionBattleHexesData(int index, Vector3 pos, Transform root, GameObject aCell, GameObject bCell) + public LegionBattleHexesData(Cell cell, Vector2Int coordinate, GameObject aCell, GameObject bCell) { - Index = index; + Index = cell.index; + Coordinate = coordinate; Camp = BattlefieldActorCamp.BacNone; - goACell = Object.Instantiate(aCell, root); // 实例化地板 - goACell.transform.position = pos; - goACell.SetActive(false); + goACell = aCell; + goBCell = bCell; - goBCell = Object.Instantiate(bCell, root); // 实例化地板 - goBCell.transform.position = pos; - goBCell.SetActive(false); + ListAroundCell = new(6); + ListAroundSideIndex = new(6); + GetAroundCell(); + GetBoardSideMath(cell, ListAroundSideIndex); } /// @@ -66,5 +79,106 @@ namespace Gameplay.LegionBattle goBCell.SetActive(false); } } + + public void UpdateSide() + { + for (int i = 0; i < ListAroundSideIndex.Count; ++i) + { + LegionBattleManager.Instance.CurBattle.UpdateSideShow(Camp, ListAroundCell[i], ListAroundSideIndex[i]); + } + } + + private void GetAroundCell() + { + if (1 == (Index & 1)) // 中心在奇数行 + { + Vector2Int newPos = new(Coordinate.x - 1, Coordinate.y + 1); // 右下 + ListAroundCell.Add(newPos); + + newPos = new(Coordinate.x - 1, Coordinate.y); // 左下 + ListAroundCell.Add(newPos); + + newPos = new(Coordinate.x, Coordinate.y + 1); // 右 + ListAroundCell.Add(newPos); + + newPos = new(Coordinate.x, Coordinate.y - 1); // 左 + ListAroundCell.Add(newPos); + + newPos = new(Coordinate.x + 1, Coordinate.y + 1); // 右上 + ListAroundCell.Add(newPos); + + newPos = new(Coordinate.x + 1, Coordinate.y); // 左上 + ListAroundCell.Add(newPos); + } + else // 中心在偶数行 + { + Vector2Int newPos = new(Coordinate.x - 1, Coordinate.y); // 右下 + ListAroundCell.Add(newPos); + + newPos = new(Coordinate.x - 1, Coordinate.y - 1); // 左下 + ListAroundCell.Add(newPos); + + newPos = new(Coordinate.x, Coordinate.y + 1); // 右 + ListAroundCell.Add(newPos); + + newPos = new(Coordinate.x, Coordinate.y - 1); // 左 + ListAroundCell.Add(newPos); + + newPos = new(Coordinate.x + 1, Coordinate.y); // 右上 + ListAroundCell.Add(newPos); + + newPos = new(Coordinate.x + 1, Coordinate.y - 1); // 左上 + ListAroundCell.Add(newPos); + } + } + + private int GetBoardIndexBySide(int centerCellIndex, int side) + { + return centerCellIndex * 10 + side; + } + + private void GetBoardSideMath(Cell centerCell, List boardSideCellIndexList) + { + Vector3Int centerPos = centerCell.mathPointV3; + int minX = centerPos.x - 1; + int maxX = centerPos.x + 1; + int minY = centerPos.y - 1; + int maxY = centerPos.y + 1; + int minZ = centerPos.z - 1; + int maxZ = centerPos.z + 1; + int cacheBoardIndex = 0; + for (int x = minX; x <= maxX; ++x) + { + for (int y = minY; y <= maxY; ++y) + { + if (x == centerPos.x && y == centerPos.y) + continue; + + int z = -x - y; + if (z < minZ || z > maxZ) + continue; + + cacheBoardIndex++; + Vector3Int pos = new(x, y, z); + int calcIndex = AroundHelper.GetCellIndexV3(pos); + int centerCellIndex = centerCell.index; + if (calcIndex < 0) + { + // 说明是边界 + int sideIndex = GetBoardIndexBySide(centerCellIndex, cacheBoardIndex); + boardSideCellIndexList.Add(sideIndex); + } + else + { + Cell cell = LegionBattleManager.Instance.CurBattle.Map.TerrainGridSystem.cells[calcIndex]; + if (!cell.isInArea) + { + int sideIndex = GetBoardIndexBySide(centerCellIndex, cacheBoardIndex); + boardSideCellIndexList.Add(sideIndex); + } + } + } + } + } } } \ No newline at end of file diff --git a/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleSideData.cs b/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleSideData.cs new file mode 100644 index 00000000000..167ee990970 --- /dev/null +++ b/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleSideData.cs @@ -0,0 +1,36 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Gameplay.LegionBattle +{ + /// + /// 军团战边界数据 + /// + public sealed class LegionBattleSideData + { + private GameObject goSide; + private bool isActive; + + public bool IsActive + { + get { return isActive; } + set + { + if (isActive == value) + return; + + goSide.SetActive(value); + } + } + + public LegionBattleSideData(Vector3 pos, GameObject go) + { + goSide = go; + goSide.transform.position = pos; + + goSide.SetActive(false); + isActive = false; + } + } +} \ No newline at end of file diff --git a/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleSideData.cs.meta b/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleSideData.cs.meta new file mode 100644 index 00000000000..e45170f4c4e --- /dev/null +++ b/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/Data/LegionBattleSideData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5818b78ba03140d478d5344dc38cd9d4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/LegionBattle.Info.cs b/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/LegionBattle.Info.cs index b50277e3cf0..a7f898a77eb 100644 --- a/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/LegionBattle.Info.cs +++ b/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/LegionBattle.Info.cs @@ -446,14 +446,16 @@ namespace Gameplay.LegionBattle { for (int i = 0; i < listX.Count; ++i) { - if (!dicHexesData.TryGetValue(new Vector2Int(listX[i], listY[i]), out LegionBattleHexesData hexesData)) + Vector2Int pos = new(listX[i], listY[i]); + if (!dicHexesData.TryGetValue(pos, out LegionBattleHexesData hexesData)) return; hexesData.SetCamp((BattlefieldActorCamp)listCamp[i]); - //UpdateCellColorByIndex(hexesData); + + setUpdateHexes.Add(pos); } - UpdateCellLine(); + UpdateAllSide(); } /// diff --git a/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/LegionBattle.Map.cs b/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/LegionBattle.Map.cs index f53342f0d68..6865e8dcc5f 100644 --- a/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/LegionBattle.Map.cs +++ b/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/LegionBattle.Map.cs @@ -17,28 +17,37 @@ namespace Gameplay.LegionBattle public sealed partial class LegionBattle : IUpdatable, ILoadable { /// - /// 蓝方势力地板颜色 + /// 最大边数 /// - private readonly Color CAMP_A_CELL_COLOR = new(0f, 0f, 1f, 0.05f); - /// - /// 红方势力地板颜色 - /// - private readonly Color CAMP_B_CELL_COLOR = new(1f, 0f, 0f, 0.05f); + private const int MAX_SIDE = 6; + private Transform tsSideRoot; /// - /// 军团战地图 + /// 边界线Prefab数组 /// - public Map Map { get; private set; } + private GameObject[] goSides = new GameObject[MAX_SIDE]; /// /// 格子数据 /// private Dictionary dicHexesData; /// - /// 所有格子 + /// 边界数据 /// - private List listAllCellIndex; - private List listBoardViewData; - public BoardLineObjPool boardLineObjPool = new(); + private Dictionary dicSideData; + + /// + /// 要更新的格子 + /// + private HashSet setUpdateHexes; + /// + /// 要更新边的格子 + /// + private HashSet setUpdateHexesSide; + + /// + /// 军团战地图 + /// + public Map Map { get; private set; } /// /// 两格之间的距离 @@ -50,7 +59,7 @@ namespace Gameplay.LegionBattle SceneHandle sceneHandle = await GameSceneManager.Instance.LoadSceneAsync(MapCfg.Path); MapData mapData = await MapUtils.LoadMapData(MapCfg.MapData); // 加载地图数据 Map = await MapUtils.LoadMap(mapData); // 加载地图 - + AroundHelper.Init(Map); Camera camera = sceneHandle.Camera; CameraManager.Instance.SwitchMainCamera(camera); MapUtils.BindCameraController(camera, Map); @@ -60,96 +69,58 @@ namespace Gameplay.LegionBattle Vector3 end = Map.TGSCellIndex2WorldPosition(1); Distance = (end - start).magnitude; - if (null == listAllCellIndex) - listAllCellIndex = new List(); + if (null == setUpdateHexes) + setUpdateHexes = new HashSet(); - if (null == listBoardViewData) - listBoardViewData = new List(); + if(null == setUpdateHexesSide) + setUpdateHexesSide = new HashSet(); if (null == dicHexesData) dicHexesData = new Dictionary(); + if (null == dicSideData) + dicSideData = new Dictionary(); + + #region 预加载 + for (int i = 0; i < MAX_SIDE; i++) + { + AssetManager.Instance.PostPreload(PathDefine.BoardSide.SIDE_PATHS[i]); + } + AssetManager.Instance.PostPreload(PathDefine.MoveArea.LEGIONBATTLE_A_AREA_PATH); // 蓝色方地格 + AssetManager.Instance.PostPreload(PathDefine.MoveArea.LEGIONBATTLE_B_AREA_PATH); // 红色方地格 + #endregion + + await AssetManager.Instance.WaitAllPreloads(); + + for (int i = 0; i < MAX_SIDE; ++i) + { + goSides[i] = AssetManager.Instance.GetPreLoadResult(PathDefine.BoardSide.SIDE_PATHS[i]); + } + GameObject goSideRoot = new("[Side]"); + tsSideRoot = goSideRoot.transform; + GameObject goCellRoot = new("[MoveArea]"); - GameObject goACell = await AssetManager.Instance.LoadAssetAsync(PathDefine.MoveArea.LEGIONBATTLE_A_AREA_PATH); - GameObject goBCell = await AssetManager.Instance.LoadAssetAsync(PathDefine.MoveArea.LEGIONBATTLE_B_AREA_PATH); + Transform tsCellRoot = goCellRoot.transform; + GameObject goACell = AssetManager.Instance.GetPreLoadResult(PathDefine.MoveArea.LEGIONBATTLE_A_AREA_PATH); + GameObject goBCell = AssetManager.Instance.GetPreLoadResult(PathDefine.MoveArea.LEGIONBATTLE_B_AREA_PATH); foreach (TGS.Cell cell in Map.TerrainGridSystem.cells) { Vector3 pos = Map.TGSCellIndex2WorldPosition(cell.index); - listAllCellIndex.Add(cell.index); - dicHexesData.Add(Map.TGSCellIndex2BlockPosition(cell.index), - new LegionBattleHexesData(cell.index, pos, goCellRoot.transform, goACell, goBCell)); + Vector2Int coordinate = Map.TGSCellIndex2BlockPosition(cell.index); + GameObject goNewACell = Object.Instantiate(goACell, tsCellRoot); // 实例化地板 + goNewACell.transform.position = pos; + goNewACell.SetActive(false); + + GameObject goNewBCell = Object.Instantiate(goBCell, tsCellRoot); // 实例化地板 + goNewBCell.transform.position = pos; + goNewBCell.SetActive(false); + LegionBattleHexesData hexesData = new(cell, coordinate, goNewACell, goNewBCell); + dicHexesData.Add(coordinate, hexesData); + foreach (int i in hexesData.ListAroundSideIndex) + { + CreateSideByIndex(i); + } } - - for (int i = 0; i < 6; i++) - { - var sidePath = PathDefine.BoardSide.SIDE_PATHS[i]; - AssetManager.Instance.PostPreload(sidePath); - } - - await AssetManager.Instance.WaitAllPreloads(); - boardLineObjPool.Init(); - AroundHelper.Init(Map); - AreaManager.CreateInstance(Map); - //AreaManager.instance.Init(); - } - - /// - /// 更新地板颜色通过索引 - /// - /// - private void UpdateCellColorByIndex(LegionBattleHexesData hexesData) - { - switch (hexesData.Camp) - { - case BattlefieldActorCamp.CampA: - { - Map.SetBlockColor(hexesData.Index, true, CmpAnimGlobalSet.instance.LegionBattle_A_CELL_COLOR); - } - break; - case BattlefieldActorCamp.CampB: - { - Map.SetBlockColor(hexesData.Index, true, CmpAnimGlobalSet.instance.LegionBattle_B_CELL_COLOR); - } - break; - default: - { - Map.SetBlockColor(hexesData.Index, true, new Color(0f, 0f, 0f, 0f)); - } - break; - } - - foreach (BoardViewData boardViewData in listBoardViewData) - { - boardViewData.Destroy(Map.TerrainGridSystem); - } - listBoardViewData.Clear(); - boardLineObjPool.ReturnAll(); - - BoardViewHelper.GenerateBoard(listAllCellIndex, listBoardViewData, Map.TerrainGridSystem); - for (int i = 0; i < listBoardViewData.Count; ++i) - { - listBoardViewData[i].CreateLegionBattleBoardSideView(Map.TerrainGridSystem, boardLineObjPool); - } - } - - /// - /// 更新建筑地板上的线 - /// - /// - private void UpdateCellLine() - { - /*foreach (BoardViewData boardViewData in listBoardViewData) - { - boardViewData.Destroy(Map.TerrainGridSystem); - } - listBoardViewData.Clear(); - boardLineObjPool.ReturnAll(); - - BoardViewHelper.GenerateBoard(listAllCellIndex, listBoardViewData, Map.TerrainGridSystem); - for (int i = 0; i < listBoardViewData.Count; ++i) - { - listBoardViewData[i].CreateLegionBattleBoardSideView(Map.TerrainGridSystem, boardLineObjPool); - }*/ } /// @@ -166,5 +137,82 @@ namespace Gameplay.LegionBattle return hexesData.Camp; } + + private void UpdateAllSide() + { + foreach (Vector2Int pos in setUpdateHexes) + { + if (!dicHexesData.TryGetValue(pos, out LegionBattleHexesData hexesData)) + continue; + + setUpdateHexesSide.Add(pos); + foreach (Vector2Int aroundPos in hexesData.ListAroundCell) + { + setUpdateHexesSide.Add(aroundPos); + } + } + + foreach (Vector2Int pos in setUpdateHexesSide) + { + if (!dicHexesData.TryGetValue(pos, out LegionBattleHexesData hexesData)) + continue; + + hexesData.UpdateSide(); + } + + setUpdateHexes.Clear(); + setUpdateHexesSide.Clear(); + } + + /// + /// 设置格子边线是否显示 + /// + /// 当前格子阵营 + /// 周围格子 + /// 边线索引 + public void UpdateSideShow(BattlefieldActorCamp camp, Vector2Int aroundCell, int sideIndex) + { + if (!dicSideData.TryGetValue(sideIndex, out LegionBattleSideData sideData)) + return; + + if (!dicHexesData.TryGetValue(aroundCell, out LegionBattleHexesData hexesData)) + { + sideData.IsActive = true; + return; + } + + sideData.IsActive = camp != hexesData.Camp; + } + + /// + /// 创建边界线 + /// + /// + private void CreateSideByIndex(int boardIndex) + { + if (dicSideData.ContainsKey(boardIndex)) + return; + + int centerCellIndex = boardIndex / 10; + Vector3 centerPos = Map.TGSCellIndex2WorldPosition(centerCellIndex); + int side = boardIndex % 10; + + dicSideData.Add(boardIndex, new LegionBattleSideData(centerPos, CreateBoardSideObj(side, centerPos, boardIndex.ToString()))); + } + + /// + /// 创建边的实例 + /// + /// + /// + /// + private GameObject CreateBoardSideObj(int side, Vector3 centerPos, string name) + { + GameObject goSide = Object.Instantiate(goSides[side - 1], tsSideRoot); + goSide.transform.position = centerPos; + goSide.name = name; + + return goSide; + } } } \ No newline at end of file