【性能】缓存高频调用部分的单例对象

main
刘涛 2024-12-31 13:09:07 +08:00
parent 6349a712dd
commit c623f9f9b2
4 changed files with 25 additions and 28 deletions

View File

@ -364,13 +364,14 @@ namespace Gameplay.Area
public int GetDistance(int cellIndex1,
int cellIndex2)
{
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));
// return _map.TerrainGridSystem.CellGetHexagonDistance(cellIndex1, cellIndex2);
// 实测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));
return _map.TerrainGridSystem.CellGetHexagonDistance(cellIndex1, cellIndex2);
}
public Vector3 GetWorldPosByIndex(int cellIndex)

View File

@ -8,6 +8,12 @@ namespace Gameplay.Area.Around
public static bool useMath = true;
private static Map _map;
public static void Init()
{
_map = LevelManager.Instance.CurrentLevel.Map;
}
public static void GetAround(int centerIndex,
int stepCount,
List<int> result)
@ -24,7 +30,7 @@ namespace Gameplay.Area.Around
public static Vector3Int GetMathPosV3FromTable(int cellIndex)
{
var cellList = LevelManager.Instance.CurrentLevel.Map.TerrainGridSystem.cells;
var cellList = _map.TerrainGridSystem.cells;
var cell = cellList[cellIndex];
return cell.mathPointV3;
}
@ -40,7 +46,7 @@ namespace Gameplay.Area.Around
var maxY = centerPos.y + stepCount;
var minZ = centerPos.z - stepCount;
var maxZ = centerPos.z + stepCount;
var blockDataList = LevelManager.Instance.CurrentLevel.Map.MapData.BlocksData;
var blockDataList = _map.MapData.BlocksData;
for (var x = minX; x <= maxX; ++x)
{
for (var y = minY; y <= maxY; ++y)
@ -67,20 +73,20 @@ namespace Gameplay.Area.Around
var col = pos.x;
var row = pos.z + (pos.x - (pos.x & 1)) / 2;
// DebugUtil.Log("GetCellIndexV3 col:" + col + " row:" + row);
if (col < 0 || col >= LevelManager.Instance.CurrentLevel.Map.MapData.Width)
if (col < 0 || col >= _map.MapData.Width)
{
return -1;
}
if (row < 0 || row >= LevelManager.Instance.CurrentLevel.Map.MapData.Height)
if (row < 0 || row >= _map.MapData.Height)
{
return -1;
}
return LevelManager.Instance.CurrentLevel.Map.BlockPosition2TGSCellIndex(new Vector2Int(col, row));
return _map.BlockPosition2TGSCellIndex(new Vector2Int(col, row));
}
public static Vector3Int GetMathPosV3(int cellIndex)
{
var gridPosV2 = LevelManager.Instance.CurrentLevel.Map.TGSCellIndex2BlockPosition(cellIndex);
var gridPosV2 = _map.TGSCellIndex2BlockPosition(cellIndex);
var col = gridPosV2.x;
var row = gridPosV2.y;
var nX = col;
@ -98,7 +104,7 @@ namespace Gameplay.Area.Around
var workStep = 0;
_cacheCellList.Clear();
var mapTgs = LevelManager.Instance.CurrentLevel.Map.TerrainGridSystem;
var mapTgs = _map.TerrainGridSystem;
var allCell = mapTgs.cells;
for (int i = 0; i < allCell.Count; i++)

View File

@ -10,6 +10,7 @@ using Cysharp.Threading.Tasks;
using Framework;
using Gameplay.AI;
using Gameplay.Area;
using Gameplay.Area.Around;
using Gameplay.BT.Variables;
using Gameplay.Buff;
using Gameplay.Bullet;
@ -115,22 +116,12 @@ namespace Gameplay.Level
private async UniTask LoadMap()
{
// if (_levelData != null)
// {
// if (_map != null)
// {
// MapUtils.RemoveCameraController(CameraManager.Instance.MainCamera);
// MapUtils.UnLoadMap(_map);
// }
_map = await MapUtils.LoadMap(_currLevelInfo.MapData);
//NLDSceneManager.Instance.SceneCamera.gameObject.SetActive(false);
AroundHelper.Init();
var sceneCamera = NLDSceneManager.Instance.SceneCamera;
// Debug.LogError(sceneCamera.scene + " " + sceneCamera.name);
CameraManager.Instance.SwitchMainCamera(sceneCamera);
MapUtils.BindCameraController(NLDSceneManager.Instance.SceneCamera, _map);
//MapUtils.BindCameraController(CameraManager.Instance.MainCamera, _map);
//}
}
private async UniTask _LoadNpc()

View File

@ -836,7 +836,6 @@ namespace Gameplay.Unit
// 放技能时不索敌
if (statusData.isInSkilling) return;
var selfCellIndex = transData.cellIndex;
var map = LevelManager.Instance.CurrentLevel.Map;
if (controlData.tauntUnit != null)
{
@ -920,7 +919,7 @@ namespace Gameplay.Unit
if (enemy == this) continue;
if (!statusData.isChaos && !Level.Level.IsEnemy(this, enemy)) continue;
var enemyCellIndex = enemy.transData.cellIndex;
var cellDistance = map.TerrainGridSystem.CellGetHexagonDistance(selfCellIndex, enemyCellIndex);
var cellDistance = AreaManager.instance.GetDistance(selfCellIndex, enemyCellIndex);
if (cellDistance >= minDistance) continue;
if (!enemy.statusData.CheckCanBeTarget(this)) continue;
minDistance = cellDistance;