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

332 lines
9.1 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-19 22:57:17 +08:00
using System.IO;
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;
2023-11-19 22:57:17 +08:00
using UnityEngine.Serialization;
2023-11-15 13:30:13 +08:00
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-22 12:23:18 +08:00
//#if UNITY_EDITOR
2023-11-21 15:04:56 +08:00
private const string MAP_DATA_NAME = "MapData";
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-19 22:57:17 +08:00
public TerrainGridSystem TGS => _tgs;
2023-11-19 23:35:44 +08:00
2023-11-19 22:57:17 +08:00
public bool HasSetGround => _groundMaterial != null && _groundMaterial.mainTexture != null;
2023-11-15 13:30:13 +08:00
2023-11-19 22:57:17 +08:00
public bool HasMap => _mapDataJson != null;
public MapData MapData
{
get => _mapData;
set
{
if (_mapData != value)
{
_mapData = value;
if (value == null)
{
Map = null;
}
else
{
Map = MapUtils.LoadMap(_tgs, _mapData);
2023-11-19 23:35:44 +08:00
MapUtils.BindCameraController(_camera, Map);
var cameraController = _camera.gameObject.GetComponent<CameraController>();
cameraController.InitializeCameraPosition(initialCameraPosition);
2023-11-19 22:57:17 +08:00
}
}
}
}
public Map Map { get; private set; }
2023-11-19 23:35:44 +08:00
2023-11-19 22:57:17 +08:00
public int Width => _tgs.columnCount;
public int Height => _tgs.rowCount;
2023-11-15 13:30:13 +08:00
2023-11-19 23:35:44 +08:00
[HideInInspector]
[SerializeField]
private Camera _camera;
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("地面图")]
2023-11-19 22:57:17 +08:00
[SerializeField]
private Material _groundMaterial;
2023-11-15 16:24:53 +08:00
2023-11-21 15:04:56 +08:00
private void OnMapMaterialChanged()
{
if (HasSetGround)
{
_tgs.GetComponent<MeshRenderer>().sharedMaterial = _groundMaterial;
ResetMapScale();
}
}
2023-11-15 16:24:53 +08:00
[LabelText("地图尺寸")]
[LabelWidth(100)]
2023-11-21 15:04:56 +08:00
//[ReadOnly]
2023-11-19 22:57:17 +08:00
[SerializeField]
2023-11-21 15:04:56 +08:00
[OnValueChanged("OnMapScaleChanged")]
[ShowIf("HasSetGround")]
2023-11-19 22:57:17 +08:00
private Vector2 _mapScale;
2023-11-21 15:04:56 +08:00
private void OnMapScaleChanged()
{
2023-11-21 18:32:18 +08:00
//防止网格上的显示在修改缩放后出现问题
_tgs.regularHexagons = false;
2023-11-21 15:04:56 +08:00
var currScale = _tgs.transform.localScale;
var xRatio = _mapScale.x / currScale.x;
var yRatio = _mapScale.y / currScale.y;
var ration = Mathf.Approximately(xRatio, 1) ? yRatio : xRatio;
_mapScale = currScale * ration;
_tgs.transform.localScale = new Vector3(_mapScale.x, _mapScale.y, 1);
2023-11-21 18:32:18 +08:00
_tgs.regularHexagons = true;
2023-11-21 15:04:56 +08:00
}
[Button("重置地图大小")]
[ShowIf("HasSetGround")]
private void ResetMapScale()
{
if (HasSetGround)
{
2023-11-21 18:32:18 +08:00
//防止网格上的显示在修改缩放后出现问题
_tgs.regularHexagons = false;
2023-11-21 15:04:56 +08:00
_mapScale = (new Vector2(_groundMaterial.mainTexture.width,
_groundMaterial.mainTexture.height) / UPP);
_tgs.transform.localScale = new Vector3(_mapScale.x, _mapScale.y, 1);
2023-11-21 18:32:18 +08:00
_tgs.regularHexagons = true;
2023-11-21 15:04:56 +08:00
}
}
2023-11-21 16:15:07 +08:00
[LabelText("网格宽高")]
[OnValueChanged("OnGridWidthHeightChanged")]
[HideIf("HasMap")]
[SerializeField]
private Vector2Int _gridWidthHeight;
private void OnGridWidthHeightChanged()
{
_tgs.columnCount = _gridWidthHeight.x;
_tgs.rowCount = _gridWidthHeight.y;
}
[HideIf("HasMap")]
[Button("铺满网格")]
private void FillMapWithGrid()
{
var width = GetHexCount(_mapScale.x, _tgs.regularHexagonsWidth, false);
var height = GetHexCount(_mapScale.y, _tgs.regularHexagonsWidth, true);
_tgs.rowCount = height;
_tgs.columnCount = width;
_gridWidthHeight = new Vector2Int(width, height);
}
2023-11-19 22:57:17 +08:00
[LabelText("摄像机初始视角")]
[LabelWidth(150)]
[OnValueChanged("OnInitialCameraPositionChanged")]
public Vector2Int initialCameraPosition;
private void OnInitialCameraPositionChanged()
{
2023-11-19 23:44:08 +08:00
if (_camera != null && HasMap)
2023-11-19 22:57:17 +08:00
{
initialCameraPosition.x = Math.Clamp(initialCameraPosition.x, 0, Height - 1);
initialCameraPosition.y = Math.Clamp(initialCameraPosition.y, 0, Width - 1);
2023-11-19 23:35:44 +08:00
var cameraController = _camera.gameObject.GetComponent<CameraController>();
2023-11-19 22:57:17 +08:00
if (_mapData != null)
_mapData.initialCameraPosition = initialCameraPosition;
cameraController.InitializeCameraPosition(initialCameraPosition);
}
}
public TextAsset MapDataJson
{
get => _mapDataJson;
private set
{
var data = JsonToMapData(value);
if (CheckMapData(data))
{
_mapDataJson = value;
MapData = data;
}
else
{
_mapDataJson = null;
MapData = null;
}
}
}
[LabelText("地图数据")]
[ShowIf("HasSetGround")]
[OnValueChanged("OnMapDataJsonChanged")]
[SerializeField]
private TextAsset _mapDataJson;
2023-11-16 18:27:03 +08:00
2023-11-19 22:57:17 +08:00
[Space]
[HideLabel]
[ShowIf("HasMap")]
[SerializeField]
private MapData _mapData;
private void OnMapDataJsonChanged(TextAsset k_mapDataJson)
{
MapDataJson = k_mapDataJson;
}
[Button("创建地图数据")]
[ShowIf("HasSetGround")]
private void CreateMapData()
{
2023-11-22 12:23:18 +08:00
#if UNITY_EDITOR
2023-11-19 22:57:17 +08:00
var scene = gameObject.scene;
2023-11-21 16:15:07 +08:00
// var scenePath = scene.path;
var mapDataDir = Path.GetDirectoryName(Constants.MAP_CONFIG_FORMAT_PATH);
var count = AssetDatabase.FindAssets("t:TextAsset", new[] { mapDataDir }).Length;
var dataName = $"{scene.name}_{count + 1}";
var dataPath = $"{mapDataDir}/{dataName}.json";
2023-11-19 22:57:17 +08:00
using (var fileStream = File.Create(dataPath))
{
}
AssetDatabase.Refresh();
var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(dataPath);
2023-11-21 15:04:56 +08:00
EditorGUIUtility.PingObject(textAsset);
2023-11-20 14:10:18 +08:00
if (MapDataJson == null)
2023-11-20 12:25:41 +08:00
MapDataJson = textAsset;
2023-11-22 12:23:18 +08:00
#endif
2023-11-19 22:57:17 +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-19 22:57:17 +08:00
private bool CheckMapData(MapData k_mapData)
2023-11-15 16:24:53 +08:00
{
2023-11-19 22:57:17 +08:00
if (k_mapData == null)
return false;
if (k_mapData.width != Width || k_mapData.height != Height)
2023-11-15 13:30:13 +08:00
{
2023-11-19 22:57:17 +08:00
Debug.LogError("当前地图数据宽高和当前地板宽高不匹配!");
return false;
2023-11-15 13:30:13 +08:00
}
2023-11-16 18:27:03 +08:00
2023-11-19 22:57:17 +08:00
return true;
}
private MapData JsonToMapData(TextAsset jsonText)
{
if (jsonText != null)
2023-11-16 18:27:03 +08:00
{
2023-11-19 22:57:17 +08:00
var jsonStr = jsonText.text;
2023-11-16 18:27:03 +08:00
if (string.IsNullOrEmpty(jsonStr))
{
2023-11-19 22:57:17 +08:00
var mapData = new MapData
{
width = Width,
height = Height
};
mapData.ResetBlocks();
return mapData;
2023-11-16 18:27:03 +08:00
}
else
{
2023-11-19 22:57:17 +08:00
return JsonConvert.DeserializeObject<MapData>(jsonStr);
2023-11-16 18:27:03 +08:00
}
}
2023-11-19 22:57:17 +08:00
return null;
2023-11-16 18:27:03 +08:00
}
2023-11-19 22:57:17 +08:00
#region MainLife
private void OnEnable()
{
2023-11-21 16:15:07 +08:00
var objs = Object.FindObjectsOfType<TerrainGridSystem>();
if (objs == null||objs.Length == 0)
{
Debug.LogError("Terrain Grid System Is Null!");
return;
}
_tgs = null;
foreach (var tgs in objs)
{
if (tgs.gameObject.scene == gameObject.scene)
_tgs = tgs;
}
2023-11-19 22:57:17 +08:00
if (_tgs == null)
{
Debug.LogError("Terrain Grid System Is Null!");
return;
}
2023-11-19 23:44:08 +08:00
2023-11-21 16:15:07 +08:00
_gridWidthHeight = new Vector2Int(_tgs.columnCount, _tgs.rowCount);
2023-11-19 22:57:17 +08:00
MapData = JsonToMapData(_mapDataJson);
2023-11-21 15:04:56 +08:00
_tgs.showCells = true;
_tgs.colorizeTerritories = true;
2023-11-21 19:52:50 +08:00
_tgs.TerritoryDestroyAll();
2023-11-19 22:57:17 +08:00
}
2023-11-19 23:35:44 +08:00
2023-11-20 14:10:18 +08:00
private void Awake()
{
2023-11-21 15:04:56 +08:00
if (Application.isPlaying)
{
if (_camera != null)
_camera.gameObject.SetActive(false);
}
2023-11-20 14:10:18 +08:00
}
2023-11-19 22:57:17 +08:00
2023-11-16 18:27:03 +08:00
[Button("保存")]
public void SaveMapData()
{
2023-11-22 12:23:18 +08:00
#if UNITY_EDITOR
2023-11-20 14:10:18 +08:00
if (_mapDataJson != null && !Application.isPlaying)
2023-11-16 18:27:03 +08:00
{
2023-11-19 22:57:17 +08:00
var path = AssetDatabase.GetAssetPath(_mapDataJson);
JsonHelper.SaveJson(path, _mapData);
Debug.Log($"保存地图数据成功!");
2023-11-16 18:27:03 +08:00
EditorSceneManager.SaveOpenScenes();
2023-11-21 15:04:56 +08:00
AssetDatabase.Refresh();
2023-11-16 18:27:03 +08:00
}
2023-11-22 12:23:18 +08:00
#endif
2023-11-16 18:27:03 +08:00
}
private void OnDestroy()
{
SaveMapData();
2023-11-15 13:30:13 +08:00
}
2023-11-19 22:57:17 +08:00
2023-11-17 15:57:18 +08:00
#endregion
2023-11-22 12:23:18 +08:00
//#endif
2023-11-15 13:30:13 +08:00
}