军团战边界

main
yurui 2025-03-31 15:23:24 +08:00
parent 4e3118c3d8
commit 0df0a0e5be
5 changed files with 318 additions and 107 deletions

View File

@ -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阵营地板
/// </summary>
private GameObject goBCell;
/// <summary>
/// 索引
/// </summary>
public int Index { get; private set; }
/// <summary>
/// 坐标
/// </summary>
public Vector2Int Coordinate { get; private set; }
/// <summary>
/// 所属阵营
/// </summary>
public BattlefieldActorCamp Camp { get; private set; }
/// <summary>
/// 周围格子
/// </summary>
public List<Vector2Int> ListAroundCell { get; private set; }
/// <summary>
/// 周围边界索引
/// </summary>
public List<int> 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);
}
/// <summary>
@ -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<int> 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);
}
}
}
}
}
}
}

View File

@ -0,0 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Gameplay.LegionBattle
{
/// <summary>
/// 军团战边界数据
/// </summary>
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;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5818b78ba03140d478d5344dc38cd9d4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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();
}
/// <summary>

View File

@ -17,28 +17,37 @@ namespace Gameplay.LegionBattle
public sealed partial class LegionBattle : IUpdatable, ILoadable
{
/// <summary>
/// 蓝方势力地板颜色
/// 最大边数
/// </summary>
private readonly Color CAMP_A_CELL_COLOR = new(0f, 0f, 1f, 0.05f);
/// <summary>
/// 红方势力地板颜色
/// </summary>
private readonly Color CAMP_B_CELL_COLOR = new(1f, 0f, 0f, 0.05f);
private const int MAX_SIDE = 6;
private Transform tsSideRoot;
/// <summary>
/// 军团战地图
/// 边界线Prefab数组
/// </summary>
public Map Map { get; private set; }
private GameObject[] goSides = new GameObject[MAX_SIDE];
/// <summary>
/// 格子数据
/// </summary>
private Dictionary<Vector2Int, LegionBattleHexesData> dicHexesData;
/// <summary>
/// 所有格子
/// 边界数据
/// </summary>
private List<int> listAllCellIndex;
private List<BoardViewData> listBoardViewData;
public BoardLineObjPool boardLineObjPool = new();
private Dictionary<int, LegionBattleSideData> dicSideData;
/// <summary>
/// 要更新的格子
/// </summary>
private HashSet<Vector2Int> setUpdateHexes;
/// <summary>
/// 要更新边的格子
/// </summary>
private HashSet<Vector2Int> setUpdateHexesSide;
/// <summary>
/// 军团战地图
/// </summary>
public Map Map { get; private set; }
/// <summary>
/// 两格之间的距离
@ -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<int>();
if (null == setUpdateHexes)
setUpdateHexes = new HashSet<Vector2Int>();
if (null == listBoardViewData)
listBoardViewData = new List<BoardViewData>();
if(null == setUpdateHexesSide)
setUpdateHexesSide = new HashSet<Vector2Int>();
if (null == dicHexesData)
dicHexesData = new Dictionary<Vector2Int, LegionBattleHexesData>();
if (null == dicSideData)
dicSideData = new Dictionary<int, LegionBattleSideData>();
#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<GameObject>(PathDefine.BoardSide.SIDE_PATHS[i]);
}
GameObject goSideRoot = new("[Side]");
tsSideRoot = goSideRoot.transform;
GameObject goCellRoot = new("[MoveArea]");
GameObject goACell = await AssetManager.Instance.LoadAssetAsync<GameObject>(PathDefine.MoveArea.LEGIONBATTLE_A_AREA_PATH);
GameObject goBCell = await AssetManager.Instance.LoadAssetAsync<GameObject>(PathDefine.MoveArea.LEGIONBATTLE_B_AREA_PATH);
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);
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();
}
/// <summary>
/// 更新地板颜色通过索引
/// </summary>
/// <param name="index"></param>
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);
}
}
/// <summary>
/// 更新建筑地板上的线
/// </summary>
/// <param name="cellIndex"></param>
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);
}*/
}
/// <summary>
@ -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();
}
/// <summary>
/// 设置格子边线是否显示
/// </summary>
/// <param name="camp">当前格子阵营</param>
/// <param name="aroundCell">周围格子</param>
/// <param name="sideIndex">边线索引</param>
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;
}
/// <summary>
/// 创建边界线
/// </summary>
/// <param name="boardIndex"></param>
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())));
}
/// <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;
}
}
}