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

192 lines
6.4 KiB
C#

using cfg.LegionBattleCfg;
using Framework;
using Gameplay.Level;
using NLDPB;
using System.Collections.Generic;
using UnityEngine;
namespace Gameplay.LegionBattle
{
/// <summary>
/// 军团战建筑数据
/// </summary>
public sealed class LegionBuildingData
{
/// <summary>
/// 建筑索引
/// </summary>
public int Index { get; private set; }
/// <summary>
/// 建筑坐标
/// </summary>
public Vector2Int Coordinate { get; private set; }
/// <summary>
/// 建筑内的玩家
/// </summary>
public List<uint> listActorId { get; private set; }
/// <summary>
/// 当前阵营
/// </summary>
public BattlefieldActorCamp CurCamp { get; private set; }
/// <summary>
/// 防守成功次数
/// </summary>
public int DefenceCount { get; private set; }
/// <summary>
/// 被攻占次数
/// </summary>
public int AttackedTotal { get; private set; }
/// <summary>
/// 名字key
/// </summary>
public string NameKey { get; private set; }
/// <summary>
/// 据点配置
/// </summary>
public DataLegionBattleBuilding Cfg { get; private set; }
/// <summary>
/// 所占所有格子坐标
/// </summary>
public List<int> ListAllIndex { get; private set; }
public LegionBuildingData(LegionLevelData.Building building, int width, int height, Map map)
{
Index = building.position;
listActorId = new List<uint>();
ListAllIndex = new List<int>();
NameKey = building.buildingNameKey;
Cfg = TableManager.Instance.Tables.LegionBattleBuilding.Get(building.buildingID);
#region 临时处理计算建筑所占地格
Dictionary<Vector2Int, bool> dicPos = new() // TODO 临时处理,记录建筑所占地格
{
{ map.TGSCellIndex2BlockPosition(building.position), false }
};
HashSet<Vector2Int> setNew = new();
List<Vector2Int> listOld = new();
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;
ListAllIndex.Add(map.BlockPosition2TGSCellIndex(pos));
}
#endregion
#region 临时处理建筑地板颜色
foreach (int index in ListAllIndex)
{
map.SetBlockColor(index, true, Color.white);
}
#endregion
}
public void CopyData(MSG_LB_Building data)
{
Coordinate = new Vector2Int((int)data.Nz, (int)data.Nx);
listActorId.Clear();
for (int i = 0; i < data.Actors.Count; ++i)
{
listActorId.Add(data.Actors[i]);
}
if (data.Actors.Count <= 0)
CurCamp = BattlefieldActorCamp.BacNone;
else
CurCamp = LegionBattleManager.Instance.GetCampByActorId(data.Actors[0]);
}
public void CopyData(BattlefieldActorCamp camp, int defCount, int total)
{
CurCamp = camp;
DefenceCount = defCount;
AttackedTotal = total;
}
}
}