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

197 lines
7.0 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using Cysharp.Threading.Tasks;
using Framework;
using SRF;
using System;
2023-10-19 15:00:19 +08:00
using PhxhSDK;
2023-11-30 15:47:49 +08:00
using PhxhSDK.Phxh;
2023-08-22 19:08:45 +08:00
using TGS;
using UnityEngine;
2023-10-19 15:00:19 +08:00
using Constants = Framework.Constants;
2023-08-22 19:08:45 +08:00
using Object = UnityEngine.Object;
namespace Gameplay
{
public static class MapUtils
{
2023-11-15 13:30:13 +08:00
public static Map LoadMap(TerrainGridSystem tgs,MapData mapData)
{
var map = Map.Create(tgs, mapData);
map.TerrainGridSystem.highlightEffect = HighlightEffect.None;
var terrainTypeConfig = TerrainTypeConfig.TerrainTypeProperties;
2023-12-18 19:42:46 +08:00
if (terrainTypeConfig != null)
2023-11-15 13:30:13 +08:00
{
2023-12-18 19:42:46 +08:00
for (int i = 0; i < map.MapData.BlocksData.Count; i++)
2023-11-15 13:30:13 +08:00
{
2023-12-18 19:42:46 +08:00
if (map.MapData.BlocksData[i].TerrainTypeIndex != -1)
2023-11-15 13:30:13 +08:00
{
2023-12-18 19:42:46 +08:00
if (terrainTypeConfig[map.MapData.BlocksData[i].TerrainTypeIndex].emptyBlock)
{
// map.TerrainGridSystem.CellSetColor(i, Color.red);
map.TerrainGridSystem.CellSetBorderVisible(i, false);
// map.TerrainGridSystem.CellSetVisible(i, false);
}
2023-11-15 13:30:13 +08:00
}
2023-12-18 19:42:46 +08:00
else
{
map.TerrainGridSystem.CellSetBorderVisible(i, true);
}
}
2023-11-15 13:30:13 +08:00
}
2023-12-18 19:42:46 +08:00
2023-11-15 13:30:13 +08:00
return map;
}
2023-11-20 14:10:18 +08:00
public static async UniTask<Map> LoadMap(MapData mapData)
2023-08-22 19:08:45 +08:00
{
Map map = null;
2023-11-20 14:10:18 +08:00
var tgs = UnityEngine.Object.FindObjectOfType<TerrainGridSystem>();
map = Map.Create(tgs, mapData);
2023-08-22 19:08:45 +08:00
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;
}
2023-10-19 15:00:19 +08:00
public static async UniTask<MapData> LoadMapData(string mapFileName)
{
2023-11-21 16:15:07 +08:00
var mapPath = string.Format(Constants.MAP_CONFIG_FORMAT_PATH, mapFileName);
2023-11-20 14:10:18 +08:00
//var configFilePath = string.Format(Constants.MAP_CONFIG_FORMAT_PATH, mapFileName);
2023-12-18 20:31:58 +08:00
//var mapData = await JsonHelper.LoadFromAddressble<MapData>(mapPath);
var textAsset = await AssetManager.Instance.LoadAssetAsync<TextAsset>(mapPath);
2024-01-10 13:40:07 +08:00
MapData mapData = null;
2023-12-18 20:31:58 +08:00
if (textAsset != null)
{
mapData = JsonUtility.FromJson<MapData>(textAsset.text);
}
2024-01-10 13:40:07 +08:00
2023-10-19 15:00:19 +08:00
if (mapData == null)
{
2023-11-21 16:15:07 +08:00
DebugUtil.LogError("LoadMap.LoadMap failed, can not find map config: {0}", mapPath);
2023-10-19 15:00:19 +08:00
}
2024-01-10 13:40:07 +08:00
2023-10-19 15:00:19 +08:00
return mapData;
}
2023-08-22 19:08:45 +08:00
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);
}
2023-11-20 14:10:18 +08:00
// 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;
// }
2023-08-22 19:08:45 +08:00
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);
}
}
}