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

157 lines
4.4 KiB
C#
Raw Normal View History

2023-11-15 13:30:13 +08:00
using System;
2023-11-17 15:57:18 +08:00
using System.Diagnostics;
2023-11-15 13:30:13 +08:00
using Framework;
2023-11-17 15:57:18 +08:00
using Gameplay;
2023-11-15 18:19:17 +08:00
using LC.Newtonsoft.Json;
2023-11-15 13:30:13 +08:00
using UnityEngine;
using Sirenix.OdinInspector;
using TGS;
using Debug = DebugUtil;
2023-11-16 18:27:03 +08:00
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
2023-11-15 13:30:13 +08:00
using Object = UnityEngine.Object;
2023-11-16 18:27:03 +08:00
#if UNITY_EDITOR
2023-11-15 13:30:13 +08:00
using UnityEditor;
2023-11-16 18:27:03 +08:00
using UnityEditor.SceneManagement;
#endif
2023-11-15 13:30:13 +08:00
[ExecuteAlways]
public class MapSceneEditor : MonoBehaviour
{
2023-11-17 15:57:18 +08:00
private static readonly Color cellBorderColor = new Color(229f, 229f, 229f, 237f) / 255f;
private static readonly float cellBorderWidth = 1f;
public static Map _map;
2023-11-15 16:24:53 +08:00
private const float UPP = 100;
2023-11-16 18:27:03 +08:00
2023-11-15 13:30:13 +08:00
private TerrainGridSystem _tgs;
2023-11-16 18:27:03 +08:00
2023-11-15 18:19:17 +08:00
[ReadOnly]
2023-11-16 18:27:03 +08:00
[HideInInspector]
public TextAsset mapDataJson;
2023-11-15 13:30:13 +08:00
2023-11-17 15:57:18 +08:00
//[BoxGroup("地图基本信息")]
2023-11-16 18:27:03 +08:00
[Space]
[HideLabel]
public MapData mapData;
2023-11-15 13:30:13 +08:00
2023-11-17 15:57:18 +08:00
//[BoxGroup("地图基本信息")]
2023-11-15 16:24:53 +08:00
[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;
2023-11-16 18:27:03 +08:00
2023-11-15 16:24:53 +08:00
private void OnMapMaterialChanged()
2023-11-15 13:30:13 +08:00
{
2023-11-16 18:27:03 +08:00
if (groundMaterial != null && groundMaterial.mainTexture != null)
2023-11-15 13:30:13 +08:00
{
2023-11-15 16:24:53 +08:00
_tgs.GetComponent<MeshRenderer>().sharedMaterial = groundMaterial;
MapScale = (new Vector2(groundMaterial.mainTexture.width,
groundMaterial.mainTexture.height) / UPP);
2023-11-16 18:27:03 +08:00
_tgs.transform.localScale = new Vector3(MapScale.x, MapScale.y, 1);
2023-11-15 16:24:53 +08:00
_tgs.regularHexagons = false;
var width = GetHexCount(MapScale.x, _tgs.regularHexagonsWidth, false);
var height = GetHexCount(MapScale.y, _tgs.regularHexagonsWidth, true);
2023-11-17 15:57:18 +08:00
mapData.width = width;
mapData.height = height;
2023-11-15 16:24:53 +08:00
_tgs.rowCount = height;
_tgs.columnCount = width;
_tgs.regularHexagons = true;
2023-11-15 13:30:13 +08:00
}
2023-11-15 16:24:53 +08:00
}
2023-11-16 18:27:03 +08:00
2023-11-15 16:24:53 +08:00
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);
}
}
2023-11-16 18:27:03 +08:00
2023-11-17 15:57:18 +08:00
#region MainLife
private void Start()
2023-11-15 16:24:53 +08:00
{
2023-11-15 13:30:13 +08:00
_tgs = Object.FindObjectOfType<TerrainGridSystem>();
if (_tgs == null)
{
Debug.LogError("Terrain Grid System Is Null!");
2023-11-17 15:57:18 +08:00
return;
2023-11-15 13:30:13 +08:00
}
2023-11-16 18:27:03 +08:00
if (mapDataJson != null)
{
var jsonStr = mapDataJson.text;
if (string.IsNullOrEmpty(jsonStr))
{
mapData = new();
}
else
{
mapData = JsonConvert.DeserializeObject<MapData>(jsonStr);
}
2023-11-17 15:57:18 +08:00
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);
}
}
2023-11-16 18:27:03 +08:00
}
}
[Button("保存")]
2023-11-17 15:57:18 +08:00
[Conditional("UNITY_EDITOR")]
2023-11-16 18:27:03 +08:00
public void SaveMapData()
{
if (mapDataJson != null)
{
var path = AssetDatabase.GetAssetPath(mapDataJson);
JsonHelper.SaveJson(path, mapData);
Debug.Log($"$path:{path}");
EditorSceneManager.SaveOpenScenes();
}
}
private void OnDestroy()
{
2023-11-17 15:57:18 +08:00
mapData.SetActiveSceneEdit(false);
_map = null;
2023-11-16 18:27:03 +08:00
SaveMapData();
2023-11-15 13:30:13 +08:00
}
2023-11-17 15:57:18 +08:00
#endregion
private void SetCellBorderType()
{
_tgs.cellBorderColor = cellBorderColor;
_tgs.cellBorderThickness = cellBorderWidth;
}
2023-11-15 13:30:13 +08:00
}