157 lines
4.4 KiB
C#
157 lines
4.4 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using Framework;
|
|
using Gameplay;
|
|
using LC.Newtonsoft.Json;
|
|
using UnityEngine;
|
|
using Sirenix.OdinInspector;
|
|
using TGS;
|
|
using Debug = DebugUtil;
|
|
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
|
|
using Object = UnityEngine.Object;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
using UnityEditor.SceneManagement;
|
|
#endif
|
|
|
|
[ExecuteAlways]
|
|
public class MapSceneEditor : MonoBehaviour
|
|
{
|
|
private static readonly Color cellBorderColor = new Color(229f, 229f, 229f, 237f) / 255f;
|
|
|
|
private static readonly float cellBorderWidth = 1f;
|
|
|
|
public static Map _map;
|
|
|
|
private const float UPP = 100;
|
|
|
|
private TerrainGridSystem _tgs;
|
|
|
|
[ReadOnly]
|
|
[HideInInspector]
|
|
public TextAsset mapDataJson;
|
|
|
|
//[BoxGroup("地图基本信息")]
|
|
[Space]
|
|
[HideLabel]
|
|
public MapData mapData;
|
|
|
|
//[BoxGroup("地图基本信息")]
|
|
[TableColumnWidth(57, Resizable = false)]
|
|
[PreviewField(Alignment = ObjectFieldAlignment.Center)]
|
|
[AssetList(Path = (Constants.MAP_TEXTURE_PATH_WITHOUT_ASSET))]
|
|
[OnValueChanged("OnMapMaterialChanged")]
|
|
[LabelText("地面图")]
|
|
public Material groundMaterial;
|
|
|
|
[LabelText("地图尺寸")]
|
|
[LabelWidth(100)]
|
|
[Indent]
|
|
[ReadOnly]
|
|
public Vector2 MapScale;
|
|
|
|
private void OnMapMaterialChanged()
|
|
{
|
|
if (groundMaterial != null && groundMaterial.mainTexture != null)
|
|
{
|
|
_tgs.GetComponent<MeshRenderer>().sharedMaterial = groundMaterial;
|
|
MapScale = (new Vector2(groundMaterial.mainTexture.width,
|
|
groundMaterial.mainTexture.height) / UPP);
|
|
_tgs.transform.localScale = new Vector3(MapScale.x, MapScale.y, 1);
|
|
_tgs.regularHexagons = false;
|
|
var width = GetHexCount(MapScale.x, _tgs.regularHexagonsWidth, false);
|
|
var height = GetHexCount(MapScale.y, _tgs.regularHexagonsWidth, true);
|
|
mapData.width = width;
|
|
mapData.height = height;
|
|
_tgs.rowCount = height;
|
|
_tgs.columnCount = width;
|
|
_tgs.regularHexagons = true;
|
|
}
|
|
}
|
|
|
|
private int GetHexCount(float totalLength, float hexWidth, bool isDiagonal)
|
|
{
|
|
if (isDiagonal)
|
|
return Mathf.FloorToInt((4 * totalLength) / (3 * hexWidth) - 1 / 3f);
|
|
else
|
|
{
|
|
var sqrt3 = Mathf.Sqrt(3);
|
|
var countInFloat = (totalLength - (sqrt3 / 4 * hexWidth)) / (Mathf.Sqrt(3) / 2 * hexWidth);
|
|
return Mathf.FloorToInt(countInFloat);
|
|
}
|
|
}
|
|
|
|
#region MainLife
|
|
|
|
private void Start()
|
|
{
|
|
_tgs = Object.FindObjectOfType<TerrainGridSystem>();
|
|
if (_tgs == null)
|
|
{
|
|
Debug.LogError("Terrain Grid System Is Null!");
|
|
return;
|
|
}
|
|
|
|
if (mapDataJson != null)
|
|
{
|
|
var jsonStr = mapDataJson.text;
|
|
if (string.IsNullOrEmpty(jsonStr))
|
|
{
|
|
mapData = new();
|
|
}
|
|
else
|
|
{
|
|
mapData = JsonConvert.DeserializeObject<MapData>(jsonStr);
|
|
}
|
|
|
|
LoadMap();
|
|
MapData.ResetEditor();
|
|
mapData.SetActiveSceneEdit(true);
|
|
}
|
|
}
|
|
|
|
private void LoadMap()
|
|
{
|
|
_map = MapUtils.LoadMap(_tgs, mapData);
|
|
_map?.ShowInfo((int)Map.InfoMask.ShowAll);
|
|
_tgs.highlightEffect = HighlightEffect.Default;
|
|
for (int i = 0; i < _map.MapData.BlocksData.Count; i++)
|
|
{
|
|
if (_map.MapData.BlocksData[i].TerrainTypeIndex != -1)
|
|
{
|
|
if (TerrainTypeConfig.TerrainTypeProperties[_map.MapData.BlocksData[i].TerrainTypeIndex].emptyBlock)
|
|
{
|
|
_map.TerrainGridSystem.CellSetBorderVisible(i, false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Button("保存")]
|
|
[Conditional("UNITY_EDITOR")]
|
|
public void SaveMapData()
|
|
{
|
|
if (mapDataJson != null)
|
|
{
|
|
var path = AssetDatabase.GetAssetPath(mapDataJson);
|
|
JsonHelper.SaveJson(path, mapData);
|
|
Debug.Log($"$path:{path}");
|
|
EditorSceneManager.SaveOpenScenes();
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
mapData.SetActiveSceneEdit(false);
|
|
_map = null;
|
|
SaveMapData();
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void SetCellBorderType()
|
|
{
|
|
_tgs.cellBorderColor = cellBorderColor;
|
|
_tgs.cellBorderThickness = cellBorderWidth;
|
|
}
|
|
} |