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

197 lines
7.0 KiB
C#

using Cysharp.Threading.Tasks;
using Framework;
using SRF;
using System;
using PhxhSDK;
using PhxhSDK.Phxh;
using TGS;
using UnityEngine;
using Constants = Framework.Constants;
using Object = UnityEngine.Object;
namespace Gameplay
{
public static class MapUtils
{
public static Map LoadMap(TerrainGridSystem tgs,MapData mapData)
{
var map = Map.Create(tgs, mapData);
map.TerrainGridSystem.highlightEffect = HighlightEffect.None;
var terrainTypeConfig = TerrainTypeConfig.TerrainTypeProperties;
if (terrainTypeConfig != null)
{
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<Map> LoadMap(MapData mapData)
{
Map map = null;
var tgs = UnityEngine.Object.FindObjectOfType<TerrainGridSystem>();
map = Map.Create(tgs, 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 mapPath = string.Format(Constants.MAP_CONFIG_FORMAT_PATH, mapFileName);
//var configFilePath = string.Format(Constants.MAP_CONFIG_FORMAT_PATH, mapFileName);
//var mapData = await JsonHelper.LoadFromAddressble<MapData>(mapPath);
var textAsset = await AssetManager.Instance.LoadAssetAsync<TextAsset>(mapPath);
MapData mapData = null;
if (textAsset != null)
{
mapData = JsonUtility.FromJson<MapData>(textAsset.text);
}
if (mapData == null)
{
DebugUtil.LogError("LoadMap.LoadMap failed, can not find map config: {0}", mapPath);
}
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>(mapFileName);
//
// 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);
}
}
}