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

166 lines
5.5 KiB
C#

using Cysharp.Threading.Tasks;
using Framework;
using SRF;
using System;
using PhxhSDK;
using TGS;
using UnityEngine;
using Constants = Framework.Constants;
using Object = UnityEngine.Object;
namespace Gameplay
{
public static class MapUtils
{
public static async UniTask<Map> LoadMap(string mapFileName)
{
Map map = null;
var asset = await LoadMapAsset(mapFileName);
if (asset == null)
{
return map;
}
var mapData = await LoadMapData(mapFileName);
map = Map.Create(Object.Instantiate(asset), mapData);
map.TerrainGridSystem.highlightEffect = HighlightEffect.None;
var terrainTypeConfig = TerrainTypeConfig.TerrainTypeProperties;
for (int i = 0; i < map.MapData.BlocksData.Count; i++)
{
if (map.MapData.BlocksData[i].TerrainTypeIndex != -1)
{
if (terrainTypeConfig[map.MapData.BlocksData[i].TerrainTypeIndex].emptyBlock)
{
// map.TerrainGridSystem.CellSetColor(i, Color.red);
map.TerrainGridSystem.CellSetBorderVisible(i, false);
// map.TerrainGridSystem.CellSetVisible(i, false);
}
}
else
{
map.TerrainGridSystem.CellSetBorderVisible(i, true);
}
}
return map;
}
public static async UniTask<MapData> LoadMapData(string mapFileName)
{
var configFilePath = string.Format(Constants.MAP_CONFIG_FORMAT_PATH, mapFileName);
var mapData = await JsonHelper.LoadFromAddressble<MapData>(configFilePath);
if (mapData == null)
{
DebugUtil.LogError("LoadMap.LoadMap failed, can not find map config: {0}", configFilePath);
}
return mapData;
}
public static void UnLoadMap(Map map)
{
if (map != null)
{
map.Destroy();
}
}
public static Vector3Int LocalToGrid(GridLayout gridLayout, Vector3 local)
{
return gridLayout.LocalToCell(local);
}
public static Vector3 GridToWorld(GridLayout gridLayout, Vector3Int position)
{
return gridLayout.CellToWorld(position);
}
public static async UniTask<GameObject> LoadMapAsset(string mapFileName)
{
if(mapFileName == null)
{
return null;
}
var prefabFilePath = string.Format(Constants.MAP_ASSET_FORMAT_PATH, mapFileName);
var asset = await AssetManager.Instance.LoadAssetAsync<GameObject>(prefabFilePath);
if (asset == null)
{
DebugUtil.LogError("LoadMap.LoadMap failed, can not find map asset: {0}",prefabFilePath);
return null;
}
return asset;
}
public static void BindCameraController(Camera camera, Map map)
{
var cameraController = camera.gameObject.GetComponent<CameraController>();
if (!cameraController)
{
cameraController = camera.gameObject.AddComponent<CameraController>();
}
if (cameraController)
{
cameraController.Map = map;
cameraController.enabled = true;
}
cameraController.InitializeCameraPosition(map.MapData.initialCameraPosition);
}
public static void RemoveCameraController(Camera camera)
{
CameraManager.Instance.MainCamera.gameObject.RemoveComponentIfExists<CameraController>();
}
public static int Distance(Map map, Vector2Int a, Vector2Int b)
{
var cellIndex1 = map.BlockPosition2TGSCellIndex(a);
var cellIndex2 = map.BlockPosition2TGSCellIndex(b);
return Distance(map, cellIndex1, cellIndex2);
}
public static int Distance(Map map, int cellIndex1, int cellIndex2)
{
return map.TerrainGridSystem.CellGetHexagonDistance(cellIndex1, cellIndex2);
}
public static int Distance(Map map, Vector3 position1, Vector3 position2)
{
if (map.WorldPosition2TGSCellIndex(position1, out var cellIndex1)
&& map.WorldPosition2TGSCellIndex(position2, out var cellIndex2))
{
return Distance(map, cellIndex1, cellIndex2);
}
return 0;
}
public static bool CanPass(int moveLevel, TerrainTypeProperty terrainTypeProperty)
{
return terrainTypeProperty.crossLevel < moveLevel && !terrainTypeProperty.emptyBlock;
}
public static bool CanPass(int moveLevel, Map.RuntimeBlockProperty terrainTypeProperty)
{
return terrainTypeProperty.crossLevel < moveLevel && !terrainTypeProperty.isEmptyBlock;
}
public static void ForceSetCanCross(bool canCross, ref Map.RuntimeBlockProperty property)
{
property.isEmptyBlock = !canCross;
property.crossLevel = canCross ? 0 : int.MaxValue;
}
public static Color GetRGBAColor(string strColor)
{
var color = Convert.ToUInt32(strColor, 16);
return new Color((color >> 24 & 0xFF) / 255f, (color >> 16 & 0xFF) / 255f,
(color >> 8 & 0xFF) / 255f, (color & 0xFF) / 255f);
}
}
}