NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Area/AreaManager.cs

564 lines
19 KiB
C#
Raw Normal View History

using cfg.AreaCfg;
2023-08-22 19:08:45 +08:00
using Framework;
using Gameplay.Level;
using System;
using System.Collections.Generic;
2024-12-11 15:32:38 +08:00
using Gameplay.Area.Around;
2024-07-12 17:06:39 +08:00
using Gameplay.Area.AttackArea;
using Gameplay.Area.Ready;
using Gameplay.Character;
2024-10-23 14:45:40 +08:00
using Gameplay.Common;
2024-11-26 14:59:20 +08:00
using Gameplay.Skill;
2023-12-13 16:20:41 +08:00
using Gameplay.Unit;
using Gameplay.Unit.Data;
2023-08-22 19:08:45 +08:00
using UnityEngine;
namespace Gameplay.Area
{
2023-12-12 12:49:49 +08:00
using Skill;
2023-08-22 19:08:45 +08:00
public class AreaManager
{
2024-10-11 13:24:33 +08:00
public static bool ENABLE_LOG = false;
2023-11-08 13:47:15 +08:00
2023-08-22 19:08:45 +08:00
private static AreaManager _instance;
public static AreaManager instance
{
get
{
return _instance;
}
}
2025-03-17 17:52:27 +08:00
public static void CreateInstance(Map map)
2023-08-22 19:08:45 +08:00
{
if (_instance != null)
{
DebugUtil.LogError("请勿重复创建AreaManager");
return;
2023-08-22 19:08:45 +08:00
}
2025-03-17 17:52:27 +08:00
_instance = new AreaManager(map);
2023-08-22 19:08:45 +08:00
}
2023-10-19 15:00:19 +08:00
private readonly Dictionary<int, List<Vector2Int>> _areaDict;
2024-11-26 14:59:20 +08:00
// public readonly SkillAreaSelector skillAreaSelector;
public readonly SkillAreaSelector2 skillAreaSelector2;
public readonly PlayerSkillAreaSelector playerSkillAreaSelector;
2023-08-22 19:08:45 +08:00
internal readonly Dictionary<int, List<GameUnit>> _cellIndex2Units;
2023-08-22 19:08:45 +08:00
private Map _map;
2024-11-26 19:59:53 +08:00
public readonly AttackAreaView attackArea;
2023-08-22 19:08:45 +08:00
public readonly ReadyAreaManager readyAreaManager;
2024-07-12 17:06:39 +08:00
public readonly AttackAreaManager attackAreaManager;
2024-09-30 18:06:04 +08:00
public readonly MoveAreaView moveAreaView;
2024-11-26 14:59:20 +08:00
// public SkillAreaSelecter skillAreaSelecter => _skillAreaSelecter;
private Dictionary<int, AroundCellData> _aroundCellDatas = new Dictionary<int, AroundCellData>();
private Dictionary<int, Vector3> _cellIndexToWorldPosCache = new Dictionary<int, Vector3>();
public bool isInSkillPreview
2023-08-22 19:08:45 +08:00
{
get
{
return skillAreaSelector2.isInPreview || playerSkillAreaSelector.isInPreview;
2023-08-22 19:08:45 +08:00
}
}
public ParentGroup parentGroup = new ParentGroup();
2023-08-22 19:08:45 +08:00
private readonly IEventHandler _eventHandler;
2025-03-17 15:03:17 +08:00
private Queue<List<int>> _cellIndexListRing;
private Dictionary<int, int> _distanceCache;
2025-03-17 17:52:27 +08:00
private AreaManager(Map map)
2023-08-22 19:08:45 +08:00
{
_areaDict = new Dictionary<int, List<Vector2Int>>();
2024-11-26 14:59:20 +08:00
skillAreaSelector2 = new SkillAreaSelector2();
playerSkillAreaSelector = new PlayerSkillAreaSelector();
2023-08-22 19:08:45 +08:00
_cellIndex2Units = new Dictionary<int, List<GameUnit>>();
2024-11-26 19:59:53 +08:00
attackArea = new AttackAreaView();
2025-03-17 17:52:27 +08:00
_map = map;
2023-12-14 16:30:55 +08:00
readyAreaManager = new ReadyAreaManager(_map);
2024-07-12 17:06:39 +08:00
attackAreaManager = new AttackAreaManager();
2024-09-30 18:06:04 +08:00
moveAreaView = new MoveAreaView();
_eventHandler = new AreaEventHandler(this);
2023-08-22 19:08:45 +08:00
}
public void Init()
{
2024-12-31 12:50:42 +08:00
_InitMathV3();
parentGroup.Init();
moveAreaView.Init(_map);
2024-11-26 19:59:53 +08:00
attackArea.Init();
2024-05-30 18:25:45 +08:00
readyAreaManager.Init();
2024-07-12 17:06:39 +08:00
attackAreaManager.Init();
_eventHandler.RegisterEvents();
2025-03-17 15:03:17 +08:00
_cellIndexListRing = new Queue<List<int>>();
for (int i = 0; i < 10; ++i)
{
var list = new List<int>();
_cellIndexListRing.Enqueue(list);
}
_distanceCache = new Dictionary<int, int>();
2024-12-31 12:50:42 +08:00
}
2024-06-04 14:12:03 +08:00
2024-09-30 18:06:04 +08:00
public void Dispose()
{
readyAreaManager.Dispose();
attackAreaManager.Dispose();
moveAreaView.Dispose();
playerSkillAreaSelector.Dispose();
parentGroup.Dispose();
_aroundCellDatas.Clear();
_eventHandler.Dispose();
2024-09-30 18:06:04 +08:00
_instance = null;
}
2023-08-22 19:08:45 +08:00
internal void _AddUnitToCellIndex(int cellIndex, GameUnit unit)
2023-08-22 19:08:45 +08:00
{
if (_cellIndex2Units.TryGetValue(cellIndex, out var unitList))
{
unitList.Add(unit);
}
else
{
unitList = new List<GameUnit>();
unitList.Add(unit);
_cellIndex2Units.Add(cellIndex, unitList);
}
if (CodeDefine.ENABLE_FIGHT_NORMAL_ATTACK_LOG)
{
_Log($"add unit:{unit.GetID()} to cellIndex:{cellIndex}");
}
2023-08-22 19:08:45 +08:00
}
internal void _OnUnitRemove(GameUnit unit)
2023-08-22 19:08:45 +08:00
{
var cellIndex = unit.transData.cellIndex;
if (_cellIndex2Units.TryGetValue(cellIndex, out var unitList))
{
unitList.Remove(unit);
}
2023-08-22 19:08:45 +08:00
}
2023-10-19 15:00:19 +08:00
private static void _Log(string str)
{
2023-11-08 13:47:15 +08:00
if (!ENABLE_LOG) return;
2023-10-19 15:00:19 +08:00
DebugUtil.Log("[AreaManager] {0}", str);
2023-08-22 19:08:45 +08:00
}
2024-09-30 18:06:04 +08:00
2023-08-22 19:08:45 +08:00
public DataArea GetConfigById(int areaId)
{
return TableManager.Instance.Tables.AreaConfig.Get(areaId);
}
private List<int> _emptyList = new List<int>();
2023-08-22 19:08:45 +08:00
public List<int> GetCellIndexsAround(int centerCellIndex,
int distance, bool containsCenter = true)
{
var key = AroundCellData.GetKey(centerCellIndex, distance, containsCenter);
if (_aroundCellDatas.TryGetValue(key, out var data))
{
return data.aroundCellIndexs;
}
2024-12-02 19:33:16 +08:00
var list = _GetCellIndexAround(centerCellIndex, distance, containsCenter);
var aroundData = new AroundCellData(centerCellIndex, distance, containsCenter);
aroundData.aroundCellIndexs = list;
_aroundCellDatas.Add(key, aroundData);
return list;
}
2024-12-02 19:33:16 +08:00
private List<int> _GetCellIndexAround(int centerCellIndex,
int distance, bool containsCenter = true)
2023-08-22 19:08:45 +08:00
{
2024-07-12 19:09:37 +08:00
if (distance <= 0)
{
if (containsCenter)
{
return new List<int>()
{
centerCellIndex
};
}
else
2024-07-12 19:09:37 +08:00
{
return _emptyList;
}
2024-07-12 19:09:37 +08:00
}
2024-12-11 15:32:38 +08:00
var list = new List<int>();
if (containsCenter)
{
list.Insert(0, centerCellIndex);
}
2024-12-11 15:32:38 +08:00
AroundHelper.GetAround(centerCellIndex, distance, list);
2023-08-22 19:08:45 +08:00
return list;
}
2023-12-25 19:00:08 +08:00
public bool IsCellEmpty(int cellIndex)
{
var unitList = GetUnitsByCellIndex(cellIndex);
return unitList.Count == 0;
}
2023-08-22 19:08:45 +08:00
public int GetDistance(int cellIndex1,
int cellIndex2)
{
if (cellIndex1 == cellIndex2)
{
return 0;
}
2025-03-17 15:03:17 +08:00
var key = cellIndex1 * 10000 + cellIndex2;
if (_distanceCache.TryGetValue(key, out var distance))
{
return distance;
}
var newDistance = _map.TerrainGridSystem.CellGetHexagonDistance(cellIndex1, cellIndex2);
_distanceCache.Add(key, newDistance);
var invertedKey = cellIndex2 * 10000 + cellIndex1;
_distanceCache.Add(invertedKey, newDistance);
return newDistance;
// 实测TGS的方法性能更好 0.66 vs 0.8
// var mathPoint1 = AroundHelper.GetMathPosV3FromTable(cellIndex1);
// var mathPoint2 = AroundHelper.GetMathPosV3FromTable(cellIndex2);
// var offsetX = Mathf.Abs(mathPoint1.x - mathPoint2.x);
// var offsetY = Mathf.Abs(mathPoint1.y - mathPoint2.y);
// var offsetZ = Mathf.Abs(mathPoint1.z - mathPoint2.z);
// return Mathf.Max(offsetX, Mathf.Max(offsetY, offsetZ));
2025-03-17 15:03:17 +08:00
// return _map.TerrainGridSystem.CellGetHexagonDistance(cellIndex1, cellIndex2);
}
2024-11-15 14:32:41 +08:00
public Vector3 GetWorldPosByIndex(int cellIndex)
2023-12-12 12:49:49 +08:00
{
// Check if the position is already cached
if (_cellIndexToWorldPosCache.TryGetValue(cellIndex, out Vector3 cachedPos))
{
return cachedPos;
}
// Calculate and cache the position if not already present
2023-12-12 12:49:49 +08:00
var getPos = _map.TerrainGridSystem.CellGetPosition(cellIndex);
_cellIndexToWorldPosCache[cellIndex] = getPos;
2023-12-12 12:49:49 +08:00
return getPos;
}
public int GetCellIndexByPos(Vector3 pos)
{
if (_map.WorldPosition2TGSCellIndex(pos, out var result))
{
return result;
}
2024-09-09 19:22:22 +08:00
DebugUtil.LogError("无法获取到CellIndex:" + pos);
return -1;
}
2023-12-12 12:49:49 +08:00
2025-03-17 15:03:17 +08:00
/// <summary>
/// 注意: 返回的list 是循环使用的
/// </summary>
/// <param name="centerCellIndex"></param>
/// <param name="areaId"></param>
/// <returns></returns>
2023-08-22 19:08:45 +08:00
public List<int> GetCellIndexs(int centerCellIndex,
int areaId)
{
2025-03-17 15:03:17 +08:00
var getList = _cellIndexListRing.Dequeue();
_cellIndexListRing.Enqueue(getList);
getList.Clear();
GetCellIndexs(centerCellIndex, areaId, getList);
return getList;
2023-08-22 19:08:45 +08:00
}
public void GetCellIndexs(int centerCellIndex,
int areaId,
List<int> list)
{
var map = LevelManager.Instance.CurrentLevel.Map;
var posVec = map.TGSCellIndex2BlockPosition(centerCellIndex);
var offsetList = _GetOffsetList(areaId);
for (var i = 0; i < offsetList.Count; ++i)
{
var offset = offsetList[i];
var calcPos = posVec + offset;
if (posVec.x % 2 == 1 && calcPos.x % 2 == 0)
{
calcPos.y += 1;
}
var cellIndex = map.BlockPosition2TGSCellIndex(calcPos);
if (cellIndex == -1)
{
continue;
}
var getSuccess = map.GetRuntimeBlockProperty(cellIndex, out var blockData);
if (!getSuccess)
{
continue;
}
if (blockData.selectable == false)
{
continue;
}
2023-08-22 19:08:45 +08:00
list.Add(cellIndex);
}
}
private List<Vector2Int> _GetOffsetList(int areaId)
{
if (_areaDict.TryGetValue(areaId, out var offsetList))
{
return offsetList;
}
else
{
// 不存在,现场进行加载
var areaData = GetConfigById(areaId);
if (areaData == null)
2023-08-22 19:08:45 +08:00
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("AreaId:{0}不存在", areaId);
2023-08-22 19:08:45 +08:00
return null;
}
offsetList = new List<Vector2Int>();
var intList = areaData.Points;
2023-08-22 19:08:45 +08:00
for (int i = 0; i < intList.Count; i += 2)
{
offsetList.Add(new Vector2Int(intList[i], intList[i + 1]));
}
_areaDict.Add(areaId, offsetList);
return offsetList;
}
}
public int SearchNearCanClaimCellIndexWithAttack(GameUnit searchUnit, int maxDistance = 2)
{
if (searchUnit.controlData.targetEnemy == null)
{
return SearchNearCanClaimCellIndex(searchUnit, maxDistance);
}
var cellIndex = searchUnit.transData.cellIndex;
if (!CheckContainsMoveUnit(cellIndex))
{
return cellIndex;
}
var enemy = searchUnit.controlData.targetEnemy;
var enemyCellIndex = enemy.transData.cellIndex;
for (int i = 1; i <= maxDistance; i++)
{
var minSteps = i - 1;
var maxSteps = i;
var list = _map.TerrainGridSystem.CellGetNeighboursWithinRange(cellIndex, minSteps, maxSteps,
canCrossCheckType: TGS.CanCrossCheckType.IgnoreCanCrossCheckOnAllCells);
for (int j = 0; j < list.Count; j++)
{
// 先判断是否可到达
var checkIndex = list[j];
if (!CheckMapCellAvailable(checkIndex))
{
continue;
}
if (!PathFinder.CanBasePass(cellIndex, searchUnit))
{
continue;
}
if (!CheckContainsMoveUnit(checkIndex))
{
// 在判断该位置是否可攻击
var searchArea = attackAreaManager.GetAttackArea(checkIndex, searchUnit.fightData.attackDistance);
if (searchArea.canAttackCells.Contains(enemyCellIndex))
{
if (maxSteps > 1)
{
// 大于1之后需要额外判断一下是否有路径
var path = PathFinder.FindPath(_map, checkIndex, searchUnit);
if (path.Count > 0)
{
return checkIndex;
}
}
else
{
return checkIndex;
}
}
}
}
}
DebugUtil.LogG("没有找到可用的位置,返回中心位置");
// 没有找到,返回原来的
return cellIndex;
}
public int SearchNearCanClaimCellIndex(GameUnit searchUnit, int maxDistance = 2)
2024-12-02 19:33:16 +08:00
{
var cellIndex = searchUnit.transData.cellIndex;
2024-12-02 19:33:16 +08:00
if (!CheckContainsMoveUnit(cellIndex))
{
return cellIndex;
}
cellIndex = SearchNearCanClaimCellIndex(cellIndex, searchUnit, maxDistance);
// 没有找到,返回原来的
return cellIndex;
}
public int SearchNearCanClaimCellIndex(int startCellIndex, GameUnit searchUnit,
int maxDistance = 2)
{
var cellIndex = startCellIndex;
for (int i = 1; i <= maxDistance; i++)
2024-12-02 19:33:16 +08:00
{
var minSteps = i - 1;
var maxSteps = i;
var list = _map.TerrainGridSystem.CellGetNeighboursWithinRange(cellIndex, minSteps, maxSteps,
canCrossCheckType: TGS.CanCrossCheckType.IgnoreCanCrossCheckOnAllCells);
for (int j = 0; j < list.Count; j++)
{
var checkIndex = list[j];
if (!CheckMapCellAvailable(checkIndex))
{
continue;
}
if (!PathFinder.CanBasePass(cellIndex, searchUnit))
{
continue;
}
if (CheckContainsMoveUnit(checkIndex))
{
continue;
}
if (maxSteps > 1)
{
// 大于1之后需要额外判断一下是否有路径
var path = PathFinder.FindPath(_map, checkIndex, searchUnit);
if (path.Count > 0)
{
return cellIndex;
}
}
else
2024-12-02 19:33:16 +08:00
{
return checkIndex;
2024-12-02 19:33:16 +08:00
}
}
}
DebugUtil.LogG("没有找到可用的位置,返回初始位置");
2024-12-02 19:33:16 +08:00
return cellIndex;
}
public bool CheckContainsMoveEnemy(int cellIndex)
{
var allUnits = GameUnitManager.instance.infightUnits.unitList;
for (int i = 0; i < allUnits.Count; i++)
{
var unit = allUnits[i];
if (!unit.statusData.isOnFloor) continue;
if (unit.commonData.troopId == ETroopsId.Self) continue;
if (unit.commonData.canClaimStandCellIndex && unit.transData.cellIndex == cellIndex)
{
return true;
}
}
return false;
}
public bool CheckContainsMoveUnit(int cellIndex)
{
var allUnits = GameUnitManager.instance.infightUnits.unitList;
for (int i = 0; i < allUnits.Count; i++)
{
var unit = allUnits[i];
if (!unit.statusData.isOnFloor) continue;
if (unit.commonData.canClaimStandCellIndex && unit.transData.cellIndex == cellIndex)
{
return true;
}
if (unit.commonData.canClaimTargetCellIndex && unit.controlData.targetCellIndex == cellIndex)
{
return true;
}
}
return false;
}
2023-08-22 19:08:45 +08:00
public List<GameUnit> GetUnitsByCellIndex(int cellIndex)
{
if (_cellIndex2Units.TryGetValue(cellIndex, out var result))
{
return result;
}
else
{
var newList = new List<GameUnit>();
_cellIndex2Units.Add(cellIndex, newList);
return newList;
}
}
public TGS.Cell GetCellByIndex(int cellIndex)
{
return _map.TerrainGridSystem.cells[cellIndex];
}
public bool CheckMapCellAvailable(int cellIndex)
{
var mapCellData = LevelManager.Instance.CurrentLevel.Map.GetBlockData(cellIndex);
if (mapCellData == null)
{
return false;
}
var propData = mapCellData.GetTerrainTypeProperty();
if (propData == null)
{
return false;
}
if (!propData.selectable)
{
return false;
}
return true;
}
2024-06-04 14:12:03 +08:00
private void _InitMathV3()
{
2025-03-17 15:03:17 +08:00
AroundHelper.ClearCache();
2025-03-17 17:52:27 +08:00
var cellList = _map.TerrainGridSystem.cells;
for (int i = 0; i < cellList.Count; i++)
{
var cell = cellList[i];
var mathPos = AroundHelper.GetMathPosV3(cell.index);
cell.mathPointV3 = mathPos;
}
}
public void LogicUpdate(float dt)
{
// skillAreaSelector.LogicUpdate(dt);
attackArea.LogicUpdate(dt);
readyAreaManager.LogicUpdate(dt);
attackAreaManager.LogicUpdate(dt);
moveAreaView.LogicUpdate();
}
2025-03-17 15:03:17 +08:00
public void FrameClear()
{
}
2023-08-22 19:08:45 +08:00
}
}