169 lines
5.4 KiB
C#
169 lines
5.4 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using System;
|
|
using Gameplay;
|
|
using PhxhSDK;
|
|
using TGS;
|
|
using UnityEngine;
|
|
using Constants = Framework.Constants;
|
|
using System.Collections.Generic;
|
|
|
|
public static class MapUtils
|
|
{
|
|
public static Map LoadMap(TerrainGridSystem tgs, MapData mapData, bool isLegionMap = false)
|
|
{
|
|
var map = Map.Create(tgs, mapData, isLegionMap);
|
|
map.TerrainGridSystem.highlightEffect = HighlightEffect.None;
|
|
|
|
Dictionary<int, TerrainTypeProperty> terrainTypeConfig = TerrainTypeConfig.DicTerrainTypePropertie;
|
|
if (null == terrainTypeConfig)
|
|
return map;
|
|
|
|
for (int i = 0; i < map.MapData.BlocksData.Count; i++)
|
|
{
|
|
map.TerrainGridSystem.CellSetBorderVisible(i, true);
|
|
}
|
|
|
|
return map;
|
|
}
|
|
|
|
public static async UniTask<Map> LoadMap(MapData mapData, bool isLegionMap = false)
|
|
{
|
|
TerrainGridSystem tgs = UnityEngine.Object.FindObjectOfType<TerrainGridSystem>();
|
|
Map map = Map.Create(tgs, mapData, isLegionMap);
|
|
map.TerrainGridSystem.highlightEffect = HighlightEffect.None;
|
|
|
|
Dictionary<int, TerrainTypeProperty> terrainTypeConfig = TerrainTypeConfig.DicTerrainTypePropertie;
|
|
for (int i = 0; i < map.MapData.BlocksData.Count; i++)
|
|
{
|
|
map.TerrainGridSystem.CellSetBorderVisible(i, true);
|
|
}
|
|
|
|
return map;
|
|
}
|
|
|
|
public async static UniTask<MapData> LoadMapData(string mapFileName)
|
|
{
|
|
if (string.IsNullOrEmpty(mapFileName))
|
|
return null;
|
|
|
|
string mapPath = string.Format(Constants.MAP_CONFIG_FORMAT_PATH, mapFileName);
|
|
TextAsset textAsset = await AssetManager.Instance.LoadAssetAsync<TextAsset>(mapPath);
|
|
MapData mapData = null;
|
|
if (textAsset is not null)
|
|
mapData = JsonUtility.FromJson<MapData>(textAsset.text);
|
|
|
|
if (null == mapData)
|
|
DebugUtil.LogError("LoadMap.LoadMap failed, can not find map config: {0}", mapPath);
|
|
|
|
AssetManager.Instance.Unload(mapPath);
|
|
|
|
return mapData;
|
|
}
|
|
|
|
public static void UnLoadMap(Map map)
|
|
{
|
|
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.CameraBounds = map.TerrainGridSystem.bounds;
|
|
cameraController.enabled = true;
|
|
}
|
|
|
|
cameraController.InitializeCameraPosition(
|
|
map.BlockPosition2WorldPosition(map.MapData.initialCameraPosition),
|
|
map.MapData.cameraMarginInfo);
|
|
}
|
|
|
|
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 TerrainTypeProperty GetTypeProperty(Map map, int pos)
|
|
{
|
|
if (map == null) return null;
|
|
if (map.MapData == null) return null;
|
|
if (pos >= map.MapData.Height * map.MapData.Width) return null;
|
|
if (map.MapData.GetBlockData(pos) == null) return null;
|
|
return map.MapData.GetBlockData(pos).GetTerrainTypeProperty();
|
|
}
|
|
|
|
public static int Distance(Map map, int cellIndex1, int cellIndex2)
|
|
{
|
|
// return AreaManager.instance.GetDistance(cellIndex1, 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 true;
|
|
}
|
|
|
|
public static bool CanPass(int moveLevel, Map.RuntimeBlockProperty terrainTypeProperty)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public static void ForceSetCanCross(bool canCross, ref Map.RuntimeBlockProperty property)
|
|
{
|
|
}
|
|
|
|
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);
|
|
}
|
|
} |