NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/LegionBattle.Map.cs

217 lines
7.8 KiB
C#
Raw Normal View History

2025-02-13 13:44:36 +08:00
using Cysharp.Threading.Tasks;
2025-03-17 17:52:27 +08:00
using Gameplay.Area;
using Gameplay.Area.Around;
using Gameplay.Area.BoardView;
using Gameplay.Common;
using Gameplay.Level;
2025-03-20 13:50:50 +08:00
using Gameplay.Story.Cmp.Anim;
2025-02-13 13:44:36 +08:00
using Gameplay.SubSystems;
2025-03-17 17:52:27 +08:00
using NLDPB;
2025-02-13 13:44:36 +08:00
using PhxhSDK;
using PhxhSDK.Res;
2025-03-17 15:51:39 +08:00
using System.Collections.Generic;
2025-02-13 13:44:36 +08:00
using UnityEngine;
namespace Gameplay.LegionBattle
{
public sealed partial class LegionBattle : IUpdatable, ILoadable
{
/// <summary>
2025-03-31 15:23:24 +08:00
/// 最大边数
/// </summary>
2025-03-31 16:17:35 +08:00
public const int MAX_SIDE = 6;
2025-03-31 15:23:24 +08:00
private Transform tsSideRoot;
2025-02-13 13:44:36 +08:00
/// <summary>
2025-03-31 15:23:24 +08:00
/// 边界线Prefab数组
2025-02-13 13:44:36 +08:00
/// </summary>
2025-03-31 15:23:24 +08:00
private GameObject[] goSides = new GameObject[MAX_SIDE];
2025-03-17 15:51:39 +08:00
/// <summary>
/// 格子数据
/// </summary>
private Dictionary<Vector2Int, LegionBattleHexesData> dicHexesData;
2025-03-17 17:52:27 +08:00
/// <summary>
2025-03-31 15:23:24 +08:00
/// 边界数据
/// </summary>
private Dictionary<int, LegionBattleSideData> dicSideData;
/// <summary>
/// 要更新的格子
/// </summary>
private HashSet<Vector2Int> setUpdateHexes;
/// <summary>
/// 要更新边的格子
/// </summary>
private HashSet<Vector2Int> setUpdateHexesSide;
/// <summary>
/// 军团战地图
2025-03-17 17:52:27 +08:00
/// </summary>
2025-03-31 15:23:24 +08:00
public Map Map { get; private set; }
2025-02-13 13:44:36 +08:00
/// <summary>
/// 两格之间的距离
/// </summary>
public float Distance { get; private set; }
2025-02-13 13:44:36 +08:00
public async UniTask LoadMap()
{
SceneHandle sceneHandle = await GameSceneManager.Instance.LoadSceneAsync(MapCfg.Path);
MapData mapData = await MapUtils.LoadMapData(MapCfg.MapData); // 加载地图数据
Map = await MapUtils.LoadMap(mapData); // 加载地图
2025-03-31 15:23:24 +08:00
AroundHelper.Init(Map);
2025-02-13 13:44:36 +08:00
Camera camera = sceneHandle.Camera;
CameraManager.Instance.SwitchMainCamera(camera);
MapUtils.BindCameraController(camera, Map);
Map.TerrainGridSystem.showCells = true;
2025-03-20 16:32:22 +08:00
Map.TerrainGridSystem.cellBorderThickness = 0.04f;
Vector3 start = Map.TGSCellIndex2WorldPosition(0);
Vector3 end = Map.TGSCellIndex2WorldPosition(1);
Distance = (end - start).magnitude;
2025-03-17 15:51:39 +08:00
2025-03-31 15:23:24 +08:00
if (null == setUpdateHexes)
setUpdateHexes = new HashSet<Vector2Int>();
2025-03-17 17:52:27 +08:00
2025-03-31 15:23:24 +08:00
if(null == setUpdateHexesSide)
setUpdateHexesSide = new HashSet<Vector2Int>();
2025-03-17 17:52:27 +08:00
2025-03-17 15:51:39 +08:00
if (null == dicHexesData)
dicHexesData = new Dictionary<Vector2Int, LegionBattleHexesData>();
2025-03-31 15:23:24 +08:00
if (null == dicSideData)
dicSideData = new Dictionary<int, LegionBattleSideData>();
2025-03-31 16:17:35 +08:00
#region 资源加载
2025-03-31 15:23:24 +08:00
for (int i = 0; i < MAX_SIDE; i++)
2025-03-17 15:51:39 +08:00
{
2025-03-31 16:17:35 +08:00
goSides[i] = await AssetManager.Instance.LoadAssetAsync<GameObject>(PathDefine.BoardSide.LEGION_BATTLE_SIDE_PATHS[i]);
2025-03-17 17:52:27 +08:00
}
2025-03-31 15:23:24 +08:00
AssetManager.Instance.PostPreload(PathDefine.MoveArea.LEGIONBATTLE_A_AREA_PATH); // 蓝色方地格
AssetManager.Instance.PostPreload(PathDefine.MoveArea.LEGIONBATTLE_B_AREA_PATH); // 红色方地格
#endregion
2025-03-17 17:52:27 +08:00
2025-03-31 15:23:24 +08:00
await AssetManager.Instance.WaitAllPreloads();
GameObject goSideRoot = new("[Side]");
tsSideRoot = goSideRoot.transform;
2025-03-17 17:52:27 +08:00
2025-03-31 15:23:24 +08:00
GameObject goCellRoot = new("[MoveArea]");
Transform tsCellRoot = goCellRoot.transform;
GameObject goACell = AssetManager.Instance.GetPreLoadResult<GameObject>(PathDefine.MoveArea.LEGIONBATTLE_A_AREA_PATH);
GameObject goBCell = AssetManager.Instance.GetPreLoadResult<GameObject>(PathDefine.MoveArea.LEGIONBATTLE_B_AREA_PATH);
foreach (TGS.Cell cell in Map.TerrainGridSystem.cells)
{
Vector3 pos = Map.TGSCellIndex2WorldPosition(cell.index);
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);
}
}
2025-02-13 13:44:36 +08:00
}
2025-02-25 13:23:42 +08:00
/// <summary>
2025-03-31 15:23:24 +08:00
/// 获取地板阵营根据索引
/// </summary>
/// <param name="index"></param>
2025-03-31 15:23:24 +08:00
/// <returns></returns>
public BattlefieldActorCamp GetCellCampByIndex(int index)
{
2025-03-31 15:23:24 +08:00
Vector2Int pos = Map.TGSCellIndex2BlockPosition(index);
2025-03-28 16:43:15 +08:00
2025-03-31 15:23:24 +08:00
if (!dicHexesData.TryGetValue(pos, out LegionBattleHexesData hexesData))
return BattlefieldActorCamp.BacNone;
return hexesData.Camp;
}
2025-03-31 16:17:35 +08:00
/// <summary>
/// 更新变化的格子边线
/// </summary>
2025-03-31 15:23:24 +08:00
private void UpdateAllSide()
{
foreach (Vector2Int pos in setUpdateHexes)
2025-03-28 16:43:15 +08:00
{
2025-03-31 15:23:24 +08:00
if (!dicHexesData.TryGetValue(pos, out LegionBattleHexesData hexesData))
continue;
setUpdateHexesSide.Add(pos);
foreach (Vector2Int aroundPos in hexesData.ListAroundCell)
{
setUpdateHexesSide.Add(aroundPos);
}
2025-03-28 16:43:15 +08:00
}
2025-03-31 15:23:24 +08:00
foreach (Vector2Int pos in setUpdateHexesSide)
2025-03-28 16:43:15 +08:00
{
2025-03-31 15:23:24 +08:00
if (!dicHexesData.TryGetValue(pos, out LegionBattleHexesData hexesData))
continue;
hexesData.UpdateSide();
2025-03-28 16:43:15 +08:00
}
2025-03-31 15:23:24 +08:00
setUpdateHexes.Clear();
setUpdateHexesSide.Clear();
}
/// <summary>
2025-03-31 15:23:24 +08:00
/// 设置格子边线是否显示
2025-02-25 13:23:42 +08:00
/// </summary>
2025-03-31 15:23:24 +08:00
/// <param name="camp">当前格子阵营</param>
/// <param name="aroundCell">周围格子</param>
/// <param name="sideIndex">边线索引</param>
public void UpdateSideShow(BattlefieldActorCamp camp, Vector2Int aroundCell, int sideIndex)
2025-03-17 17:52:27 +08:00
{
2025-03-31 15:23:24 +08:00
if (!dicSideData.TryGetValue(sideIndex, out LegionBattleSideData sideData))
return;
if (!dicHexesData.TryGetValue(aroundCell, out LegionBattleHexesData hexesData))
2025-03-17 17:52:27 +08:00
{
2025-03-31 15:23:24 +08:00
sideData.IsActive = true;
return;
2025-03-17 17:52:27 +08:00
}
2025-03-31 15:23:24 +08:00
sideData.IsActive = camp != hexesData.Camp;
2025-02-25 13:23:42 +08:00
}
2025-03-24 17:09:05 +08:00
/// <summary>
2025-03-31 15:23:24 +08:00
/// 创建边界线
2025-03-24 17:09:05 +08:00
/// </summary>
2025-03-31 15:23:24 +08:00
/// <param name="boardIndex"></param>
private void CreateSideByIndex(int boardIndex)
2025-03-24 17:09:05 +08:00
{
2025-03-31 15:23:24 +08:00
if (dicSideData.ContainsKey(boardIndex))
return;
2025-03-24 17:09:05 +08:00
2025-03-31 15:23:24 +08:00
int centerCellIndex = boardIndex / 10;
Vector3 centerPos = Map.TGSCellIndex2WorldPosition(centerCellIndex);
int side = boardIndex % 10;
2025-03-24 17:09:05 +08:00
2025-03-31 15:23:24 +08:00
dicSideData.Add(boardIndex, new LegionBattleSideData(centerPos, CreateBoardSideObj(side, centerPos, boardIndex.ToString())));
}
/// <summary>
/// 创建边的实例
/// </summary>
/// <param name="side"></param>
/// <param name="centerPos"></param>
/// <returns></returns>
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;
2025-03-24 17:09:05 +08:00
}
2025-02-13 13:44:36 +08:00
}
}